2000-02-04 23:10:34 +08:00
|
|
|
"""An object-oriented interface to .netrc files."""
|
|
|
|
|
2001-01-15 08:50:52 +08:00
|
|
|
# Module and documentation by Eric S. Raymond, 21 Dec 1998
|
1998-12-22 13:19:29 +08:00
|
|
|
|
1999-05-04 02:10:37 +08:00
|
|
|
import os, shlex
|
1998-12-22 13:19:29 +08:00
|
|
|
|
2001-03-06 14:33:08 +08:00
|
|
|
__all__ = ["netrc", "NetrcParseError"]
|
|
|
|
|
|
|
|
|
|
|
|
class NetrcParseError(Exception):
|
|
|
|
"""Exception raised on syntax errors in the .netrc file."""
|
|
|
|
def __init__(self, msg, filename=None, lineno=None):
|
2001-04-15 20:51:42 +08:00
|
|
|
self.filename = filename
|
2001-03-06 14:33:08 +08:00
|
|
|
self.lineno = lineno
|
|
|
|
self.msg = msg
|
|
|
|
Exception.__init__(self, msg)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s (%s, line %s)" % (self.msg, self.filename, self.lineno)
|
|
|
|
|
2001-02-06 09:07:02 +08:00
|
|
|
|
1998-12-22 13:19:29 +08:00
|
|
|
class netrc:
|
|
|
|
def __init__(self, file=None):
|
|
|
|
if not file:
|
2002-03-18 10:13:48 +08:00
|
|
|
try:
|
|
|
|
file = os.path.join(os.environ['HOME'], ".netrc")
|
|
|
|
except KeyError:
|
|
|
|
raise IOError("Could not find .netrc: $HOME is not set")
|
2000-07-13 21:25:07 +08:00
|
|
|
fp = open(file)
|
1998-12-22 13:19:29 +08:00
|
|
|
self.hosts = {}
|
|
|
|
self.macros = {}
|
|
|
|
lexer = shlex.shlex(fp)
|
2001-01-15 08:50:52 +08:00
|
|
|
# Allows @ in hostnames. Not a big deal...
|
2000-07-13 21:12:21 +08:00
|
|
|
lexer.wordchars = lexer.wordchars + '.-@'
|
1998-12-22 13:19:29 +08:00
|
|
|
while 1:
|
|
|
|
# Look for a machine, default, or macdef top-level keyword
|
|
|
|
toplevel = tt = lexer.get_token()
|
2000-12-13 07:20:45 +08:00
|
|
|
if not tt:
|
1998-12-22 13:19:29 +08:00
|
|
|
break
|
|
|
|
elif tt == 'machine':
|
|
|
|
entryname = lexer.get_token()
|
|
|
|
elif tt == 'default':
|
|
|
|
entryname = 'default'
|
2001-01-15 08:50:52 +08:00
|
|
|
elif tt == 'macdef': # Just skip to end of macdefs
|
1998-12-22 13:19:29 +08:00
|
|
|
entryname = lexer.get_token()
|
|
|
|
self.macros[entryname] = []
|
2002-03-22 10:46:41 +08:00
|
|
|
lexer.whitespace = ' \t'
|
1998-12-22 13:19:29 +08:00
|
|
|
while 1:
|
|
|
|
line = lexer.instream.readline()
|
2002-03-22 10:46:41 +08:00
|
|
|
if not line or line == '\012':
|
|
|
|
lexer.whitespace = ' \t\r\n'
|
1998-12-22 13:19:29 +08:00
|
|
|
break
|
|
|
|
self.macros[entryname].append(line)
|
2002-03-22 10:46:41 +08:00
|
|
|
continue
|
1998-12-22 13:19:29 +08:00
|
|
|
else:
|
2001-03-06 14:33:08 +08:00
|
|
|
raise NetrcParseError(
|
|
|
|
"bad toplevel token %r" % tt, file, lexer.lineno)
|
1998-12-22 13:19:29 +08:00
|
|
|
|
|
|
|
# We're looking at start of an entry for a named machine or default.
|
2002-03-22 10:46:41 +08:00
|
|
|
login = account = password = None
|
|
|
|
self.hosts[entryname] = {}
|
1998-12-22 13:19:29 +08:00
|
|
|
while 1:
|
|
|
|
tt = lexer.get_token()
|
2002-03-22 10:46:41 +08:00
|
|
|
if (tt=='' or tt == 'machine' or
|
|
|
|
tt == 'default' or tt =='macdef'):
|
|
|
|
if login and password:
|
1998-12-22 13:19:29 +08:00
|
|
|
self.hosts[entryname] = (login, account, password)
|
|
|
|
lexer.push_token(tt)
|
|
|
|
break
|
|
|
|
else:
|
2001-03-06 14:33:08 +08:00
|
|
|
raise NetrcParseError(
|
|
|
|
"malformed %s entry %s terminated by %s"
|
|
|
|
% (toplevel, entryname, repr(tt)),
|
|
|
|
file, lexer.lineno)
|
1998-12-22 13:19:29 +08:00
|
|
|
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:
|
2001-03-06 14:33:08 +08:00
|
|
|
raise NetrcParseError("bad follower token %r" % tt,
|
|
|
|
file, lexer.lineno)
|
1998-12-22 13:19:29 +08:00
|
|
|
|
|
|
|
def authenticators(self, host):
|
2000-02-04 23:10:34 +08:00
|
|
|
"""Return a (user, account, password) tuple for given host."""
|
1998-12-22 13:19:29 +08: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 23:10:34 +08:00
|
|
|
"""Dump the class data in the format of a .netrc file."""
|
1998-12-22 13:19:29 +08: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
|
|
|
|
|
2001-01-15 08:50:52 +08:00
|
|
|
if __name__ == '__main__':
|
1998-12-22 13:19:29 +08:00
|
|
|
print netrc()
|