Mercurial > fife-parpg
annotate engine/python/fife/extensions/serializers/simplexml.py @ 596:0bbe6e8ad9c8
- corrected example in docstring
- this fixes ticket:480
author | chewie@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Mon, 23 Aug 2010 17:44:45 +0000 |
parents | 466d76db9701 |
children | 867aad1c01cd |
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 |
569
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
26 |
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
27 from fife.extensions.serializers import ET, SerializerError, InvalidFormat, NotFound |
568
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
28 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
29 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
30 EMPTY_XML_FILE="""\ |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
31 <?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
|
32 <Settings> |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
33 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
34 </Settings> |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
35 """ |
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 class SimpleXMLSerializer(object): |
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 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
|
40 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
41 Usage:: |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
42 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
|
43 serializer = SimpleXMLSerializer(filename="somefile.xml") |
596
0bbe6e8ad9c8
- corrected example in docstring
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
569
diff
changeset
|
44 serializer.set("module_name", "variable_name", "value") |
0bbe6e8ad9c8
- corrected example in docstring
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
569
diff
changeset
|
45 somevariable = serializer.get("module_name", "variable_name", "default_value") |
568
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
46 """ |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
47 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
|
48 self._file = filename |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
49 self._tree = None |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
50 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
|
51 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
52 if self._file: |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
53 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
|
54 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
55 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
|
56 """ |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
57 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
|
58 |
569
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
59 Raises a SerializerError exception if the file is not specified. |
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
60 |
568
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
61 @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
|
62 """ |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
63 if filename: |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
64 self._file = filename |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
65 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
66 if not self._file: |
569
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
67 raise SerializerError("Cannot load file or create file. No filename specified!") |
568
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
68 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
69 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
|
70 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
|
71 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
|
72 else: |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
73 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
|
74 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
75 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
|
76 self._validateTree() |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
77 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
78 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
|
79 """ |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
80 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
|
81 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
82 @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
|
83 """ |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
84 if filename: |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
85 savefile = filename |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
86 else: |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
87 savefile = self._file |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
88 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
89 if not savefile: |
569
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
90 raise SerializerError("Cannot save file. No filename specified!") |
568
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
91 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
92 """ 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
|
93 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
|
94 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
|
95 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
96 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
97 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
|
98 """ 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
|
99 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
100 @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
|
101 @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
|
102 @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
|
103 @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
|
104 """ |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
105 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
|
106 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
|
107 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
108 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
|
109 element = None |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
110 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
|
111 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
|
112 element = e |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
113 break |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
114 else: |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
115 return defaultValue |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
116 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
117 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
|
118 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
|
119 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
|
120 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
121 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
|
122 return defaultValue |
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 # Strip value |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
125 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
|
126 e_strip = False |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
127 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
|
128 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
129 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
|
130 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
|
131 else: |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
132 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
|
133 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
134 # Return value |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
135 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
|
136 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
|
137 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 return False |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
143 else: |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
144 return True |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
154 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
|
155 """ |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
156 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
|
157 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
158 @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
|
159 @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
|
160 @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
|
161 @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
|
162 @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
|
163 @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
|
164 """ |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
165 if not isinstance(name, str) and not isinstance(name, unicode): |
569
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
166 raise AttributeError("SimpleXMLSerializer.set(): Invalid type for name argument.") |
568
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 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
|
169 e_type = "str" |
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 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
|
172 e_type = "bool" |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
173 value = str(value) |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
174 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
|
175 e_type = "int" |
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, float): |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
178 e_type = "float" |
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, unicode): |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
181 e_type = "unicode" |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
182 value = unicode(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, list): |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
184 e_type = "list" |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
185 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
|
186 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
|
187 e_type = "dict" |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
188 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
|
189 else: |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
190 e_type = "str" |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
191 value = str(value) |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
192 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
193 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
|
194 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
|
195 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
|
196 e.text = value |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
197 break |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
198 else: |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
199 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
|
200 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
|
201 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
|
202 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
|
203 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
|
204 elm.text = value |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
205 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
206 def _validateTree(self): |
569
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
207 """ |
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
208 Iterates the XML tree and prints warning when an invalid tag is found. |
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
209 |
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
210 Raises an InvalidFormat exception if there is a format error. |
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
211 """ |
568
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
212 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
|
213 if c.tag != "Module": |
569
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
214 raise InvalidFormat("Invalid tag in " + self._file + ". Expected Module, got: " + c.tag) |
568
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
215 elif c.get("name", "") == "": |
569
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
216 raise InvalidFormat("Invalid tag in " + self._file + ". Module name is empty.") |
568
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
217 else: |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
218 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
|
219 if e.tag != "Setting": |
569
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
220 raise InvalidFormat("Invalid tag in " + self._file + " in module: " + c.tag + ". Expected Setting, got: " + e.tag) |
568
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", "") == "": |
569
466d76db9701
Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
568
diff
changeset
|
222 raise InvalidFormat("Invalid tag in " + self._file + " in module: " + c.tag + ". Setting name is empty" + e.tag) |
568
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
223 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
224 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
|
225 """ |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
226 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
|
227 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
|
228 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
229 @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
|
230 @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
|
231 """ |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
232 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
|
233 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
|
234 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
235 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
|
236 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
|
237 return c |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
238 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
239 # Create module |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
240 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
|
241 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
242 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
|
243 """ |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
244 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
|
245 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
|
246 """ |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
247 i = "\n" + level*" " |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
248 if len(elem): |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
249 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
|
250 elem.text = i + " " |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
251 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
|
252 elem.tail = i |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
253 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
|
254 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
|
255 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
|
256 elem.tail = i |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
257 else: |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
258 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
|
259 elem.tail = i |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
260 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
261 # FIXME: |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
262 # 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
|
263 # 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
|
264 # 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
|
265 # 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
|
266 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
|
267 """ 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
|
268 return " ; ".join(list) |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
269 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
270 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
|
271 """ 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
|
272 return string.split(" ; ") |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
273 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
274 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
|
275 """ 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
|
276 serial = "" |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
277 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
|
278 value = dict[key] |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
279 if serial != "": serial += " ; " |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
280 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
|
281 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
282 return serial |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
283 |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
284 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
|
285 """ 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
|
286 dict = {} |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
287 items = serial.split(" ; ") |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
288 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
|
289 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
|
290 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
|
291 return dict |
bfbf329e1da8
Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
292 |