import py import ConfigParser from uconf.__.user import User import uconf def getlist(userlistpath = None): if userlistpath is None: userlist = config.userlistpath userlistpath = py.path.local(userlistpath) assert userlistpath.check(file=1) parser = UserConfig(userlistpath) return parser.getuserlist() def readconfig(path): """ return list of User object from configuration file. (ini-format)""" path = py.path.local(path) assert path.check(file=1) parser = ConfigParser.ConfigParser() parser.read(str(path)) l = [UserFromConfig(parser, name) for name in parser.sections() if name != 'default'] return l class UserFromConfig(User): __attrs__ = ('name', 'email', 'realname', 'shell', 'hashspec', 'organisation', 'homedir', 'ssh_authorized_keys', 'groups') def __init__(self, parser, name): self.name = name self._parser = parser def __str__(self): return "User(%r)" % self.name __repr__ = __str__ def __getattr__(self, name): if name not in self.__attrs__: raise AttributeError, name try: return self._parser.get(self.name, name) except ConfigParser.NoSectionError: raise ValueError, "User not found: %s" % self.name except ConfigParser.NoOptionError: try: return self._parser.get('default', name) except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): return None def password(self): try: return self._password except AttributeError: if self.hashspec is not None: self._password = uconf.Password(hashspec=self.hashspec) else: self._password = None return self._password password=property(password) def _get_homedir(self): try: return py.path.local(self.__getattr__('home')) except AttributeError: return py.path.local('/home/%s' % self.name) homedir = property(_get_homedir) def _get_groups(self): try: return self._groups except AttributeError: groups = self.__getattr__('groups') groups = [x.strip() for x in groups.split(',')] self._groups = groups return groups groups = property(_get_groups) def _get_ssh_authorized_keys(self): try: key = self.__getattr__('ssh_authorized_keys') except AttributeError: return None else: keys = [x.strip() for x in key.split(',')] return keys ssh_authorized_keys = property(_get_ssh_authorized_keys) #def _getdict(self): # for name in self.__attrs__: # setattr(self, name, getattr(self, name)) # dictdescr = object.__dict__ # return dictdescr.__get__(self) #__dict__ = property(_getdict)