mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-25 19:44:13 +01:00
Added an annotated option to registry.close and a fullNames option (to be used later) to getValues.
This commit is contained in:
parent
0e1b6923f7
commit
19e9d86a7c
@ -35,6 +35,7 @@ import re
|
|||||||
import copy
|
import copy
|
||||||
import sets
|
import sets
|
||||||
import types
|
import types
|
||||||
|
import textwrap
|
||||||
|
|
||||||
import utils
|
import utils
|
||||||
|
|
||||||
@ -63,9 +64,17 @@ def open(filename):
|
|||||||
raise InvalidRegistryFile, 'Error unpacking line #%s' % (i+1)
|
raise InvalidRegistryFile, 'Error unpacking line #%s' % (i+1)
|
||||||
_cache[key.lower()] = value
|
_cache[key.lower()] = value
|
||||||
|
|
||||||
def close(registry, filename):
|
def close(registry, filename, annotated=True):
|
||||||
fd = file(filename, 'w')
|
fd = file(filename, 'w')
|
||||||
for (name, value) in registry.getValues(getChildren=True):
|
for (name, value) in registry.getValues(getChildren=True):
|
||||||
|
if annotated and hasattr(value, 'help'):
|
||||||
|
lines = textwrap.wrap(value.help)
|
||||||
|
for (i, line) in enumerate(lines):
|
||||||
|
lines[i] = '# %s\n' % line
|
||||||
|
lines.insert(0, '###\n')
|
||||||
|
lines.insert(0, '\n')
|
||||||
|
lines.append('###\n')
|
||||||
|
fd.writelines(lines)
|
||||||
fd.write('%s: %s\n' % (name, value))
|
fd.write('%s: %s\n' % (name, value))
|
||||||
fd.close()
|
fd.close()
|
||||||
|
|
||||||
@ -89,9 +98,6 @@ class Value(object):
|
|||||||
than 100 in this method."""
|
than 100 in this method."""
|
||||||
self.value = v
|
self.value = v
|
||||||
|
|
||||||
def reset(self):
|
|
||||||
self.setValue(self.default)
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return repr(self.value)
|
return repr(self.value)
|
||||||
|
|
||||||
@ -103,9 +109,9 @@ class Value(object):
|
|||||||
class Boolean(Value):
|
class Boolean(Value):
|
||||||
def set(self, s):
|
def set(self, s):
|
||||||
s = s.lower()
|
s = s.lower()
|
||||||
if s in ('true', 'on', 'enabled'):
|
if s in ('true', 'on', 'enable', 'enabled'):
|
||||||
value = True
|
value = True
|
||||||
elif s in ('false', 'off', 'disabled'):
|
elif s in ('false', 'off', 'disable', 'disabled'):
|
||||||
value = False
|
value = False
|
||||||
elif s == 'toggle':
|
elif s == 'toggle':
|
||||||
value = not self.value
|
value = not self.value
|
||||||
@ -132,7 +138,7 @@ class PositiveInteger(Value):
|
|||||||
|
|
||||||
def setValue(self, v):
|
def setValue(self, v):
|
||||||
if v <= 0:
|
if v <= 0:
|
||||||
raise ValueError, 'Value must be a positive integer.'
|
raise InvalidRegistryValue, 'Value must be a positive integer.'
|
||||||
self.value = v
|
self.value = v
|
||||||
|
|
||||||
class Float(Value):
|
class Float(Value):
|
||||||
@ -142,9 +148,17 @@ class Float(Value):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
raise InvalidRegistryValue, 'Value must be a float.'
|
raise InvalidRegistryValue, 'Value must be a float.'
|
||||||
|
|
||||||
|
def setValue(self, v):
|
||||||
|
try:
|
||||||
|
self.value = float(v)
|
||||||
|
except ValueError:
|
||||||
|
raise InvalidRegistryValue, 'Value must be a float.'
|
||||||
|
|
||||||
class String(Value):
|
class String(Value):
|
||||||
def set(self, s):
|
def set(self, s):
|
||||||
if not s or (s[0] not in '\'"' and s[-1] not in '\'"'):
|
if not s:
|
||||||
|
s = '""'
|
||||||
|
elif s[0] != s[-1] or s[0] not in '\'"':
|
||||||
s = repr(s)
|
s = repr(s)
|
||||||
try:
|
try:
|
||||||
v = utils.safeEval(s)
|
v = utils.safeEval(s)
|
||||||
@ -170,7 +184,7 @@ class StringSurroundedBySpaces(String):
|
|||||||
|
|
||||||
def setValue(self, v):
|
def setValue(self, v):
|
||||||
if v.lstrip() == v:
|
if v.lstrip() == v:
|
||||||
v= ' ' + self.value
|
v= ' ' + v
|
||||||
if v.rstrip() == v:
|
if v.rstrip() == v:
|
||||||
v += ' '
|
v += ' '
|
||||||
self.value = v
|
self.value = v
|
||||||
@ -244,20 +258,21 @@ class Group(object):
|
|||||||
if _cache and fullname in _cache:
|
if _cache and fullname in _cache:
|
||||||
group.set(_cache[fullname])
|
group.set(_cache[fullname])
|
||||||
|
|
||||||
def getValues(self, getChildren=False):
|
def getValues(self, getChildren=False, fullNames=True):
|
||||||
L = []
|
L = []
|
||||||
items = self.values.items()
|
items = self.values.items()
|
||||||
for (name, child) in self.children.items():
|
for (name, child) in self.children.items():
|
||||||
if hasattr(child, 'value'):
|
if hasattr(child, 'value'):
|
||||||
items.append((name, child))
|
items.append((name, child))
|
||||||
utils.sortBy(lambda (k, _): (k.lower(), len(k), k), items)
|
utils.sortBy(lambda (k, _): (k.lower(), len(k), k), items)
|
||||||
for (name, value) in items:
|
if fullNames:
|
||||||
L.append(('%s.%s' % (self.getName(), self.originals[name]), value))
|
for (name, value) in items:
|
||||||
|
L.append(('%s.%s'%(self.getName(),self.originals[name]),value))
|
||||||
if getChildren:
|
if getChildren:
|
||||||
items = self.children.items()
|
items = self.children.items()
|
||||||
utils.sortBy(lambda (k, _): (k.lower(), len(k), k), items)
|
utils.sortBy(lambda (k, _): (k.lower(), len(k), k), items)
|
||||||
for (_, child) in items:
|
for (_, child) in items:
|
||||||
L.extend(child.getValues(getChildren))
|
L.extend(child.getValues(getChildren, fullNames))
|
||||||
return L
|
return L
|
||||||
|
|
||||||
|
|
||||||
@ -273,9 +288,6 @@ class GroupWithValue(Group):
|
|||||||
def setValue(self, v):
|
def setValue(self, v):
|
||||||
self.value.setValue(v)
|
self.value.setValue(v)
|
||||||
|
|
||||||
def reset(self):
|
|
||||||
self.value.reset()
|
|
||||||
|
|
||||||
def __call__(self):
|
def __call__(self):
|
||||||
return self.value()
|
return self.value()
|
||||||
|
|
||||||
@ -289,14 +301,9 @@ class GroupWithDefault(GroupWithValue):
|
|||||||
def set(self, *args):
|
def set(self, *args):
|
||||||
self.__class__ = value.__class__
|
self.__class__ = value.__class__
|
||||||
self.set(*args)
|
self.set(*args)
|
||||||
|
|
||||||
def setValue(self, *args):
|
def setValue(self, *args):
|
||||||
self.__class__ = value.__class__
|
self.__class__ = value.__class__
|
||||||
self.setValue(*args)
|
self.setValue(*args)
|
||||||
|
|
||||||
def reset(self, *args):
|
|
||||||
self.__class__ = value.__class__
|
|
||||||
self.reset(*args)
|
|
||||||
self.X = X
|
self.X = X
|
||||||
GroupWithValue.__init__(self, value)
|
GroupWithValue.__init__(self, value)
|
||||||
|
|
||||||
@ -323,8 +330,8 @@ class GroupWithDefault(GroupWithValue):
|
|||||||
(_, group) = rsplit(k, '.', 1)
|
(_, group) = rsplit(k, '.', 1)
|
||||||
self.__makeChild(group, v)
|
self.__makeChild(group, v)
|
||||||
|
|
||||||
def getValues(self, getChildren=False):
|
def getValues(self, getChildren=False, fullNames=True):
|
||||||
L = GroupWithValue.getValues(self, getChildren)
|
L = GroupWithValue.getValues(self, getChildren, fullNames)
|
||||||
L = [v for v in L if v[1].__class__ is not self.X]
|
L = [v for v in L if v[1].__class__ is not self.X]
|
||||||
return L
|
return L
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user