[Python-checkins] python/dist/src/Lib/plat-os2emx grp.py,1.1,1.2 pwd.py,1.1,1.2

aimacintyre@users.sourceforge.net aimacintyre@users.sourceforge.net
Thu, 10 Jul 2003 05:53:06 -0700


Update of /cvsroot/python/python/dist/src/Lib/plat-os2emx
In directory sc8-pr-cvs1:/tmp/cvs-serv6682

Modified Files:
	grp.py pwd.py 
Log Message:
Extend the pwd & grp emulations to support accessing the pwd/grp
record tuple by name as well as index, to match the behaviour of
the pwd/grp extension modules for Unix.  These emulation modules
now pass test_pwd & test_grp.


Index: grp.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/plat-os2emx/grp.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** grp.py	22 Feb 2002 11:06:30 -0000	1.1
--- grp.py	10 Jul 2003 12:52:52 -0000	1.2
***************
*** 3,7 ****
  
  # written by Andrew MacIntyre, April 2001.
! # released into the public domain "as is", with NO WARRANTY
  
  # note that this implementation checks whether ":" or ";" as used as 
--- 3,7 ----
  
  # written by Andrew MacIntyre, April 2001.
! # updated July 2003, adding field accessor support
  
  # note that this implementation checks whether ":" or ";" as used as 
***************
*** 97,100 ****
--- 97,134 ----
          raise KeyError, '>> group database fields not delimited <<'
  
+ # class to match the new record field name accessors.
+ # the resulting object is intended to behave like a read-only tuple,
+ # with each member also accessible by a field name.
+ class Group:
+     def __init__(self, name, passwd, gid, mem):
+         self.__dict__['gr_name'] = name
+         self.__dict__['gr_passwd'] = passwd
+         self.__dict__['gr_gid'] = gid
+         self.__dict__['gr_mem'] = mem
+         self.__dict__['_record'] = (self.gr_name, self.gr_passwd,
+                                     self.gr_gid, self.gr_mem)
+ 
+     def __len__(self):
+         return 4
+ 
+     def __getitem__(self, key):
+         return self._record[key]
+ 
+     def __setattr__(self, name, value):
+         raise AttributeError('attribute read-only: %s' % name)
+ 
+     def __repr__(self):
+         return str(self._record)
+ 
+     def __cmp__(self, other):
+         this = str(self._record)
+         if this == other:
+             return 0
+         elif this < other:
+             return -1
+         else:
+             return 1
+ 
+ 
  # read the whole file, parsing each entry into tuple form
  # with dictionaries to speed recall by GID or group name
***************
*** 114,118 ****
              fields = entry.split(sep)
              fields[2] = int(fields[2])
!             record = tuple(fields)
              if not gidx.has_key(fields[2]):
                  gidx[fields[2]] = record
--- 148,153 ----
              fields = entry.split(sep)
              fields[2] = int(fields[2])
!             fields[3] = [f.strip() for f in fields[3].split(',')]
!             record = Group(*fields)
              if not gidx.has_key(fields[2]):
                  gidx[fields[2]] = record

Index: pwd.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/plat-os2emx/pwd.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** pwd.py	22 Feb 2002 11:06:30 -0000	1.1
--- pwd.py	10 Jul 2003 12:52:54 -0000	1.2
***************
*** 3,7 ****
  
  # written by Andrew MacIntyre, April 2001.
! # released into the public domain "as is", with NO WARRANTY
  
  # note that this implementation checks whether ":" or ";" as used as 
--- 3,7 ----
  
  # written by Andrew MacIntyre, April 2001.
! # updated July 2003, adding field accessor support
  
  # note that this implementation checks whether ":" or ";" as used as 
***************
*** 116,119 ****
--- 116,158 ----
          raise KeyError, '>> passwd database fields not delimited <<'
  
+ # class to match the new record field name accessors.
+ # the resulting object is intended to behave like a read-only tuple,
+ # with each member also accessible by a field name.
+ class Passwd:
+     def __init__(self, name, passwd, uid, gid, gecos, dir, shell):
+         self.__dict__['pw_name'] = name
+         self.__dict__['pw_passwd'] = passwd
+         self.__dict__['pw_uid'] = uid
+         self.__dict__['pw_gid'] = gid
+         self.__dict__['pw_gecos'] = gecos
+         self.__dict__['pw_dir'] = dir
+         self.__dict__['pw_shell'] = shell
+         self.__dict__['_record'] = (self.pw_name, self.pw_passwd,
+                                     self.pw_uid, self.pw_gid,
+                                     self.pw_gecos, self.pw_dir,
+                                     self.pw_shell)
+ 
+     def __len__(self):
+         return 7
+ 
+     def __getitem__(self, key):
+         return self._record[key]
+ 
+     def __setattr__(self, name, value):
+         raise AttributeError('attribute read-only: %s' % name)
+ 
+     def __repr__(self):
+         return str(self._record)
+ 
+     def __cmp__(self, other):
+         this = str(self._record)
+         if this == other:
+             return 0
+         elif this < other:
+             return -1
+         else:
+             return 1
+ 
+ 
  # read the whole file, parsing each entry into tuple form
  # with dictionaries to speed recall by UID or passwd name
***************
*** 136,140 ****
              for i in (5, 6):
                  fields[i] = __field_sep[sep](fields[i])
!             record = tuple(fields)
              if not uidx.has_key(fields[2]):
                  uidx[fields[2]] = record
--- 175,179 ----
              for i in (5, 6):
                  fields[i] = __field_sep[sep](fields[i])
!             record = Passwd(*fields)
              if not uidx.has_key(fields[2]):
                  uidx[fields[2]] = record