Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """An object-oriented interface to .netrc files.""" |
| 2 | |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 3 | # Module and documentation by Eric S. Raymond, 21 Dec 1998 |
| 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 | |
| 7 | class netrc: |
| 8 | def __init__(self, file=None): |
| 9 | if not file: |
| 10 | file = os.path.join(os.environ['HOME'], ".netrc") |
Eric S. Raymond | 5e70cfe | 2000-07-13 13:25:07 +0000 | [diff] [blame] | 11 | fp = open(file) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 12 | self.hosts = {} |
| 13 | self.macros = {} |
| 14 | lexer = shlex.shlex(fp) |
Eric S. Raymond | dfbd4c7 | 2000-07-13 13:12:21 +0000 | [diff] [blame] | 15 | # Allows @ in hostnames. Not a big deal... |
| 16 | lexer.wordchars = lexer.wordchars + '.-@' |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 17 | while 1: |
| 18 | # Look for a machine, default, or macdef top-level keyword |
| 19 | toplevel = tt = lexer.get_token() |
| 20 | if tt == '' or tt == None: |
| 21 | break |
| 22 | elif tt == 'machine': |
| 23 | entryname = lexer.get_token() |
| 24 | elif tt == 'default': |
| 25 | entryname = 'default' |
| 26 | elif tt == 'macdef': # Just skip to end of macdefs |
| 27 | entryname = lexer.get_token() |
| 28 | self.macros[entryname] = [] |
| 29 | lexer.whitepace = ' \t' |
| 30 | while 1: |
| 31 | line = lexer.instream.readline() |
| 32 | if not line or line == '\012' and tt == '\012': |
| 33 | lexer.whitepace = ' \t\r\n' |
| 34 | break |
| 35 | tt = line |
| 36 | self.macros[entryname].append(line) |
| 37 | else: |
| 38 | raise SyntaxError, "bad toplevel token %s, file %s, line %d" \ |
| 39 | % (tt, file, lexer.lineno) |
| 40 | |
| 41 | # We're looking at start of an entry for a named machine or default. |
| 42 | if toplevel == 'machine': |
| 43 | login = account = password = None |
| 44 | self.hosts[entryname] = {} |
| 45 | while 1: |
| 46 | tt = lexer.get_token() |
| 47 | if tt=='' or tt == 'machine' or tt == 'default' or tt == 'macdef': |
| 48 | if toplevel == 'macdef': |
| 49 | break; |
| 50 | elif login and password: |
| 51 | self.hosts[entryname] = (login, account, password) |
| 52 | lexer.push_token(tt) |
| 53 | break |
| 54 | else: |
| 55 | raise SyntaxError, "malformed %s entry %s terminated by %s" % (toplevel, entryname, repr(tt)) |
| 56 | elif tt == 'login' or tt == 'user': |
| 57 | login = lexer.get_token() |
| 58 | elif tt == 'account': |
| 59 | account = lexer.get_token() |
| 60 | elif tt == 'password': |
| 61 | password = lexer.get_token() |
| 62 | else: |
| 63 | raise SyntaxError, "bad follower token %s, file %s, line %d"%(tt,file,lexer.lineno) |
| 64 | |
| 65 | def authenticators(self, host): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 66 | """Return a (user, account, password) tuple for given host.""" |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 67 | if self.hosts.has_key(host): |
| 68 | return self.hosts[host] |
| 69 | elif self.hosts.has_key('default'): |
| 70 | return self.hosts['default'] |
| 71 | else: |
| 72 | return None |
| 73 | |
| 74 | def __repr__(self): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 75 | """Dump the class data in the format of a .netrc file.""" |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 76 | rep = "" |
| 77 | for host in self.hosts.keys(): |
| 78 | attrs = self.hosts[host] |
| 79 | rep = rep + "machine "+ host + "\n\tlogin " + repr(attrs[0]) + "\n" |
| 80 | if attrs[1]: |
| 81 | rep = rep + "account " + repr(attrs[1]) |
| 82 | rep = rep + "\tpassword " + repr(attrs[2]) + "\n" |
| 83 | for macro in self.macros.keys(): |
| 84 | rep = rep + "macdef " + macro + "\n" |
| 85 | for line in self.macros[macro]: |
| 86 | rep = rep + line |
| 87 | rep = rep + "\n" |
| 88 | return rep |
| 89 | |
| 90 | if __name__ == '__main__': |
| 91 | print netrc() |
| 92 | |