summaryrefslogtreecommitdiffstats
path: root/build_tools/larch8/larch0/cli/userinfo.py
diff options
context:
space:
mode:
authorJames Meyer <james.meyer@operamail.com>2010-12-02 22:37:23 (GMT)
committerJames Meyer <james.meyer@operamail.com>2010-12-02 22:37:34 (GMT)
commit8b94d7f39c71234712bead363526a0283efeb9fa (patch)
tree23f1dbd6458dc39a2c1b08bcdd4cbf768a60d84d /build_tools/larch8/larch0/cli/userinfo.py
parent338af567e74d08cbd357079941208e494463d61e (diff)
downloadlinhes_dev-8b94d7f39c71234712bead363526a0283efeb9fa.zip
larch8: first checkin, still needs some work
Diffstat (limited to 'build_tools/larch8/larch0/cli/userinfo.py')
-rw-r--r--build_tools/larch8/larch0/cli/userinfo.py93
1 files changed, 93 insertions, 0 deletions
diff --git a/build_tools/larch8/larch0/cli/userinfo.py b/build_tools/larch8/larch0/cli/userinfo.py
new file mode 100644
index 0000000..f2045aa
--- /dev/null
+++ b/build_tools/larch8/larch0/cli/userinfo.py
@@ -0,0 +1,93 @@
+# userinfo.py
+#
+# (c) Copyright 2009-2010 Michael Towers (larch42 at googlemail dot com)
+#
+# This file is part of the larch project.
+#
+# larch is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# larch is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with larch; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+#
+#----------------------------------------------------------------------------
+# 2010.07.11
+
+import os
+from ConfigParser import SafeConfigParser
+
+# Default list of 'additional' groups for a new user
+BASEGROUPS = 'video,audio,optical,storage,scanner,power,camera'
+
+class Userinfo:
+ def __init__(self, profile):
+ self.profile_dir = profile
+
+ def getusers(self):
+ """Read user information by means of a SafeConfigParser instance.
+ This is then available as self.userconf.
+ """
+ self.userconf = SafeConfigParser({'pw':'', 'maingroup':'', 'uid':'',
+ 'skel':'', 'xgroups':BASEGROUPS, 'expert':''})
+ users = self.profile_dir + '/users'
+ if os.path.isfile(users):
+ try:
+ self.userconf.read(users)
+ except:
+ error0(_("Invalid 'users' file"))
+
+ def allusers(self):
+ self.getusers()
+ return self.userconf.sections()
+
+ def get(self, user, field):
+ return self.userconf.get(user, field)
+
+ def userinfo(self, user, fields):
+ """Get an ordered list of the given field data for the given user.
+ """
+ return [self.userconf.get(user, f) for f in fields]
+
+ def userset(self, uname, field, text):
+ self.userconf.set(uname, field, text)
+
+ def newuser(self, user):
+ try:
+ self.userconf.add_section(user)
+ return self.saveusers()
+ except:
+ error0(_("Couldn't add user '%s'") % user)
+ return False
+
+ def deluser(self, user):
+ try:
+ self.userconf.remove_section(user)
+ return self.saveusers()
+ except:
+ error0(_("Couldn't remove user '%s'") % user)
+ return False
+
+ def saveusers(self):
+ """Save the user configuration data (in 'INI' format)
+ """
+ try:
+ fh = None
+ fh = open(self.profile_dir + '/users', 'w')
+ self.userconf.write(fh)
+ fh.close()
+ return True
+ except:
+ if fh:
+ fh.close()
+ error0(_("Couldn't save 'users' file"))
+ self.getusers()
+ return False
+