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 | |
Fred Drake | ec6ec90 | 2001-03-06 06:33:08 +0000 | [diff] [blame] | 7 | __all__ = ["netrc", "NetrcParseError"] |
| 8 | |
| 9 | |
| 10 | class NetrcParseError(Exception): |
| 11 | """Exception raised on syntax errors in the .netrc file.""" |
| 12 | def __init__(self, msg, filename=None, lineno=None): |
Guido van Rossum | 815bee4 | 2001-04-15 12:51:42 +0000 | [diff] [blame] | 13 | self.filename = filename |
Fred Drake | ec6ec90 | 2001-03-06 06:33:08 +0000 | [diff] [blame] | 14 | self.lineno = lineno |
| 15 | self.msg = msg |
| 16 | Exception.__init__(self, msg) |
| 17 | |
| 18 | def __str__(self): |
| 19 | return "%s (%s, line %s)" % (self.msg, self.filename, self.lineno) |
| 20 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 22 | class netrc: |
| 23 | def __init__(self, file=None): |
Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 24 | if file is None: |
Paul Prescod | b845f3b | 2002-03-18 02:13:48 +0000 | [diff] [blame] | 25 | try: |
| 26 | file = os.path.join(os.environ['HOME'], ".netrc") |
| 27 | except KeyError: |
| 28 | raise IOError("Could not find .netrc: $HOME is not set") |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 29 | self.hosts = {} |
| 30 | self.macros = {} |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 31 | with open(file) as fp: |
| 32 | self._parse(file, fp) |
| 33 | |
| 34 | def _parse(self, file, fp): |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 35 | lexer = shlex.shlex(fp) |
Raymond Hettinger | 6eca358 | 2003-04-23 18:59:54 +0000 | [diff] [blame] | 36 | lexer.wordchars += r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 37 | while 1: |
| 38 | # Look for a machine, default, or macdef top-level keyword |
| 39 | toplevel = tt = lexer.get_token() |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 40 | if not tt: |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 41 | break |
| 42 | elif tt == 'machine': |
| 43 | entryname = lexer.get_token() |
| 44 | elif tt == 'default': |
| 45 | entryname = 'default' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 46 | elif tt == 'macdef': # Just skip to end of macdefs |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 47 | entryname = lexer.get_token() |
| 48 | self.macros[entryname] = [] |
Andrew M. Kuchling | 366a1df | 2002-03-22 02:46:41 +0000 | [diff] [blame] | 49 | lexer.whitespace = ' \t' |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 50 | while 1: |
| 51 | line = lexer.instream.readline() |
Andrew M. Kuchling | 366a1df | 2002-03-22 02:46:41 +0000 | [diff] [blame] | 52 | if not line or line == '\012': |
| 53 | lexer.whitespace = ' \t\r\n' |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 54 | break |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 55 | self.macros[entryname].append(line) |
Andrew M. Kuchling | 366a1df | 2002-03-22 02:46:41 +0000 | [diff] [blame] | 56 | continue |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 57 | else: |
Fred Drake | ec6ec90 | 2001-03-06 06:33:08 +0000 | [diff] [blame] | 58 | raise NetrcParseError( |
| 59 | "bad toplevel token %r" % tt, file, lexer.lineno) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 60 | |
| 61 | # We're looking at start of an entry for a named machine or default. |
Raymond Hettinger | b34ef94 | 2003-04-24 20:11:20 +0000 | [diff] [blame] | 62 | login = '' |
| 63 | account = password = None |
Andrew M. Kuchling | 366a1df | 2002-03-22 02:46:41 +0000 | [diff] [blame] | 64 | self.hosts[entryname] = {} |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 65 | while 1: |
| 66 | tt = lexer.get_token() |
Andrew M. Kuchling | 366a1df | 2002-03-22 02:46:41 +0000 | [diff] [blame] | 67 | if (tt=='' or tt == 'machine' or |
| 68 | tt == 'default' or tt =='macdef'): |
Raymond Hettinger | b34ef94 | 2003-04-24 20:11:20 +0000 | [diff] [blame] | 69 | if password: |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 70 | self.hosts[entryname] = (login, account, password) |
| 71 | lexer.push_token(tt) |
| 72 | break |
| 73 | else: |
Fred Drake | ec6ec90 | 2001-03-06 06:33:08 +0000 | [diff] [blame] | 74 | raise NetrcParseError( |
| 75 | "malformed %s entry %s terminated by %s" |
| 76 | % (toplevel, entryname, repr(tt)), |
| 77 | file, lexer.lineno) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 78 | elif tt == 'login' or tt == 'user': |
| 79 | login = lexer.get_token() |
| 80 | elif tt == 'account': |
| 81 | account = lexer.get_token() |
| 82 | elif tt == 'password': |
| 83 | password = lexer.get_token() |
| 84 | else: |
Fred Drake | ec6ec90 | 2001-03-06 06:33:08 +0000 | [diff] [blame] | 85 | raise NetrcParseError("bad follower token %r" % tt, |
| 86 | file, lexer.lineno) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 87 | |
| 88 | def authenticators(self, host): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 89 | """Return a (user, account, password) tuple for given host.""" |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 90 | if host in self.hosts: |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 91 | return self.hosts[host] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 92 | elif 'default' in self.hosts: |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 93 | return self.hosts['default'] |
| 94 | else: |
| 95 | return None |
| 96 | |
| 97 | def __repr__(self): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 98 | """Dump the class data in the format of a .netrc file.""" |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 99 | rep = "" |
| 100 | for host in self.hosts.keys(): |
| 101 | attrs = self.hosts[host] |
| 102 | rep = rep + "machine "+ host + "\n\tlogin " + repr(attrs[0]) + "\n" |
| 103 | if attrs[1]: |
| 104 | rep = rep + "account " + repr(attrs[1]) |
| 105 | rep = rep + "\tpassword " + repr(attrs[2]) + "\n" |
| 106 | for macro in self.macros.keys(): |
| 107 | rep = rep + "macdef " + macro + "\n" |
| 108 | for line in self.macros[macro]: |
| 109 | rep = rep + line |
| 110 | rep = rep + "\n" |
| 111 | return rep |
| 112 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 113 | if __name__ == '__main__': |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 114 | print(netrc()) |