Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / python / fakepwd.py
1 # -*- test-case-name: twisted.python.test.test_fakepwd -*-
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
4
5 """
6 L{twisted.python.fakepwd} provides a fake implementation of the L{pwd} API.
7 """
8
9
10 __all__ = ['UserDatabase', 'ShadowDatabase']
11
12
13 class _UserRecord(object):
14     """
15     L{_UserRecord} holds the user data for a single user in L{UserDatabase}.
16     It corresponds to L{pwd.struct_passwd}.  See that class for attribute
17     documentation.
18     """
19     def __init__(self, name, password, uid, gid, gecos, home, shell):
20         self.pw_name = name
21         self.pw_passwd = password
22         self.pw_uid = uid
23         self.pw_gid = gid
24         self.pw_gecos = gecos
25         self.pw_dir = home
26         self.pw_shell = shell
27
28
29     def __len__(self):
30         return 7
31
32
33     def __getitem__(self, index):
34         return (
35             self.pw_name, self.pw_passwd, self.pw_uid,
36             self.pw_gid, self.pw_gecos, self.pw_dir, self.pw_shell)[index]
37
38
39
40 class UserDatabase(object):
41     """
42     L{UserDatabase} holds a traditional POSIX user data in memory and makes it
43     available via the same API as L{pwd}.
44
45     @ivar _users: A C{list} of L{_UserRecord} instances holding all user data
46         added to this database.
47     """
48     def __init__(self):
49         self._users = []
50
51
52     def addUser(self, username, password, uid, gid, gecos, home, shell):
53         """
54         Add a new user record to this database.
55
56         @param username: The value for the C{pw_name} field of the user
57             record to add.
58         @type username: C{str}
59
60         @param password: The value for the C{pw_passwd} field of the user
61             record to add.
62         @type password: C{str}
63
64         @param uid: The value for the C{pw_uid} field of the user record to
65             add.
66         @type uid: C{int}
67
68         @param gid: The value for the C{pw_gid} field of the user record to
69             add.
70         @type gid: C{int}
71
72         @param gecos: The value for the C{pw_gecos} field of the user record
73             to add.
74         @type gecos: C{str}
75
76         @param home: The value for the C{pw_dir} field of the user record to
77             add.
78         @type home: C{str}
79
80         @param shell: The value for the C{pw_shell} field of the user record to
81             add.
82         @type shell: C{str}
83         """
84         self._users.append(_UserRecord(
85             username, password, uid, gid, gecos, home, shell))
86
87
88     def getpwuid(self, uid):
89         """
90         Return the user record corresponding to the given uid.
91         """
92         for entry in self._users:
93             if entry.pw_uid == uid:
94                 return entry
95         raise KeyError()
96
97
98     def getpwnam(self, name):
99         """
100         Return the user record corresponding to the given username.
101         """
102         for entry in self._users:
103             if entry.pw_name == name:
104                 return entry
105         raise KeyError()
106
107
108     def getpwall(self):
109         """
110         Return a list of all user records.
111         """
112         return self._users
113
114
115
116 class _ShadowRecord(object):
117     """
118     L{_ShadowRecord} holds the shadow user data for a single user in
119     L{ShadowDatabase}.  It corresponds to C{spwd.struct_spwd}.  See that class
120     for attribute documentation.
121     """
122     def __init__(self, username, password, lastChange, min, max, warn, inact,
123                  expire, flag):
124         self.sp_nam = username
125         self.sp_pwd = password
126         self.sp_lstchg = lastChange
127         self.sp_min = min
128         self.sp_max = max
129         self.sp_warn = warn
130         self.sp_inact = inact
131         self.sp_expire = expire
132         self.sp_flag = flag
133
134
135     def __len__(self):
136         return 9
137
138
139     def __getitem__(self, index):
140         return (
141             self.sp_nam, self.sp_pwd, self.sp_lstchg, self.sp_min,
142             self.sp_max, self.sp_warn, self.sp_inact, self.sp_expire,
143             self.sp_flag)[index]
144
145
146
147 class ShadowDatabase(object):
148     """
149     L{ShadowDatabase} holds a shadow user database in memory and makes it
150     available via the same API as C{spwd}.
151
152     @ivar _users: A C{list} of L{_ShadowRecord} instances holding all user data
153         added to this database.
154
155     @since: 12.0
156     """
157     def __init__(self):
158         self._users = []
159
160
161     def addUser(self, username, password, lastChange, min, max, warn, inact,
162                 expire, flag):
163         """
164         Add a new user record to this database.
165
166         @param username: The value for the C{sp_nam} field of the user record to
167             add.
168         @type username: C{str}
169
170         @param password: The value for the C{sp_pwd} field of the user record to
171             add.
172         @type password: C{str}
173
174         @param lastChange: The value for the C{sp_lstchg} field of the user
175             record to add.
176         @type lastChange: C{int}
177
178         @param min: The value for the C{sp_min} field of the user record to add.
179         @type min: C{int}
180
181         @param max: The value for the C{sp_max} field of the user record to add.
182         @type max: C{int}
183
184         @param warn: The value for the C{sp_warn} field of the user record to
185             add.
186         @type warn: C{int}
187
188         @param inact: The value for the C{sp_inact} field of the user record to
189             add.
190         @type inact: C{int}
191
192         @param expire: The value for the C{sp_expire} field of the user record
193             to add.
194         @type expire: C{int}
195
196         @param flag: The value for the C{sp_flag} field of the user record to
197             add.
198         @type flag: C{int}
199         """
200         self._users.append(_ShadowRecord(
201                 username, password, lastChange,
202                 min, max, warn, inact, expire, flag))
203
204
205     def getspnam(self, username):
206         """
207         Return the shadow user record corresponding to the given username.
208         """
209         for entry in self._users:
210             if entry.sp_nam == username:
211                 return entry
212         raise KeyError
213
214
215     def getspall(self):
216         """
217         Return a list of all shadow user records.
218         """
219         return self._users