My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 1 attachment: libconfig.py (3.8 KB)

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
import pylibconfig as pylib
from os import path

class Group(object):
_base = ''
_config = None
_autoSave = False
_filepath = None

def __init__(self, config, group_name, filepath, auto_save):
self._filepath = filepath
self._autoSave = auto_save
self._base = group_name
self._config = config

def _check(self, key):
newkey = '.'.join([self._base, key]) if len(self._base) > 0 else key
(val, status) = self._config.value(newkey)
exists = self._config.exists(newkey)
return (newkey, val, status, exists)

def add_group(self, group_name):
(newkey, val, status, exists) = self._check(group_name)
if not status and exists:
# Group already exists.
return True
elif status and exists:
# Can't. Value with that name already exists.
return False
else:
# We're good.
self._config.addGroup(self._base, group_name)
if self._autoSave:
self._config.writeFile(self._filepath)
return True

def __getattr__(self, key):
if self.__dict__.has_key(key):
return self.__dict__[key]
(newkey, val, status, exists) = self._check(key)
if not status and exists:
self.__dict__[key] = Group(self._config, newkey, self._filepath, self._autoSave)
return self.__dict__[key]
elif status and exists:
self.__dict__[key] = val
return self.__dict__[key]
else:
return None

def __setattr__(self, key, value):
if self._config is None:
super(Group, self).__setattr__(key, value)
return
(newkey, val, status, exists) = self._check(key)
config = self._config
valtype = type(value)
if not status and exists:
config.remove(self._base, key)
self.__setattr__(key, value)
elif status and exists:
config.setValue(newkey, value)
self.__dict__[key] = value
else:
if valtype == dict:
config.addGroup(self._base, key)
self.__dict__[key] = Group(self._config, newkey, self._filepath, self._autoSave)
for k in value.keys():
self.__dict__[key].__setattr__(k, value[k])
elif valtype == list or valtype == set:
config.addList(self._base, key)
value = list(value)
self.__dict__[key] = value
for v in value:
config.appendToList(newkey, str(v))
else:
if valtype == str:
config.addString(self._base, key)
elif valtype == bool:
config.addBoolean(self._base, key)
elif valtype == float:
config.addFloat(self._base, key)
elif valtype == int or valtype == long:
config.addInteger(self._base, key)
#elif valtype == tuple:
# config.addArray(self._base, key)
else:
raise Exception('Could not detect a valid type to use.')
self._config.setValue(newkey, value)
self.__dict__[key] = value
if self._autoSave:
self._config.writeFile(self._filepath)

def __delattr__(self, key):
if self._config is None:
return
(newkey, val, status, exists) = self._check(key)
if exists:
self._config.remove(self._base, key)
if self._autoSave:
self._config.writeFile(self._filepath)
if self.__dict__.has_key(key):
del self.__dict__[key]

class Config(Group):

def __init__(self, filepath, autoSave=False):
self._autoSave = autoSave
self._filepath = filepath
self._config = pylib.Config()
self._config.setAutoConvert(True)
self.reload()

def reload(self):
if not path.exists(self._filepath):
return
self._config.readFile(self._filepath)

def save(self):
return self._config.writeFile(self._filepath)

config = Config('abc.cfg', True)
config.test_list = [0, 'a', 2, 3.0, 4, 5L]
# Currently these two wont work.
#config.test_array = (0, 1, 2, 3, 4,)
#config.useStuff = True
config.name = __file__
config.add_group('aliases')
config.aliases.out = 'echo!'
config.aliases.pop = 'popd'
config.cmds = {
'abc' : 1,
'abc2' : 2.0,
'abc3' : 3L,
}
config.remove_me = 'Hello World'
del config.remove_me
Powered by Google Project Hosting