-
Notifications
You must be signed in to change notification settings - Fork 3
/
libSettingsfile.py
109 lines (92 loc) · 3.69 KB
/
libSettingsfile.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# This class manages settings which are stored in an xml file. The
# settings can autmatically be written back as soon as changed.
import lxml
from lxml import etree
import os
class SettingsFile :
def __init__(self, filename, rootTag=None, writeback=False, forceTag=False) :
"""writeback immeadiately writes changes"""
self.writeback = writeback
self.filename = os.path.abspath(filename)
if rootTag == None :
rootTag = etree.Element('xml')
if not isinstance(rootTag, lxml.etree._Element) :
raise Exception('rootTag element %s is not of type %s!' % (rootTag, lxml.etree._Element))
self.exists = False
# Check whether the info file is already existing.
# If yes, read it, if not, create one in memory.
if os.path.exists(self.filename) :
try :
self.doc = etree.parse(filename).getroot()
if forceTag and (not rootTag.tag == self.doc.tag) :
None
else :
print("Read settings: " + self.filename)
self.exists = True
except lxml.etree.XMLSyntaxError:
print("Error reading settings. " + self.filename)
None
if not self.exists :
self.doc = rootTag
def write(self) :
"""Write the map info to disk. Not necessary if writeback is
enabled."""
file = open(self.filename, 'w')
file.write(etree.tostring(self.doc, encoding='utf-8', pretty_print=True))
file.close()
def node(self, tag) :
return self.doc.find('.//' + tag)
def text(self, tag, default=None) :
"""Returns the tag's text node («value»)"""
if default is None :
# Set default value
# See http://docs.python.org/tutorial/controlflow.html#default-argument-values
default = ''
try :
value = self.doc.find('.//%s' % (tag)).text
except AttributeError :
value = default
return value
def setText(self, tag='unknown', value='none') :
"""Sets text node («value»). Adds text node if necessary.
Returns true if tag already contained value."""
equal = False
converted = False
# Check encoding
try :
# Assuming value is an utf-8 encoded byte sequence it should
# be possible to decode the object to an abstract unicode
# object. If it fails, value was not utf-8 encoded and
# needs to be decoded (to standard python string format)
# from another encoding.
value.decode('utf-8')
except UnicodeDecodeError :
# This should not happen here!
value = value.decode('cp1252')
print('Converted value: >' + value + "<")
converted = True
except AttributeError :
value = str(value) # Not an object of type str
# Set/update node value
node = self.doc.find('.//' + tag)
if node is None :
node = etree.SubElement(self.doc, tag)
if node.text == value :
equal = True
else :
node.text = value
if self.writeback :
self.write()
return equal
def removeTag(self, tag) :
"""Returns True if tag has been removed."""
removed = False
node = self.node(tag)
if node is not None :
node.getparent().remove(node)
removed = True
return removed
def empty(self, tag) :
return (self.text(tag) == '')