blob: bf944b7a7ef69914560c8b97264e419c29fb7df8 [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""An object-oriented interface to .netrc files."""
2
Tim Peters2344fae2001-01-15 00:50:52 +00003# Module and documentation by Eric S. Raymond, 21 Dec 1998
Guido van Rossum9c30c241998-12-22 05:19:29 +00004
Guido van Rossum1e1cfa31999-05-03 18:10:37 +00005import os, shlex
Guido van Rossum9c30c241998-12-22 05:19:29 +00006
Skip Montanaro269b83b2001-02-06 01:07:02 +00007__all__ = ["netrc"]
8
Guido van Rossum9c30c241998-12-22 05:19:29 +00009class netrc:
10 def __init__(self, file=None):
11 if not file:
12 file = os.path.join(os.environ['HOME'], ".netrc")
Eric S. Raymond5e70cfe2000-07-13 13:25:07 +000013 fp = open(file)
Guido van Rossum9c30c241998-12-22 05:19:29 +000014 self.hosts = {}
15 self.macros = {}
16 lexer = shlex.shlex(fp)
Tim Peters2344fae2001-01-15 00:50:52 +000017 # Allows @ in hostnames. Not a big deal...
Eric S. Raymonddfbd4c72000-07-13 13:12:21 +000018 lexer.wordchars = lexer.wordchars + '.-@'
Guido van Rossum9c30c241998-12-22 05:19:29 +000019 while 1:
20 # Look for a machine, default, or macdef top-level keyword
21 toplevel = tt = lexer.get_token()
Fred Drake8152d322000-12-12 23:20:45 +000022 if not tt:
Guido van Rossum9c30c241998-12-22 05:19:29 +000023 break
24 elif tt == 'machine':
25 entryname = lexer.get_token()
26 elif tt == 'default':
27 entryname = 'default'
Tim Peters2344fae2001-01-15 00:50:52 +000028 elif tt == 'macdef': # Just skip to end of macdefs
Guido van Rossum9c30c241998-12-22 05:19:29 +000029 entryname = lexer.get_token()
30 self.macros[entryname] = []
31 lexer.whitepace = ' \t'
32 while 1:
33 line = lexer.instream.readline()
34 if not line or line == '\012' and tt == '\012':
35 lexer.whitepace = ' \t\r\n'
36 break
37 tt = line
38 self.macros[entryname].append(line)
39 else:
40 raise SyntaxError, "bad toplevel token %s, file %s, line %d" \
Tim Peters2344fae2001-01-15 00:50:52 +000041 % (tt, file, lexer.lineno)
Guido van Rossum9c30c241998-12-22 05:19:29 +000042
43 # We're looking at start of an entry for a named machine or default.
44 if toplevel == 'machine':
45 login = account = password = None
46 self.hosts[entryname] = {}
47 while 1:
48 tt = lexer.get_token()
49 if tt=='' or tt == 'machine' or tt == 'default' or tt == 'macdef':
50 if toplevel == 'macdef':
Andrew M. Kuchling49d27c82000-12-23 14:20:24 +000051 break
Guido van Rossum9c30c241998-12-22 05:19:29 +000052 elif login and password:
53 self.hosts[entryname] = (login, account, password)
54 lexer.push_token(tt)
55 break
56 else:
57 raise SyntaxError, "malformed %s entry %s terminated by %s" % (toplevel, entryname, repr(tt))
58 elif tt == 'login' or tt == 'user':
59 login = lexer.get_token()
60 elif tt == 'account':
61 account = lexer.get_token()
62 elif tt == 'password':
63 password = lexer.get_token()
64 else:
65 raise SyntaxError, "bad follower token %s, file %s, line %d"%(tt,file,lexer.lineno)
66
67 def authenticators(self, host):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000068 """Return a (user, account, password) tuple for given host."""
Guido van Rossum9c30c241998-12-22 05:19:29 +000069 if self.hosts.has_key(host):
70 return self.hosts[host]
71 elif self.hosts.has_key('default'):
72 return self.hosts['default']
73 else:
74 return None
75
76 def __repr__(self):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000077 """Dump the class data in the format of a .netrc file."""
Guido van Rossum9c30c241998-12-22 05:19:29 +000078 rep = ""
79 for host in self.hosts.keys():
80 attrs = self.hosts[host]
81 rep = rep + "machine "+ host + "\n\tlogin " + repr(attrs[0]) + "\n"
82 if attrs[1]:
83 rep = rep + "account " + repr(attrs[1])
84 rep = rep + "\tpassword " + repr(attrs[2]) + "\n"
85 for macro in self.macros.keys():
86 rep = rep + "macdef " + macro + "\n"
87 for line in self.macros[macro]:
88 rep = rep + line
89 rep = rep + "\n"
90 return rep
91
Tim Peters2344fae2001-01-15 00:50:52 +000092if __name__ == '__main__':
Guido van Rossum9c30c241998-12-22 05:19:29 +000093 print netrc()