1 """An object-oriented interface to .netrc files."""
3 # Module and documentation by Eric S. Raymond, 21 Dec 1998
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
16 Exception.__init
__(self
, msg
)
19 return "%s (%s, line %s)" % (self
.msg
, self
.filename
, self
.lineno
)
23 def __init__(self
, file=None):
26 file = os
.path
.join(os
.environ
['HOME'], ".netrc")
28 raise IOError("Could not find .netrc: $HOME is not set")
31 with
open(file) as fp
:
34 def _parse(self
, file, fp
):
35 lexer
= shlex
.shlex(fp
)
36 lexer
.wordchars
+= r
"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
38 # Look for a machine, default, or macdef top-level keyword
39 toplevel
= tt
= lexer
.get_token()
43 entryname
= lexer
.get_token()
46 elif tt
== 'macdef': # Just skip to end of macdefs
47 entryname
= lexer
.get_token()
48 self
.macros
[entryname
] = []
49 lexer
.whitespace
= ' \t'
51 line
= lexer
.instream
.readline()
52 if not line
or line
== '\012':
53 lexer
.whitespace
= ' \t\r\n'
55 self
.macros
[entryname
].append(line
)
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.
63 account
= password
= None
64 self
.hosts
[entryname
] = {}
66 tt
= lexer
.get_token()
67 if (tt
=='' or tt
== 'machine' or
68 tt
== 'default' or tt
=='macdef'):
70 self
.hosts
[entryname
] = (login
, account
, password
)
74 raise NetrcParseError(
75 "malformed %s entry %s terminated by %s"
76 % (toplevel
, entryname
, repr(tt
)),
78 elif tt
== 'login' or tt
== 'user':
79 login
= lexer
.get_token()
81 account
= lexer
.get_token()
82 elif tt
== 'password':
83 password
= lexer
.get_token()
85 raise NetrcParseError("bad follower token %r" % tt
,
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']
98 """Dump the class data in the format of a .netrc file."""
100 for host
in self
.hosts
.keys():
101 attrs
= self
.hosts
[host
]
102 rep
= rep
+ "machine "+ host
+ "\n\tlogin " + repr(attrs
[0]) + "\n"
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
]:
113 if __name__
== '__main__':