~/.netrc & multiple accounts with same @domain

Pan Tsu inyaoo at gmail.com
Tue Mar 22 14:27:58 GMT 2011


David Soulayrol <david.soulayrol at gmail.com> writes:

> 2011/3/21 Pan Tsu <inyaoo at gmail.com>:
>> offlineimap is unable to get password from ~/.netrc when it contains
>> more than one entry for `machine'
>>
>>   machine imap.gmail.com login foo password blah
>>   machine imap.gmail.com login bar password blah
>>
>
> I'm really interested in a nice fix for this problem too. My current
> workaround is to use two domain names which point to the same server
> (at ovh.net), but that's a bit annoying.

Here is another workaround using remotepasseval.

  [general]
  pythonfile = ~/.offlineimap.py

  [Repository foo|remote]
  type = Gmail
  remoteuser = foo
  remotepasseval = getnetrc(machine='imap.gmail.com', login='foo')['password']

-- crapcode begins --
import os

def getnetrc(authinfo=None, **match):
    '''A dumb filter for ~/.netrc. It understands oneline entries,
    comments and default entry. No macros or multiline entries.

    Return first matching dict or None.'''
    default = None
    if not authinfo:
        authinfo = os.getenv('NETRC') or '~/.netrc'

    for li in open(os.path.expanduser(authinfo)).readlines():
        li = li.partition('#')[0].split() # current line in list
        if not li:
            continue
        if li[0] == 'default':
            default = dict(zip(li[1:-1:2], li[2::2]))
            continue
        elif li[0] == 'macdef':
            continue

        li = dict(zip(li[:-1:2], li[1::2])) # current line in dict

        if match and contains(li, match):
            return li

    if default and contains(default, match):
        return default

def contains(d, m):
    '''Return True if d contains all items of m.'''
    for k in m:
        if not k in d or d[k] != m[k]:
            return False
    return True

# debug
if __name__ == "__main__":
    print getnetrc(machine='imap.gmail.com', login='foo')
-- crapcode ends --




More information about the OfflineIMAP-project mailing list