annotate Lib/netrc.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents c34cf9ce952f
children e5c4eb6b8e05 e6e3a0e91bea 4cdf21a0dbbc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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")
13587
7c127a79a369 Apply Greg Kochanski's fix for open/265.
Eric S. Raymond <esr@thyrsus.com>
parents: 13586
diff changeset
29 fp = open(file)
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 self.hosts = {}
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 self.macros = {}
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 lexer = shlex.shlex(fp)
28743
6e2e5985375c SF 557704: netrc module can't handle all passwords
Raymond Hettinger <python@rcn.com>
parents: 23622
diff changeset
33 lexer.wordchars += r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 while 1:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 # 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
36 toplevel = tt = lexer.get_token()
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 13587
diff changeset
37 if not tt:
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 break
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 elif tt == 'machine':
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 entryname = lexer.get_token()
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 elif tt == 'default':
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 entryname = 'default'
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15655
diff changeset
43 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
44 entryname = lexer.get_token()
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 self.macros[entryname] = []
22437
d2e457c44bf2 [Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents: 22362
diff changeset
46 lexer.whitespace = ' \t'
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 while 1:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 line = lexer.instream.readline()
22437
d2e457c44bf2 [Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents: 22362
diff changeset
49 if not line or line == '\012':
d2e457c44bf2 [Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents: 22362
diff changeset
50 lexer.whitespace = ' \t\r\n'
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 break
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 self.macros[entryname].append(line)
22437
d2e457c44bf2 [Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents: 22362
diff changeset
53 continue
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 else:
16987
61c9fa94ce9d Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents: 16460
diff changeset
55 raise NetrcParseError(
61c9fa94ce9d Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents: 16460
diff changeset
56 "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
57
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 # 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
59 login = ''
c34cf9ce952f SF bug 557704: netrc module can't handle all passwords
Raymond Hettinger <python@rcn.com>
parents: 28743
diff changeset
60 account = password = None
22437
d2e457c44bf2 [Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents: 22362
diff changeset
61 self.hosts[entryname] = {}
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 while 1:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 tt = lexer.get_token()
22437
d2e457c44bf2 [Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents: 22362
diff changeset
64 if (tt=='' or tt == 'machine' or
d2e457c44bf2 [Bug #532115] netrc module was broken
Andrew M. Kuchling <amk@amk.ca>
parents: 22362
diff changeset
65 tt == 'default' or tt =='macdef'):
28803
c34cf9ce952f SF bug 557704: netrc module can't handle all passwords
Raymond Hettinger <python@rcn.com>
parents: 28743
diff changeset
66 if password:
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 self.hosts[entryname] = (login, account, password)
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 lexer.push_token(tt)
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 break
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 else:
16987
61c9fa94ce9d Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents: 16460
diff changeset
71 raise NetrcParseError(
61c9fa94ce9d Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents: 16460
diff changeset
72 "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
73 % (toplevel, entryname, repr(tt)),
61c9fa94ce9d Define & use NetrcParseError instead of improperly overloading SyntaxError.
Fred Drake <fdrake@acm.org>
parents: 16460
diff changeset
74 file, lexer.lineno)
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 elif tt == 'login' or tt == 'user':
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 login = lexer.get_token()
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 elif tt == 'account':
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 account = lexer.get_token()
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 elif tt == 'password':
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 password = lexer.get_token()
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("bad follower token %r" % 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
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 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
86 """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
87 if host in self.hosts:
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 return self.hosts[host]
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22437
diff changeset
89 elif 'default' in self.hosts:
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 return self.hosts['default']
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 else:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 return None
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 __repr__(self):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10823
diff changeset
95 """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
96 rep = ""
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 for host in self.hosts.keys():
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 attrs = self.hosts[host]
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 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
100 if attrs[1]:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 rep = rep + "account " + repr(attrs[1])
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 rep = rep + "\tpassword " + repr(attrs[2]) + "\n"
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 for macro in self.macros.keys():
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 rep = rep + "macdef " + macro + "\n"
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 for line in self.macros[macro]:
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 rep = rep + line
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 rep = rep + "\n"
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 return rep
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15655
diff changeset
110 if __name__ == '__main__':
9689
0a0434efebcf Adding shlex and netrc by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 print netrc()