Mercurial > cpython-lru_cache-weakref
annotate Lib/netrc.py @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | 8625ce7da152 |
| children | 6396d1fc72da 7d69d04522e3 |
| 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 |
|
70769
8625ce7da152
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66597
diff
changeset
|
5 import io, 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 = {} |
|
43684
4bb6a0c353ab
Patch # 1331 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
31 with open(file) as fp: |
|
4bb6a0c353ab
Patch # 1331 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
32 self._parse(file, fp) |
|
4bb6a0c353ab
Patch # 1331 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
33 |
|
4bb6a0c353ab
Patch # 1331 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
40775
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"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" |
|
66597
078e5c7b3159
Merged revisions 86925 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
43684
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 |
|
70769
8625ce7da152
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66597
diff
changeset
|
40 saved_lineno = lexer.lineno |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 toplevel = tt = lexer.get_token() |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
13587
diff
changeset
|
42 if not tt: |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 break |
|
66597
078e5c7b3159
Merged revisions 86925 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
43684
diff
changeset
|
44 elif tt[0] == '#': |
|
70769
8625ce7da152
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66597
diff
changeset
|
45 if lexer.lineno == saved_lineno and len(tt) == 1: |
|
8625ce7da152
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66597
diff
changeset
|
46 lexer.instream.readline() |
|
8625ce7da152
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66597
diff
changeset
|
47 continue |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 elif tt == 'machine': |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 entryname = lexer.get_token() |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 elif tt == 'default': |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 entryname = 'default' |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15655
diff
changeset
|
52 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
|
53 entryname = lexer.get_token() |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 self.macros[entryname] = [] |
|
22437
d2e457c44bf2
[Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents:
22362
diff
changeset
|
55 lexer.whitespace = ' \t' |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 while 1: |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 line = lexer.instream.readline() |
|
22437
d2e457c44bf2
[Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents:
22362
diff
changeset
|
58 if not line or line == '\012': |
|
d2e457c44bf2
[Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents:
22362
diff
changeset
|
59 lexer.whitespace = ' \t\r\n' |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 break |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 self.macros[entryname].append(line) |
|
22437
d2e457c44bf2
[Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents:
22362
diff
changeset
|
62 continue |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 else: |
|
16987
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
64 raise NetrcParseError( |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
65 "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
|
66 |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 # 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
|
68 login = '' |
|
c34cf9ce952f
SF bug 557704: netrc module can't handle all passwords
Raymond Hettinger <python@rcn.com>
parents:
28743
diff
changeset
|
69 account = password = None |
|
22437
d2e457c44bf2
[Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents:
22362
diff
changeset
|
70 self.hosts[entryname] = {} |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 while 1: |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 tt = lexer.get_token() |
|
70769
8625ce7da152
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66597
diff
changeset
|
73 if (tt.startswith('#') or |
|
8625ce7da152
fix regression in netrc comment handling (closes #12009)
Benjamin Peterson <benjamin@python.org>
parents:
66597
diff
changeset
|
74 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
|
75 if password: |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 self.hosts[entryname] = (login, account, password) |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 lexer.push_token(tt) |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 break |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 else: |
|
16987
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
80 raise NetrcParseError( |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
81 "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
|
82 % (toplevel, entryname, repr(tt)), |
|
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
83 file, lexer.lineno) |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 elif tt == 'login' or tt == 'user': |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 login = lexer.get_token() |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 elif tt == 'account': |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 account = lexer.get_token() |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 elif tt == 'password': |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 password = lexer.get_token() |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 else: |
|
16987
61c9fa94ce9d
Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents:
16460
diff
changeset
|
91 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
|
92 file, lexer.lineno) |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 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
|
95 """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
|
96 if host in self.hosts: |
|
9689
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 return self.hosts[host] |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22437
diff
changeset
|
98 elif 'default' 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['default'] |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 else: |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 return None |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 def __repr__(self): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
10823
diff
changeset
|
104 """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
|
105 rep = "" |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 for host in self.hosts.keys(): |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 attrs = self.hosts[host] |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 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
|
109 if attrs[1]: |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 rep = rep + "account " + repr(attrs[1]) |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 rep = rep + "\tpassword " + repr(attrs[2]) + "\n" |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 for macro in self.macros.keys(): |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 rep = rep + "macdef " + macro + "\n" |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 for line in self.macros[macro]: |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 rep = rep + line |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 rep = rep + "\n" |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 return rep |
|
0a0434efebcf
Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15655
diff
changeset
|
119 if __name__ == '__main__': |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
28803
diff
changeset
|
120 print(netrc()) |
