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
150
151
152
|
'''
This file is part of ConfigShell.
Copyright (c) 2011-2013 by Datera, Inc
Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.
'''
import six
import fcntl
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 six.iteritems(self._prefs)
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')
fcntl.lockf(fsock, fcntl.LOCK_UN)
try:
six.moves.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')
fcntl.lockf(fsock, fcntl.LOCK_SH)
try:
self._prefs = six.moves.cPickle.load(fsock)
finally:
fsock.close()
|