annotate engine/python/fife/extensions/serializers/simplexml.py @ 568:bfbf329e1da8

Forgot to add the simplexml.py file in my last commit.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 28 Jun 2010 18:43:03 +0000
parents
children 466d76db9701
rev   line source
568
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
1 # -*- coding: utf-8 -*-
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
2
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
3 # ####################################################################
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
4 # Copyright (C) 2005-2010 by the FIFE team
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
5 # http://www.fifengine.net
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
6 # This file is part of FIFE.
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
7 #
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
8 # FIFE is free software; you can redistribute it and/or
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
9 # modify it under the terms of the GNU Lesser General Public
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
10 # License as published by the Free Software Foundation; either
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
11 # version 2.1 of the License, or (at your option) any later version.
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
12 #
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
13 # This library is distributed in the hope that it will be useful,
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
16 # Lesser General Public License for more details.
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
17 #
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
18 # You should have received a copy of the GNU Lesser General Public
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
19 # License along with this library; if not, write to the
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
20 # Free Software Foundation, Inc.,
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
22 # ####################################################################
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
23
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
24 import os
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
25 from StringIO import StringIO
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
26
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
27 try:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
28 import xml.etree.cElementTree as ET
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
29 except:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
30 import xml.etree.ElementTree as ET
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
31
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
32
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
33 EMPTY_XML_FILE="""\
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
34 <?xml version='1.0' encoding='UTF-8'?>
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
35 <Settings>
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
36
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
37 </Settings>
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
38 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
39
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
40 class SimpleXMLSerializer(object):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
41 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
42 This class is a simple interface to get and store data in XML files.
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
43
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
44 Usage::
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
45 from fife.extensions.serializers.simplexml import SimpleXMLSerializer
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
46 serializer = SimpleXMLSerializer(filename="somefile.xml")
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
47 settings.set("module_name", "variable_name", "value")
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
48 somevariable = settings.get("module_name", "variable_name", "default_value")
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
49 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
50 def __init__(self, filename=None):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
51 self._file = filename
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
52 self._tree = None
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
53 self._root_element = None
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
54
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
55 if self._file:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
56 self.load(self._file)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
57
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
58 def load(self, filename=None):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
59 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
60 Loads the XML file into memory and validates it.
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
61
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
62 @note: If the file does not exist it will automatically create a blank file for you.
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
63 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
64 if filename:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
65 self._file = filename
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
66
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
67 if not self._file:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
68 print "Cannot load file. No filename specified!"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
69 return
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
70
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
71 if not os.path.exists(self._file):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
72 self._tree = ET.parse(StringIO(EMPTY_XML_FILE))
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
73 self._tree.write(self._file, 'UTF-8')
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
74 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
75 self._tree = ET.parse(self._file)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
76
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
77 self._root_element = self._tree.getroot()
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
78 self._validateTree()
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
79
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
80 def save(self, filename=None):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
81 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
82 Saves the XML file.
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
83
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
84 @note: This Overwrites the file if it exists.
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
85 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
86 if filename:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
87 savefile = filename
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
88 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
89 savefile = self._file
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
90
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
91 if not savefile:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
92 print "Cannot save file. No filename specified!"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
93 return
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
94
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
95 """ Writes the settings to file """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
96 self._indent(self._root_element)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
97 self._tree.write(savefile, 'UTF-8')
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
98
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
99
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
100 def get(self, module, name, defaultValue=None):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
101 """ Gets the value of a specified variable
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
102
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
103 @param module: Name of the module to get the variable from
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
104 @param name: Variable name
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
105 @param defaultValue: Specifies the default value to return if the variable is not found
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
106 @type defaultValue: C{str} or C{unicode} or C{int} or C{float} or C{bool} or C{list} or C{dict}
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
107 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
108 if not isinstance(name, str) and not isinstance(name, unicode):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
109 raise AttributeError("SimpleXMLSerializer.get(): Invalid type for name argument.")
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
110
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
111 moduleTree = self._getModuleTree(module)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
112 element = None
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
113 for e in moduleTree.getchildren():
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
114 if e.tag == "Setting" and e.get("name", "") == name:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
115 element = e
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
116 break
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
117 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
118 return defaultValue
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
119
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
120 e_value = element.text
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
121 e_strip = element.get("strip", "1").strip().lower()
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
122 e_type = str(element.get("type", "str")).strip()
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
123
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
124 if e_value is None:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
125 return defaultValue
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
126
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
127 # Strip value
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
128 if e_strip == "" or e_strip == "false" or e_strip == "no" or e_strip == "0":
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
129 e_strip = False
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
130 else: e_strip = True
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
131
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
132 if e_type == "str" or e_type == "unicode":
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
133 if e_strip: e_value = e_value.strip()
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
134 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
135 e_value = e_value.strip()
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
136
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
137 # Return value
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
138 if e_type == 'int':
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
139 return int(e_value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
140 elif e_type == 'float':
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
141 return float(e_value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
142 elif e_type == 'bool':
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
143 e_value = e_value.lower()
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
144 if e_value == "" or e_value == "false" or e_value == "no" or e_value == "0":
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
145 return False
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
146 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
147 return True
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
148 elif e_type == 'str':
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
149 return str(e_value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
150 elif e_type == 'unicode':
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
151 return unicode(e_value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
152 elif e_type == 'list':
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
153 return self._deserializeList(e_value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
154 elif e_type == 'dict':
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
155 return self._deserializeDict(e_value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
156
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
157 def set(self, module, name, value, extra_attrs={}):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
158 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
159 Sets a variable to specified value.
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
160
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
161 @param module: Module where the variable should be set
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
162 @param name: Name of the variable
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
163 @param value: Value to assign to the variable
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
164 @type value: C{str} or C{unicode} or C{int} or C{float} or C{bool} or C{list} or C{dict}
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
165 @param extra_attrs: Extra attributes to be stored in the XML-file
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
166 @type extra_attrs: C{dict}
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
167 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
168 if not isinstance(name, str) and not isinstance(name, unicode):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
169 raise AttributeError("Settings:set: Invalid type for name argument.")
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
170
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
171 moduleTree = self._getModuleTree(module)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
172 e_type = "str"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
173
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
174 if isinstance(value, bool): # This must be before int
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
175 e_type = "bool"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
176 value = str(value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
177 elif isinstance(value, int):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
178 e_type = "int"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
179 value = str(value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
180 elif isinstance(value, float):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
181 e_type = "float"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
182 value = str(value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
183 elif isinstance(value, unicode):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
184 e_type = "unicode"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
185 value = unicode(value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
186 elif isinstance(value, list):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
187 e_type = "list"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
188 value = self._serializeList(value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
189 elif isinstance(value, dict):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
190 e_type = "dict"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
191 value = self._serializeDict(value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
192 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
193 e_type = "str"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
194 value = str(value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
195
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
196 for e in moduleTree.getchildren():
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
197 if e.tag != "Setting": continue
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
198 if e.get("name", "") == name:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
199 e.text = value
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
200 break
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
201 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
202 attrs = {"name":name, "type":e_type}
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
203 for k in extra_attrs:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
204 if k not in attrs:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
205 attrs[k] = extra_args[k]
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
206 elm = ET.SubElement(moduleTree, "Setting", attrs)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
207 elm.text = value
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
208
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
209 def _validateTree(self):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
210 """ Iterates the XML tree and prints warning when an invalid tag is found """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
211 for c in self._root_element.getchildren():
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
212 if c.tag != "Module":
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
213 print "Invalid tag in " + self._file + ". Expected Module, got: ", c.tag
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
214 elif c.get("name", "") == "":
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
215 print "Invalid tag in " + self._file + ". Module name is empty."
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
216 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
217 for e in c.getchildren():
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
218 if e.tag != "Setting":
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
219 print "Invalid tag in " + self._file + " in module: ",c.tag,
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
220 print ". Expected Setting, got: ", e.tag
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
221 elif c.get("name", "") == "":
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
222 print "Invalid tag in " + self._file + " in module: ",c.tag,
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
223 print ". Setting name is empty", e.tag
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
224
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
225 def _getModuleTree(self, module):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
226 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
227 Returns a module element from the XML tree. If no module with the specified
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
228 name exists, a new element will be created.
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
229
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
230 @param module: The module to get from the settings tree
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
231 @type module: C{string}
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
232 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
233 if not isinstance(module, str) and not isinstance(module, unicode):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
234 raise AttributeError("Settings:_getModuleTree: Invalid type for module argument.")
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
235
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
236 for c in self._root_element.getchildren():
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
237 if c.tag == "Module" and c.get("name", "") == module:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
238 return c
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
239
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
240 # Create module
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
241 return ET.SubElement(self._root_element, "Module", {"name":module})
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
242
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
243 def _indent(self, elem, level=0):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
244 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
245 Adds whitespace, so the resulting XML-file is properly indented.
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
246 Shamelessly stolen from http://effbot.org/zone/element-lib.htm
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
247 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
248 i = "\n" + level*" "
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
249 if len(elem):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
250 if not elem.text or not elem.text.strip():
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
251 elem.text = i + " "
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
252 if not elem.tail or not elem.tail.strip():
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
253 elem.tail = i
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
254 for elem in elem:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
255 self._indent(elem, level+1)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
256 if not elem.tail or not elem.tail.strip():
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
257 elem.tail = i
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
258 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
259 if level and (not elem.tail or not elem.tail.strip()):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
260 elem.tail = i
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
261
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
262 # FIXME:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
263 # These serialization functions are not reliable at all
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
264 # This will only serialize the first level of a dict or list
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
265 # It will not check the types nor the content for conflicts.
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
266 # Perhaps we should add a small serialization library?
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
267 def _serializeList(self, list):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
268 """ Serializes a list, so it can be stored in a text file """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
269 return " ; ".join(list)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
270
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
271 def _deserializeList(self, string):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
272 """ Deserializes a list back into a list object """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
273 return string.split(" ; ")
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
274
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
275 def _serializeDict(self, dict):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
276 """ Serializes a list, so it can be stored in a text file """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
277 serial = ""
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
278 for key in dict:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
279 value = dict[key]
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
280 if serial != "": serial += " ; "
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
281 serial += str(key)+" : "+str(value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
282
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
283 return serial
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
284
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
285 def _deserializeDict(self, serial):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
286 """ Deserializes a list back into a dict object """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
287 dict = {}
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
288 items = serial.split(" ; ")
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
289 for i in items:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
290 kv_pair = i.split(" : ")
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
291 dict[kv_pair[0]] = kv_pair[1]
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
292 return dict
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
293