1
# -*- coding: utf-8 -*-
3
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
4
#Copyright (c) 2005 Ali Afshar aafshar@gmail.com
6
#Permission is hereby granted, free of charge, to any person obtaining a copy
7
#of this software and associated documentation files (the "Software"), to deal
8
#in the Software without restriction, including without limitation the rights
9
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
#copies of the Software, and to permit persons to whom the Software is
11
#furnished to do so, subject to the following conditions:
13
#The above copyright notice and this permission notice shall be included in
14
#all copies or substantial portions of the Software.
16
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
import ConfigParser as configparser
33
class registry_item(base.pidacomponent):
34
"""A single item in the registry."""
36
def init(self, name, doc, default):
39
doc = doc.replace('\n', ' ')
42
self.__default = default
45
self.value = self.__default
47
def validate(self, value):
50
def unserialize(self, data):
54
return '%s' % self.value
58
value = self.unserialize(data)
60
# Any unserialisation error is a failure
65
except errors.BadRegistryValueError:
69
def set_value(self, value):
70
if self.validate(value):
73
raise errors.BadRegistryValueError, value
78
value = property(get_value, set_value)
82
name = property(get_name)
87
def set_doc(self, value):
90
doc = property(get_doc, set_doc)
92
def get_default(self):
95
default = property(get_default)
98
return ('Registry Value typ=%s name=%s value=%s default=%s '
99
'doc=%s' % (self.__class__.__name__, self.__name,
100
self.__value, self.__default, self.doc))
102
class registry_group(base.pidagroup):
103
"""A registry group."""
105
group_type = lambda *a: registry_group(*a)
106
def __create_component(self, name, doc, default, typ):
107
return typ(name, doc, default)
108
component_type = __create_component
110
def init(self, name, doc):
111
base.pidagroup.init(self, name)
116
doc = property(get_doc)
119
class registry(base.pidamanager):
120
"""The pida registry."""
122
group_type = registry_group
124
file_intro = '# pida generated ini file'
126
def __get_tempopts(self, filename):
127
self.filename = filename
128
tempopts = configparser.ConfigParser()
129
if os.path.exists(self.filename):
130
f = open(self.filename, 'r')
135
def load(self, filename):
136
tempopts = self.__get_tempopts(filename)
137
for group, option in self.iter_items():
138
if tempopts.has_option(group.name, option.name):
139
data = tempopts.get(group.name, option.name)
140
if not option.load(data):
146
f = open(self.filename, 'w')
147
f.write(self.file_intro)
148
for group in self.iter_groups():
149
f.write('\n[%s]\n' % group.name)
151
f.write('# %s\n' % option.name)
152
f.write('# %s\n' % option.doc)
153
f.write('# default value = %s\n' % option.default)
154
f.write('%s = %s\n\n' % (option.name, option.value))
159
"""Types used by the registry."""
161
class string(registry_item):
162
"""A plain string."""
164
class directory(registry_item):
165
def validate(self, value):
166
return os.path.isdir(value)
168
class directorycreating(directory):
169
def validate(self, value):
170
if Directory.validate(self, value):
174
return directory.validate(self, value)
176
class file(registry_item):
179
class filemustexist(file):
181
def validate(self, value):
182
return os.path.exists(value)
184
class filewhich(file):
186
def setdefault(self):
187
import distutils.spawn as spawn
188
path = spawn.find_executable(self.default)
194
class font(registry_item):
197
class boolean(registry_item):
198
def unserialize(self, data):
202
val = int(data[0].lower() in ['t'])
204
raise errors.BadRegistryDataError
207
class integer(registry_item):
208
def unserialize(self, data):
212
raise errors.BadRegistryDataError
215
class list(registry_item):
220
choices = ['vim', 'culebra', 'emacs', 'scerpent']
222
class color(registry_item):
225
class fileembedded(registry_item):
228
class password(registry_item):
229
"""A password item."""
232
def intrange(lower, upper, step):
233
# can't use class call
234
classdict = {'lower': lower,
237
return type('intrange', (types.integer,), classdict)
240
def stringlist(*args):
241
classdict = {'choices': args}
242
return type('stringlist', (types.list,), classdict)
244
CONFIG_FILE_INTRO = (
245
'#This is an automatically generated Pida config file.\n'
246
'#Please edit it, your changes will be preserved (if valid).\n'
247
'#If you want a fresh config file, delete it.\n\n'
249
'#Boolean values are 1 or 0\n'
250
'#Blank lines are ignored as are lines beginning #\n'