summaryrefslogtreecommitdiffstats
path: root/build_tools/larch8/larch0/cli/userinfo.py
blob: f2045aa8dc1adbce0f539c97cd05578b9eadfbf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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