/usr/share/pyshared/coherence/extern/config.py is in python-coherence 0.6.6.2-8.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | #
# Licensed under the MIT license
# http://opensource.org/licenses/mit-license.php
# Copyright 2007,2008 Frank Scholz <coherence@beebits.net>
from coherence.extern.et import ET, indent
class ConfigMixin(object):
def nodes_to_dict(self,node):
if node.tag.endswith('list'):
return ConfigList(node)
else:
return ConfigDict(node)
class ConfigList(list,ConfigMixin):
def __init__(self,node):
self.name = node.tag
list.__init__(self)
self.from_element(node)
def to_element(self):
root = ET.Element(self.name)
for v in self:
if isinstance(v, (dict,list)):
root.append(v.to_element())
return root
def from_element(self,node):
for n in node:
if n.get('active','yes') == 'yes':
if len(n) == 0:
a = {}
for attr,value in n.items():
if attr == 'active':
continue
a[attr] = value
if len(a):
self.append(a)
else:
self.append(self.nodes_to_dict(n))
class ConfigDict(dict,ConfigMixin):
def __init__(self,node):
self.name = node.tag
dict.__init__(self)
self.from_element(node)
def to_element(self):
root = ET.Element(self.name)
for key, value in self.items():
if isinstance(value, (dict,list)):
root.append(value.to_element())
else:
s = ET.SubElement(root,key)
if isinstance(value, basestring):
s.text = value
else:
s.text = str(value)
return root
def from_element(self, node):
for attr,value in node.items():
if attr == 'active':
continue
self[attr] = value
for n in node:
if n.get('active','yes') == 'yes':
if len(n) == 0:
if n.text is not None and len(n.text)>0:
self[n.get('name',n.tag)] = n.text
for attr,value in n.items():
if attr == 'active':
continue
self[attr] = value
else:
tag = n.tag
#if tag.endswith('list'):
# tag = tag[:-4]
self[n.get('name',tag)] = self.nodes_to_dict(n)
#def __setitem__(self, key, value):
# self.config[key] = value
#def __getitem__(self, key):
# """ fetch an item """
# value = self.config.get(key, None)
# return value
#def __delitem__(self, key):
# del self.config[key]
#def get(self, key, default=None):
# try:
# return self[key]
# except KeyError:
# return default
#def items(self):
# """ """
# return self.config.items()
#def keys(self):
# """ """
# return self.config.keys()
#def values(self):
# """ """
# return self.config.values()
#def __repr__(self):
# return "%r" % self.config
class Config(ConfigDict):
"""
an incomplete XML file to dict and vice versa mapper
- nodes with an attribute 'active' set to 'no' are ignored
and not transferred into the dict
- nodes with tags ending with 'list' are transferrend into
an item with the key = 'tag' and a list with the subnodes
as the value
at the moment we parse the xml file and create dicts or lists out
of the nodes, but maybe it is much easier to keep the xml structure
as it is and simulate the dict/list access behavior on it?
"""
def __init__(self, file):
self.file = file
dict.__init__(self)
try:
xml = ET.parse(file)
except SyntaxError, msg:
raise SyntaxError, msg
except IOError, msg:
raise IOError, msg
except Exception, msg:
raise SyntaxError, msg
xmlroot = xml.getroot()
self.name = xmlroot.tag
self.from_element(xmlroot)
def save(self,file=None):
if file == None:
file = self.file
e = ET.Element(self.name)
for key, value in self.items():
if isinstance(value, (dict,list)):
e.append(value.to_element())
else:
s = ET.SubElement(e,key)
if isinstance(value, basestring):
s.text = value
else:
s.text = str(value)
indent(e)
db = ET.ElementTree(e)
db.write(file, encoding='utf-8')
if __name__ == '__main__':
import sys
config = Config(sys.argv[1])
print config
config['serverport'] = 55555
config['test'] = 'test'
config['logging']['level'] = 'info'
del config['controlpoint']
#del config['logging']['level']
print config
config.save('/tmp/t')
|