Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """An object-oriented interface to .netrc files.""" |
| 2 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 3 | # Module and documentation by Eric S. Raymond, 21 Dec 1998 |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 1e1cfa3 | 1999-05-03 18:10:37 +0000 | [diff] [blame] | 5 | import os, shlex |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 6 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame] | 7 | __all__ = ["netrc"] |
| 8 | |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 9 | class netrc: |
| 10 | def __init__(self, file=None): |
| 11 | if not file: |
| 12 | file = os.path.join(os.environ['HOME'], ".netrc") |
Eric S. Raymond | 5e70cfe | 2000-07-13 13:25:07 +0000 | [diff] [blame] | 13 | fp = open(file) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 14 | self.hosts = {} |
| 15 | self.macros = {} |
| 16 | lexer = shlex.shlex(fp) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 17 | # Allows @ in hostnames. Not a big deal... |
Eric S. Raymond | dfbd4c7 | 2000-07-13 13:12:21 +0000 | [diff] [blame] | 18 | lexer.wordchars = lexer.wordchars + '.-@' |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 19 | while 1: |
| 20 | # Look for a machine, default, or macdef top-level keyword |
| 21 | toplevel = tt = lexer.get_token() |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 22 | if not tt: |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 23 | break |
| 24 | elif tt == 'machine': |
| 25 | entryname = lexer.get_token() |
| 26 | elif tt == 'default': |
| 27 | entryname = 'default' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 28 | elif tt == 'macdef': # Just skip to end of macdefs |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 29 | 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 Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 41 | % (tt, file, lexer.lineno) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 42 | |
| 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. Kuchling | 49d27c8 | 2000-12-23 14:20:24 +0000 | [diff] [blame] | 51 | break |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 52 | 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 Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 68 | """Return a (user, account, password) tuple for given host.""" |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 69 | 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 Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 77 | """Dump the class data in the format of a .netrc file.""" |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 78 | 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 Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 92 | if __name__ == '__main__': |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 93 | print netrc() |