Change a variable type to avoid signed overflow; replace repeated '19999' constant...
[python.git] / Lib / netrc.py
blob723fc31d86fe52addacaac14551bdfa185ebccf8
1 """An object-oriented interface to .netrc files."""
3 # Module and documentation by Eric S. Raymond, 21 Dec 1998
5 import os, shlex
7 __all__ = ["netrc", "NetrcParseError"]
10 class NetrcParseError(Exception):
11 """Exception raised on syntax errors in the .netrc file."""
12 def __init__(self, msg, filename=None, lineno=None):
13 self.filename = filename
14 self.lineno = lineno
15 self.msg = msg
16 Exception.__init__(self, msg)
18 def __str__(self):
19 return "%s (%s, line %s)" % (self.msg, self.filename, self.lineno)
22 class netrc:
23 def __init__(self, file=None):
24 if file is None:
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")
29 self.hosts = {}
30 self.macros = {}
31 with open(file) as fp:
32 self._parse(file, fp)
34 def _parse(self, file, fp):
35 lexer = shlex.shlex(fp)
36 lexer.wordchars += r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
37 while 1:
38 # Look for a machine, default, or macdef top-level keyword
39 toplevel = tt = lexer.get_token()
40 if not tt:
41 break
42 elif tt == 'machine':
43 entryname = lexer.get_token()
44 elif tt == 'default':
45 entryname = 'default'
46 elif tt == 'macdef': # Just skip to end of macdefs
47 entryname = lexer.get_token()
48 self.macros[entryname] = []
49 lexer.whitespace = ' \t'
50 while 1:
51 line = lexer.instream.readline()
52 if not line or line == '\012':
53 lexer.whitespace = ' \t\r\n'
54 break
55 self.macros[entryname].append(line)
56 continue
57 else:
58 raise NetrcParseError(
59 "bad toplevel token %r" % tt, file, lexer.lineno)
61 # We're looking at start of an entry for a named machine or default.
62 login = ''
63 account = password = None
64 self.hosts[entryname] = {}
65 while 1:
66 tt = lexer.get_token()
67 if (tt=='' or tt == 'machine' or
68 tt == 'default' or tt =='macdef'):
69 if password:
70 self.hosts[entryname] = (login, account, password)
71 lexer.push_token(tt)
72 break
73 else:
74 raise NetrcParseError(
75 "malformed %s entry %s terminated by %s"
76 % (toplevel, entryname, repr(tt)),
77 file, lexer.lineno)
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:
85 raise NetrcParseError("bad follower token %r" % tt,
86 file, lexer.lineno)
88 def authenticators(self, host):
89 """Return a (user, account, password) tuple for given host."""
90 if host in self.hosts:
91 return self.hosts[host]
92 elif 'default' in self.hosts:
93 return self.hosts['default']
94 else:
95 return None
97 def __repr__(self):
98 """Dump the class data in the format of a .netrc file."""
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
113 if __name__ == '__main__':
114 print netrc()