2000-02-04 11:10:34 -04:00
|
|
|
"""An object-oriented interface to .netrc files."""
|
|
|
|
|
1998-12-22 01:19:29 -04:00
|
|
|
# Module and documentation by Eric S. Raymond, 21 Dec 1998
|
|
|
|
|
1999-05-03 15:10:37 -03:00
|
|
|
import os, shlex
|
1998-12-22 01:19:29 -04:00
|
|
|
|
|
|
|
class netrc:
|
|
|
|
def __init__(self, file=None):
|
|
|
|
if not file:
|
|
|
|
file = os.path.join(os.environ['HOME'], ".netrc")
|
2000-07-13 10:25:07 -03:00
|
|
|
fp = open(file)
|
1998-12-22 01:19:29 -04:00
|
|
|
self.hosts = {}
|
|
|
|
self.macros = {}
|
|
|
|
lexer = shlex.shlex(fp)
|
2000-07-13 10:12:21 -03:00
|
|
|
# Allows @ in hostnames. Not a big deal...
|
|
|
|
lexer.wordchars = lexer.wordchars + '.-@'
|
1998-12-22 01:19:29 -04:00
|
|
|
while 1:
|
|
|
|
# Look for a machine, default, or macdef top-level keyword
|
|
|
|
toplevel = tt = lexer.get_token()
|
2000-12-12 19:20:45 -04:00
|
|
|
if not tt:
|
1998-12-22 01:19:29 -04:00
|
|
|
break
|
|
|
|
elif tt == 'machine':
|
|
|
|
entryname = lexer.get_token()
|
|
|
|
elif tt == 'default':
|
|
|
|
entryname = 'default'
|
|
|
|
elif tt == 'macdef': # Just skip to end of macdefs
|
|
|
|
entryname = lexer.get_token()
|
|
|
|
self.macros[entryname] = []
|
|
|
|
lexer.whitepace = ' \t'
|
|
|
|
while 1:
|
|
|
|
line = lexer.instream.readline()
|
|
|
|
if not line or line == '\012' and tt == '\012':
|
|
|
|
lexer.whitepace = ' \t\r\n'
|
|
|
|
break
|
|
|
|
tt = line
|
|
|
|
self.macros[entryname].append(line)
|
|
|
|
else:
|
|
|
|
raise SyntaxError, "bad toplevel token %s, file %s, line %d" \
|
|
|
|
% (tt, file, lexer.lineno)
|
|
|
|
|
|
|
|
# We're looking at start of an entry for a named machine or default.
|
|
|
|
if toplevel == 'machine':
|
|
|
|
login = account = password = None
|
|
|
|
self.hosts[entryname] = {}
|
|
|
|
while 1:
|
|
|
|
tt = lexer.get_token()
|
|
|
|
if tt=='' or tt == 'machine' or tt == 'default' or tt == 'macdef':
|
|
|
|
if toplevel == 'macdef':
|
2000-12-23 10:20:24 -04:00
|
|
|
break
|
1998-12-22 01:19:29 -04:00
|
|
|
elif login and password:
|
|
|
|
self.hosts[entryname] = (login, account, password)
|
|
|
|
lexer.push_token(tt)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise SyntaxError, "malformed %s entry %s terminated by %s" % (toplevel, entryname, repr(tt))
|
|
|
|
elif tt == 'login' or tt == 'user':
|
|
|
|
login = lexer.get_token()
|
|
|
|
elif tt == 'account':
|
|
|
|
account = lexer.get_token()
|
|
|
|
elif tt == 'password':
|
|
|
|
password = lexer.get_token()
|
|
|
|
else:
|
|
|
|
raise SyntaxError, "bad follower token %s, file %s, line %d"%(tt,file,lexer.lineno)
|
|
|
|
|
|
|
|
def authenticators(self, host):
|
2000-02-04 11:10:34 -04:00
|
|
|
"""Return a (user, account, password) tuple for given host."""
|
1998-12-22 01:19:29 -04:00
|
|
|
if self.hosts.has_key(host):
|
|
|
|
return self.hosts[host]
|
|
|
|
elif self.hosts.has_key('default'):
|
|
|
|
return self.hosts['default']
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __repr__(self):
|
2000-02-04 11:10:34 -04:00
|
|
|
"""Dump the class data in the format of a .netrc file."""
|
1998-12-22 01:19:29 -04:00
|
|
|
rep = ""
|
|
|
|
for host in self.hosts.keys():
|
|
|
|
attrs = self.hosts[host]
|
|
|
|
rep = rep + "machine "+ host + "\n\tlogin " + repr(attrs[0]) + "\n"
|
|
|
|
if attrs[1]:
|
|
|
|
rep = rep + "account " + repr(attrs[1])
|
|
|
|
rep = rep + "\tpassword " + repr(attrs[2]) + "\n"
|
|
|
|
for macro in self.macros.keys():
|
|
|
|
rep = rep + "macdef " + macro + "\n"
|
|
|
|
for line in self.macros[macro]:
|
|
|
|
rep = rep + line
|
|
|
|
rep = rep + "\n"
|
|
|
|
return rep
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print netrc()
|
|
|
|
|