Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 71d74e8

Browse files
author
Andrew MacIntyre
committed
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.
1 parent 28df64a commit 71d74e8

2 files changed

Lines changed: 78 additions & 4 deletions

File tree

Lib/plat-os2emx/grp.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# extension module.
33

44
# written by Andrew MacIntyre, April 2001.
5-
# released into the public domain "as is", with NO WARRANTY
5+
# updated July 2003, adding field accessor support
66

77
# note that this implementation checks whether ":" or ";" as used as
88
# the field separator character.
@@ -96,6 +96,40 @@ def __get_field_sep(record):
9696
else:
9797
raise KeyError, '>> group database fields not delimited <<'
9898

99+
# class to match the new record field name accessors.
100+
# the resulting object is intended to behave like a read-only tuple,
101+
# with each member also accessible by a field name.
102+
class Group:
103+
def __init__(self, name, passwd, gid, mem):
104+
self.__dict__['gr_name'] = name
105+
self.__dict__['gr_passwd'] = passwd
106+
self.__dict__['gr_gid'] = gid
107+
self.__dict__['gr_mem'] = mem
108+
self.__dict__['_record'] = (self.gr_name, self.gr_passwd,
109+
self.gr_gid, self.gr_mem)
110+
111+
def __len__(self):
112+
return 4
113+
114+
def __getitem__(self, key):
115+
return self._record[key]
116+
117+
def __setattr__(self, name, value):
118+
raise AttributeError('attribute read-only: %s' % name)
119+
120+
def __repr__(self):
121+
return str(self._record)
122+
123+
def __cmp__(self, other):
124+
this = str(self._record)
125+
if this == other:
126+
return 0
127+
elif this < other:
128+
return -1
129+
else:
130+
return 1
131+
132+
99133
# read the whole file, parsing each entry into tuple form
100134
# with dictionaries to speed recall by GID or group name
101135
def __read_group_file():
@@ -113,7 +147,8 @@ def __read_group_file():
113147
sep = __get_field_sep(entry)
114148
fields = entry.split(sep)
115149
fields[2] = int(fields[2])
116-
record = tuple(fields)
150+
fields[3] = [f.strip() for f in fields[3].split(',')]
151+
record = Group(*fields)
117152
if not gidx.has_key(fields[2]):
118153
gidx[fields[2]] = record
119154
if not namx.has_key(fields[0]):

Lib/plat-os2emx/pwd.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# extension module.
33

44
# written by Andrew MacIntyre, April 2001.
5-
# released into the public domain "as is", with NO WARRANTY
5+
# updated July 2003, adding field accessor support
66

77
# note that this implementation checks whether ":" or ";" as used as
88
# the field separator character. Path conversions are are applied when
@@ -115,6 +115,45 @@ def __get_field_sep(record):
115115
else:
116116
raise KeyError, '>> passwd database fields not delimited <<'
117117

118+
# class to match the new record field name accessors.
119+
# the resulting object is intended to behave like a read-only tuple,
120+
# with each member also accessible by a field name.
121+
class Passwd:
122+
def __init__(self, name, passwd, uid, gid, gecos, dir, shell):
123+
self.__dict__['pw_name'] = name
124+
self.__dict__['pw_passwd'] = passwd
125+
self.__dict__['pw_uid'] = uid
126+
self.__dict__['pw_gid'] = gid
127+
self.__dict__['pw_gecos'] = gecos
128+
self.__dict__['pw_dir'] = dir
129+
self.__dict__['pw_shell'] = shell
130+
self.__dict__['_record'] = (self.pw_name, self.pw_passwd,
131+
self.pw_uid, self.pw_gid,
132+
self.pw_gecos, self.pw_dir,
133+
self.pw_shell)
134+
135+
def __len__(self):
136+
return 7
137+
138+
def __getitem__(self, key):
139+
return self._record[key]
140+
141+
def __setattr__(self, name, value):
142+
raise AttributeError('attribute read-only: %s' % name)
143+
144+
def __repr__(self):
145+
return str(self._record)
146+
147+
def __cmp__(self, other):
148+
this = str(self._record)
149+
if this == other:
150+
return 0
151+
elif this < other:
152+
return -1
153+
else:
154+
return 1
155+
156+
118157
# read the whole file, parsing each entry into tuple form
119158
# with dictionaries to speed recall by UID or passwd name
120159
def __read_passwd_file():
@@ -135,7 +174,7 @@ def __read_passwd_file():
135174
fields[i] = int(fields[i])
136175
for i in (5, 6):
137176
fields[i] = __field_sep[sep](fields[i])
138-
record = tuple(fields)
177+
record = Passwd(*fields)
139178
if not uidx.has_key(fields[2]):
140179
uidx[fields[2]] = record
141180
if not namx.has_key(fields[0]):

0 commit comments

Comments
 (0)