Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions
netrc.netrc Class Reference

List of all members.

Public Member Functions

def __init__
def authenticators
def __repr__

Public Attributes

 hosts
 macros

Private Member Functions

def _parse

Detailed Description

Definition at line 22 of file netrc.py.


Constructor & Destructor Documentation

def netrc.netrc.__init__ (   self,
  file = None 
)

Definition at line 23 of file netrc.py.

00023 
00024     def __init__(self, file=None):
00025         if file is None:
00026             try:
00027                 file = os.path.join(os.environ['HOME'], ".netrc")
00028             except KeyError:
00029                 raise IOError("Could not find .netrc: $HOME is not set")
00030         self.hosts = {}
00031         self.macros = {}
00032         with open(file) as fp:
00033             self._parse(file, fp)

Here is the caller graph for this function:


Member Function Documentation

def netrc.netrc.__repr__ (   self)
Dump the class data in the format of a .netrc file.

Definition at line 103 of file netrc.py.

00103 
00104     def __repr__(self):
00105         """Dump the class data in the format of a .netrc file."""
00106         rep = ""
00107         for host in self.hosts.keys():
00108             attrs = self.hosts[host]
00109             rep = rep + "machine "+ host + "\n\tlogin " + repr(attrs[0]) + "\n"
00110             if attrs[1]:
00111                 rep = rep + "account " + repr(attrs[1])
00112             rep = rep + "\tpassword " + repr(attrs[2]) + "\n"
00113         for macro in self.macros.keys():
00114             rep = rep + "macdef " + macro + "\n"
00115             for line in self.macros[macro]:
00116                 rep = rep + line
00117             rep = rep + "\n"
00118         return rep

def netrc.netrc._parse (   self,
  file,
  fp 
) [private]

Definition at line 34 of file netrc.py.

00034 
00035     def _parse(self, file, fp):
00036         lexer = shlex.shlex(fp)
00037         lexer.wordchars += r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
00038         lexer.commenters = lexer.commenters.replace('#', '')
00039         while 1:
00040             # Look for a machine, default, or macdef top-level keyword
00041             saved_lineno = lexer.lineno
00042             toplevel = tt = lexer.get_token()
00043             if not tt:
00044                 break
00045             elif tt[0] == '#':
00046                 if lexer.lineno == saved_lineno and len(tt) == 1:
00047                     lexer.instream.readline()
00048                 continue
00049             elif tt == 'machine':
00050                 entryname = lexer.get_token()
00051             elif tt == 'default':
00052                 entryname = 'default'
00053             elif tt == 'macdef':                # Just skip to end of macdefs
00054                 entryname = lexer.get_token()
00055                 self.macros[entryname] = []
00056                 lexer.whitespace = ' \t'
00057                 while 1:
00058                     line = lexer.instream.readline()
00059                     if not line or line == '\012':
00060                         lexer.whitespace = ' \t\r\n'
00061                         break
00062                     self.macros[entryname].append(line)
00063                 continue
00064             else:
00065                 raise NetrcParseError(
00066                     "bad toplevel token %r" % tt, file, lexer.lineno)
00067 
00068             # We're looking at start of an entry for a named machine or default.
00069             login = ''
00070             account = password = None
00071             self.hosts[entryname] = {}
00072             while 1:
00073                 tt = lexer.get_token()
00074                 if (tt.startswith('#') or
00075                     tt in {'', 'machine', 'default', 'macdef'}):
00076                     if password:
00077                         self.hosts[entryname] = (login, account, password)
00078                         lexer.push_token(tt)
00079                         break
00080                     else:
00081                         raise NetrcParseError(
00082                             "malformed %s entry %s terminated by %s"
00083                             % (toplevel, entryname, repr(tt)),
00084                             file, lexer.lineno)
00085                 elif tt == 'login' or tt == 'user':
00086                     login = lexer.get_token()
00087                 elif tt == 'account':
00088                     account = lexer.get_token()
00089                 elif tt == 'password':
00090                     password = lexer.get_token()
00091                 else:
00092                     raise NetrcParseError("bad follower token %r" % tt,
00093                                           file, lexer.lineno)

Here is the call graph for this function:

def netrc.netrc.authenticators (   self,
  host 
)
Return a (user, account, password) tuple for given host.

Definition at line 94 of file netrc.py.

00094 
00095     def authenticators(self, host):
00096         """Return a (user, account, password) tuple for given host."""
00097         if host in self.hosts:
00098             return self.hosts[host]
00099         elif 'default' in self.hosts:
00100             return self.hosts['default']
00101         else:
00102             return None


Member Data Documentation

Definition at line 29 of file netrc.py.

Definition at line 30 of file netrc.py.


The documentation for this class was generated from the following file: