Mercurial > cpython-lru_cache-weakref
annotate Lib/netrc.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | cb3a77b0f8dd |
| children | 2e19c65d6688 |
| rev | line source |
|---|---|
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
10823
diff
changeset
|
1 """An object-oriented interface to .netrc files.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
10823
diff
changeset
|
2 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15655
diff
changeset
|
3 # Module and documentation by Eric S. Raymond, 21 Dec 1998 |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
10823
0bc7c502eb4d
No need to import sys or string. (Andrew Dalke & kjpylint)
Guido van Rossum <guido@python.org>
parents:
10556
diff
changeset
|
5 import os, shlex |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
16987
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
7 __all__ = ["netrc", "NetrcParseError"] |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
8 |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
9 |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
10 class NetrcParseError(Exception): |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
11 """Exception raised on syntax errors in the .netrc file.""" |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
12 def __init__(self, msg, filename=None, lineno=None): |
|
17525
5fb0c46753f9
Fix typo in attribute name (file should be filename) found by
Guido van Rossum <guido@python.org>
parents:
16987
diff
changeset
|
13 self.filename = filename |
|
16987
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
14 self.lineno = lineno |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
15 self.msg = msg |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
16 Exception.__init__(self, msg) |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
17 |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
18 def __str__(self): |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
19 return "%s (%s, line %s)" % (self.msg, self.filename, self.lineno) |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
20 |
|
16460
c18ac8c2f036
added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
15870
diff
changeset
|
21 |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 class netrc: |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 def __init__(self, file=None): |
|
23622
5edd80726d96
Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
24 if file is None: |
|
22362
8ed647d31f44
netrc will now raise a more predictable exception when $HOME is not set
Paul Prescod <prescod@prescod.net>
parents:
17525
diff
changeset
|
25 try: |
|
8ed647d31f44
netrc will now raise a more predictable exception when $HOME is not set
Paul Prescod <prescod@prescod.net>
parents:
17525
diff
changeset
|
26 file = os.path.join(os.environ['HOME'], ".netrc") |
|
8ed647d31f44
netrc will now raise a more predictable exception when $HOME is not set
Paul Prescod <prescod@prescod.net>
parents:
17525
diff
changeset
|
27 except KeyError: |
|
8ed647d31f44
netrc will now raise a more predictable exception when $HOME is not set
Paul Prescod <prescod@prescod.net>
parents:
17525
diff
changeset
|
28 raise IOError("Could not find .netrc: $HOME is not set") |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 self.hosts = {} |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 self.macros = {} |
|
54382
e6e3a0e91bea
explicitly close the file, merged from py3k
Philip Jenvey <pjenvey@underboss.org>
parents:
28803
diff
changeset
|
31 with open(file) as fp: |
|
e6e3a0e91bea
explicitly close the file, merged from py3k
Philip Jenvey <pjenvey@underboss.org>
parents:
28803
diff
changeset
|
32 self._parse(file, fp) |
|
e6e3a0e91bea
explicitly close the file, merged from py3k
Philip Jenvey <pjenvey@underboss.org>
parents:
28803
diff
changeset
|
33 |
|
e6e3a0e91bea
explicitly close the file, merged from py3k
Philip Jenvey <pjenvey@underboss.org>
parents:
28803
diff
changeset
|
34 def _parse(self, file, fp): |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 lexer = shlex.shlex(fp) |
|
28743
6e2e5985375c
SF 557704: netrc module can't handle all passwords
Raymond Hettinger <python@rcn.com>
parents:
23622
diff
changeset
|
36 lexer.wordchars += r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" |
|
66598
e7844b7f6ed4
Merged revisions 86925 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
54382
diff
changeset
|
37 lexer.commenters = lexer.commenters.replace('#', '') |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 while 1: |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 # Look for a machine, default, or macdef top-level keyword |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 toplevel = tt = lexer.get_token() |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
13587
diff
changeset
|
41 if not tt: |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 break |
|
66598
e7844b7f6ed4
Merged revisions 86925 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
54382
diff
changeset
|
43 elif tt[0] == '#': |
|
70766
cb3a77b0f8dd
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66598
diff
changeset
|
44 # seek to beginning of comment, in case reading the token put |
|
cb3a77b0f8dd
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66598
diff
changeset
|
45 # us on a new line, and then skip the rest of the line. |
|
cb3a77b0f8dd
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66598
diff
changeset
|
46 pos = len(tt) + 1 |
|
cb3a77b0f8dd
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66598
diff
changeset
|
47 lexer.instream.seek(-pos, 1) |
|
cb3a77b0f8dd
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66598
diff
changeset
|
48 lexer.instream.readline() |
|
cb3a77b0f8dd
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66598
diff
changeset
|
49 continue |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 elif tt == 'machine': |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 entryname = lexer.get_token() |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 elif tt == 'default': |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 entryname = 'default' |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15655
diff
changeset
|
54 elif tt == 'macdef': # Just skip to end of macdefs |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 entryname = lexer.get_token() |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 self.macros[entryname] = [] |
|
22437
d2e457c44bf2
[Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents:
22362
diff
changeset
|
57 lexer.whitespace = ' \t' |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 while 1: |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 line = lexer.instream.readline() |
|
22437
d2e457c44bf2
[Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents:
22362
diff
changeset
|
60 if not line or line == '\012': |
|
d2e457c44bf2
[Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents:
22362
diff
changeset
|
61 lexer.whitespace = ' \t\r\n' |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 break |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 self.macros[entryname].append(line) |
|
22437
d2e457c44bf2
[Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents:
22362
diff
changeset
|
64 continue |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 else: |
|
16987
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
66 raise NetrcParseError( |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
67 "bad toplevel token %r" % tt, file, lexer.lineno) |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 # We're looking at start of an entry for a named machine or default. |
|
28803
c34cf9ce952f
SF bug 557704: netrc module can't handle all passwords
Raymond Hettinger <python@rcn.com>
parents:
28743
diff
changeset
|
70 login = '' |
|
c34cf9ce952f
SF bug 557704: netrc module can't handle all passwords
Raymond Hettinger <python@rcn.com>
parents:
28743
diff
changeset
|
71 account = password = None |
|
22437
d2e457c44bf2
[Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents:
22362
diff
changeset
|
72 self.hosts[entryname] = {} |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 while 1: |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 tt = lexer.get_token() |
|
70766
cb3a77b0f8dd
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66598
diff
changeset
|
75 if (tt.startswith('#') or |
|
cb3a77b0f8dd
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66598
diff
changeset
|
76 tt in {'', 'machine', 'default', 'macdef'}): |
|
28803
c34cf9ce952f
SF bug 557704: netrc module can't handle all passwords
Raymond Hettinger <python@rcn.com>
parents:
28743
diff
changeset
|
77 if password: |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 self.hosts[entryname] = (login, account, password) |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 lexer.push_token(tt) |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 break |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 else: |
|
16987
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
82 raise NetrcParseError( |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
83 "malformed %s entry %s terminated by %s" |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
84 % (toplevel, entryname, repr(tt)), |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
85 file, lexer.lineno) |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 elif tt == 'login' or tt == 'user': |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 login = lexer.get_token() |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 elif tt == 'account': |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 account = lexer.get_token() |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 elif tt == 'password': |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 password = lexer.get_token() |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 else: |
|
16987
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
93 raise NetrcParseError("bad follower token %r" % tt, |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
94 file, lexer.lineno) |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 def authenticators(self, host): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
10823
diff
changeset
|
97 """Return a (user, account, password) tuple for given host.""" |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22437
diff
changeset
|
98 if host in self.hosts: |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 return self.hosts[host] |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22437
diff
changeset
|
100 elif 'default' in self.hosts: |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 return self.hosts['default'] |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 else: |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 return None |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 def __repr__(self): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
10823
diff
changeset
|
106 """Dump the class data in the format of a .netrc file.""" |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 rep = "" |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 for host in self.hosts.keys(): |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 attrs = self.hosts[host] |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 rep = rep + "machine "+ host + "\n\tlogin " + repr(attrs[0]) + "\n" |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 if attrs[1]: |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 rep = rep + "account " + repr(attrs[1]) |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 rep = rep + "\tpassword " + repr(attrs[2]) + "\n" |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 for macro in self.macros.keys(): |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 rep = rep + "macdef " + macro + "\n" |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 for line in self.macros[macro]: |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 rep = rep + line |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 rep = rep + "\n" |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 return rep |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15655
diff
changeset
|
121 if __name__ == '__main__': |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 print netrc() |
