[Patches] Proposed changes to netrc.py

lannert@uni-duesseldorf.de lannert@uni-duesseldorf.de
Wed, 5 Apr 100 18:47:19 +0200 (MEST)


--ELM954953239-23322-0_
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

This is a -c diff (and the complete source, and the disclaimer) for the
netrc.py module to make it work with macdefs. (See discussion on
python-bugs list.) The original author, Eric S. Raymond, hasn't yet
approved of this change.

  Detlef

--ELM954953239-23322-0_
Content-Type: text/plain; charset=ISO-8859-1
Content-Disposition: attachment; filename=netrc.diff
Content-Description: /tmp/netrc.diff
Content-Transfer-Encoding: 7bit

*** /sw/lang/python/Python-1.6a1/Lib/netrc.py	Fri Feb  4 16:10:33 2000
--- netrc.py	Wed Apr  5 13:39:23 2000
***************
*** 1,68 ****
  """An object-oriented interface to .netrc files."""
  
  # Module and documentation by Eric S. Raymond, 21 Dec 1998 
  
  import os, shlex
  
  class netrc:
      def __init__(self, file=None):
          if not file:
              file = os.path.join(os.environ['HOME'], ".netrc")
!         try:
!             fp = open(file)
!         except:
!             return None
          self.hosts = {}
!         self.macros = {}
          lexer = shlex.shlex(fp)
!         lexer.wordchars = lexer.wordchars + '.'
          while 1:
!             # Look for a machine, default, or macdef top-level keyword
!             toplevel = tt = lexer.get_token()
!             if tt == '' or tt == None:
                  break
!             elif tt == 'machine':
                  entryname = lexer.get_token()
!             elif tt == 'default':
                  entryname = 'default'
!             elif tt == 'macdef':		# Just skip to end of macdefs
!                 entryname = lexer.get_token()
!                 self.macros[entryname] = []
!                 lexer.whitepace = ' \t'
!                 while 1:
!                     line = lexer.instream.readline()
!                     if not line or line == '\012' and tt == '\012':
!                         lexer.whitepace = ' \t\r\n'
!                         break
!                     tt = line
!                     self.macros[entryname].append(line)
              else:
                  raise SyntaxError, "bad toplevel token %s, file %s, line %d" \
!             				% (tt, file, lexer.lineno) 
  
              # We're looking at start of an entry for a named machine or default.
!             if toplevel == 'machine':
!                 login = account = password = None
!                 self.hosts[entryname] = {}
              while 1:
!                 tt = lexer.get_token()
!                 if tt=='' or tt == 'machine' or tt == 'default' or tt == 'macdef':
!                     if toplevel == 'macdef':
!                         break;
!                     elif login and password:
                          self.hosts[entryname] = (login, account, password)
!                         lexer.push_token(tt)
!                         break
!                     else:
!                         raise SyntaxError, "malformed %s entry %s terminated by %s" % (toplevel, entryname, repr(tt))
!                 elif tt == 'login' or tt == 'user':
                      login = lexer.get_token()
!                 elif tt == 'account':
                      account = lexer.get_token()
!                 elif tt == 'password':
                      password = lexer.get_token()
                  else:
!                     raise SyntaxError, "bad follower token %s, file %s, line %d"%(tt,file,lexer.lineno)
  
      def authenticators(self, host):
          """Return a (user, account, password) tuple for given host."""
--- 1,73 ----
  """An object-oriented interface to .netrc files."""
  
  # Module and documentation by Eric S. Raymond, 21 Dec 1998 
+ # Recklessly hacked and macdefs fixed by Detlef Lannert, 05 Apr 2000
  
  import os, shlex
+ import string  # not from Python 1.6 onwards
  
  class netrc:
      def __init__(self, file=None):
          if not file:
              file = os.path.join(os.environ['HOME'], ".netrc")
!         fp = open(file)
          self.hosts = {}
!         self.macros = {}  # indexed by hostnames
          lexer = shlex.shlex(fp)
!         lexer.wordchars = lexer.wordchars + '.-'
          while 1:
!             # Look for a machine or default top-level keyword
!             nexttoken = lexer.get_token()
!             if nexttoken in ('', None):
                  break
!             elif nexttoken == 'machine':
                  entryname = lexer.get_token()
!             elif nexttoken == 'default':
                  entryname = 'default'
!             elif nexttoken == 'macdef':
!                 # this is a toplevel macdef; what the heck is it good for??
!                 entryname = ''  # put it into self.macros['']
!                 lexer.push_token(nexttoken)
              else:
                  raise SyntaxError, "bad toplevel token %s, file %s, line %d" \
!             				% (nexttoken, file, lexer.lineno) 
  
              # We're looking at start of an entry for a named machine or default.
!             login = account = password = None
!             macdefs = {}
              while 1:
!                 nexttoken = lexer.get_token()
!                 if nexttoken in ('machine', 'default', ''):
!                     if (login and not password) or (password and not login):
!                         # macdef-only entries are acceptable!
!                         raise SyntaxError(
!                                 "incomplete %s entry terminated by %s"
!                                 % (`entryname`, nexttoken or "EOF"))
!                     if login:
                          self.hosts[entryname] = (login, account, password)
!                     if macdefs:
!                         self.macros[entryname] = macdefs
!                     lexer.push_token(nexttoken)
!                     break
!                 elif nexttoken in ('login', 'user'):
                      login = lexer.get_token()
!                 elif nexttoken == 'account':
                      account = lexer.get_token()
!                 elif nexttoken == 'password':
                      password = lexer.get_token()
+                 elif nexttoken == 'macdef':
+                     macroname = lexer.get_token()
+                     macro = []
+                     while 1:  # macro continues until empty line
+                         line = lexer.instream.readline()
+                         if not line or line == "\n":
+                             break
+                         macro.append(line)
+                     macdefs[macroname] = macro
                  else:
!                     raise SyntaxError(
!                             "bad follower token %s, file %s, line %d"
!                             % (nexttoken, file, lexer.lineno))
  
      def authenticators(self, host):
          """Return a (user, account, password) tuple for given host."""
***************
*** 72,94 ****
              return self.hosts['default']
          else:
              return None
! 
      def __repr__(self):
          """Dump the class data in the format of a .netrc file."""
!         rep = ""
          for host in self.hosts.keys():
!             attrs = self.hosts[host]
!             rep = rep + "machine "+ host + "\n\tlogin " + repr(attrs[0]) + "\n"
!             if attrs[1]:
!                 rep = rep + "account " + repr(attrs[1])
!             rep = rep + "\tpassword " + repr(attrs[2]) + "\n"
!         for macro in self.macros.keys():
!             rep = rep + "macdef " + macro + "\n"
!             for line in self.macros[macro]:
!                 rep = rep + line
!             rep = rep + "\n"
!         return rep
! 
  if __name__ == '__main__': 
      print netrc()
- 
--- 77,121 ----
              return self.hosts['default']
          else:
              return None
!     
      def __repr__(self):
          """Dump the class data in the format of a .netrc file."""
!         result = []
!         
!         # First process the mysterious top-level macdef's:
!         host = ""  # dummy entry
!         for macroname in self.macros.get(host, {}).keys():
!             result.append("\tmacdef %s\n" % macroname)
!             result.extend(self.macros[host][macroname])
!             result.append("\n")
!         
!         # Now for the machines (and the optional default entry):
          for host in self.hosts.keys():
!             login, account, password = self.hosts[host]
!             result.append("machine %s \n\tlogin %s\n" % (host, login))
!             if account:
!                 result.append("\taccount %s\n" % account)
!             result.append("\tpassword %s\n" % password)
!             for macroname in self.macros.get(host, {}).keys():
!                 result.append("\tmacdef %s\n" % macroname)
!                 result.extend(self.macros[host][macroname])
!                 result.append("\n")
!         
!         # That's it ...
!         return string.join(result, "")
!         #return "".join(result)  # Python 1.6?
! 
! def test():
!     import sys
!     if len(sys.argv) > 1:
!       file = sys.argv[1]
!     else:
!       file = ""
!     n = netrc(file)
!     print "hosts:", `n.hosts`
!     print "macros:", `n.macros`
!     print n
!     
  if __name__ == '__main__': 
+     #test()
      print netrc()

--ELM954953239-23322-0_
Content-Type: text/plain; charset=ISO-8859-1
Content-Disposition: attachment; filename=netrc.py
Content-Description: /tmp/netrc.py
Content-Transfer-Encoding: 7bit

"""An object-oriented interface to .netrc files."""

# Module and documentation by Eric S. Raymond, 21 Dec 1998 
# Recklessly hacked and macdefs fixed by Detlef Lannert, 05 Apr 2000

import os, shlex
import string  # not from Python 1.6 onwards

class netrc:
    def __init__(self, file=None):
        if not file:
            file = os.path.join(os.environ['HOME'], ".netrc")
        fp = open(file)
        self.hosts = {}
        self.macros = {}  # indexed by hostnames
        lexer = shlex.shlex(fp)
        lexer.wordchars = lexer.wordchars + '.-'
        while 1:
            # Look for a machine or default top-level keyword
            nexttoken = lexer.get_token()
            if nexttoken in ('', None):
                break
            elif nexttoken == 'machine':
                entryname = lexer.get_token()
            elif nexttoken == 'default':
                entryname = 'default'
            elif nexttoken == 'macdef':
                # this is a toplevel macdef; what the heck is it good for??
                entryname = ''  # put it into self.macros['']
                lexer.push_token(nexttoken)
            else:
                raise SyntaxError, "bad toplevel token %s, file %s, line %d" \
            				% (nexttoken, file, lexer.lineno) 

            # We're looking at start of an entry for a named machine or default.
            login = account = password = None
            macdefs = {}
            while 1:
                nexttoken = lexer.get_token()
                if nexttoken in ('machine', 'default', ''):
                    if (login and not password) or (password and not login):
                        # macdef-only entries are acceptable!
                        raise SyntaxError(
                                "incomplete %s entry terminated by %s"
                                % (`entryname`, nexttoken or "EOF"))
                    if login:
                        self.hosts[entryname] = (login, account, password)
                    if macdefs:
                        self.macros[entryname] = macdefs
                    lexer.push_token(nexttoken)
                    break
                elif nexttoken in ('login', 'user'):
                    login = lexer.get_token()
                elif nexttoken == 'account':
                    account = lexer.get_token()
                elif nexttoken == 'password':
                    password = lexer.get_token()
                elif nexttoken == 'macdef':
                    macroname = lexer.get_token()
                    macro = []
                    while 1:  # macro continues until empty line
                        line = lexer.instream.readline()
                        if not line or line == "\n":
                            break
                        macro.append(line)
                    macdefs[macroname] = macro
                else:
                    raise SyntaxError(
                            "bad follower token %s, file %s, line %d"
                            % (nexttoken, file, lexer.lineno))

    def authenticators(self, host):
        """Return a (user, account, password) tuple for given host."""
        if self.hosts.has_key(host):
            return self.hosts[host]
        elif self.hosts.has_key('default'):
            return self.hosts['default']
        else:
            return None
    
    def __repr__(self):
        """Dump the class data in the format of a .netrc file."""
        result = []
        
        # First process the mysterious top-level macdef's:
        host = ""  # dummy entry
        for macroname in self.macros.get(host, {}).keys():
            result.append("\tmacdef %s\n" % macroname)
            result.extend(self.macros[host][macroname])
            result.append("\n")
        
        # Now for the machines (and the optional default entry):
        for host in self.hosts.keys():
            login, account, password = self.hosts[host]
            result.append("machine %s \n\tlogin %s\n" % (host, login))
            if account:
                result.append("\taccount %s\n" % account)
            result.append("\tpassword %s\n" % password)
            for macroname in self.macros.get(host, {}).keys():
                result.append("\tmacdef %s\n" % macroname)
                result.extend(self.macros[host][macroname])
                result.append("\n")
        
        # That's it ...
        return string.join(result, "")
        #return "".join(result)  # Python 1.6?

def test():
    import sys
    if len(sys.argv) > 1:
      file = sys.argv[1]
    else:
      file = ""
    n = netrc(file)
    print "hosts:", `n.hosts`
    print "macros:", `n.macros`
    print n
    
if __name__ == '__main__': 
    #test()
    print netrc()

--ELM954953239-23322-0_
Content-Type: text/plain; charset=ISO-8859-1
Content-Disposition: attachment; filename=disclaimer.txt
Content-Description: /sw/lang/python/disclaimer.txt
Content-Transfer-Encoding: 7bit

I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims").  To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.

I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation.  I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.

--ELM954953239-23322-0_--