summaryrefslogtreecommitdiff
path: root/configshell/prefs.py
blob: 9ddb420760afe7c25955496b6c1af20d305b5b25 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'''
This file is part of ConfigShell Community Edition.
Copyright (c) 2011 by RisingTide Systems LLC

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3 (AGPLv3).

This program 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

import cPickle

class Prefs(object):
    '''
    This is a preferences backend object used to:
        - Hold the ConfigShell preferences
        - Handle persistent storage and retrieval of these preferences
        - Share the preferences between the ConfigShell and ConfigNode objects

    As it is inherently destined to be shared between objects, this is a Borg.
    '''
    _prefs = {}
    filename = None
    autosave = False
    __borg_state = {}

    def __init__(self, filename=None):
        '''
        Instanciates the ConfigShell preferences object.
        @param filename: File to store the preferencces to.
        @type filename: str
        '''
        self.__dict__ = self.__borg_state
        if filename is not None:
            self.filename = filename

    def __getitem__(self, key):
        '''
        Proxies dict-like references to prefs.
        One specific behavior, though, is that if the key does not exists,
        we will return None instead of raising an exception.
        @param key: The preferences dictionnary key to get.
        @type key: any valid dict key
        @return: The key value
        @rtype: n/a
        '''
        if key in self._prefs:
            return self._prefs[key]
        else:
            return None

    def __setitem__(self, key, value):
        '''
        Proxies dict-like references to prefs.
        @param key: The preferences dictionnary key to set.
        @type key: any valid dict key
        '''
        self._prefs[key] = value
        if self.autosave:
            self.save()

    def __contains__(self, key):
        '''
        Do the preferences contain key ?
        @param key: The preferences dictionnary key to check.
        @type key: any valid dict key
        '''
        if key in self._prefs:
            return True
        else:
            return False

    def __delitem__(self, key):
        '''
        Deletes a preference key.
        @param key: The preference to delete.
        @type key: any valid dict key
        '''
        del self._prefs[key]
        if self.autosave:
            self.save()

    def __iter__(self):
        '''
        Generic iterator for the preferences.
        '''
        return self._prefs.__iter__()

    # Public methods

    def keys(self):
        '''
        @return: Returns the list of keys in preferences.
        @rtype: list
        '''
        return self._prefs.keys()

    def items(self):
        '''
        @return: Returns the list of items in preferences.
        @rtype: list of (key, value) tuples
        '''
        return self._prefs.items()

    def iteritems(self):
        '''
        @return: Iterates on the items in preferences.
        @rtype: yields items that are (key, value) pairs
        '''
        return self._prefs.iteritems()

    def save(self, filename=None):
        '''
        Saves the preferences to disk. If filename is not specified,
        use the default one if it is set, else do nothing.
        @param filename: Optional alternate file to use.
        @type filename: str
        '''
        if filename is None:
            filename = self.filename

        if filename is not None:
            fsock = open(filename, 'wb')
            try:
                cPickle.dump(self._prefs, fsock, 2)
            finally:
                fsock.close()

    def load(self, filename=None):
        '''
        Loads the preferences from file. Use either the supplied filename,
        or the default one if set. Else, do nothing.
        '''
        if filename is None:
            filename = self.filename

        if filename is not None:
            fsock = open(filename, 'rb')
            try:
                self._prefs = cPickle.load(fsock)
            finally:
                fsock.close()