annotate engine/python/fife/extensions/serializers/simplexml.py @ 612:867aad1c01cd

Added the ability to use a custom serializer with the fife.Setting class.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Wed, 22 Sep 2010 14:05:33 +0000
parents 0bbe6e8ad9c8
children 567d53c1c010
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
612
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
37 class SimpleSerializer(object):
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
38 """
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
39 Use this as a base class for custom setting loaders/savers to use with the Setting class.
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
40 """
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
41
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
42 def __init__(self, filename=None):
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
43 pass
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
44
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
45 def get(self, module, name, defaultValue=None):
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
46 pass
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
47
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
48 def set(self, module, name, value, extra_attrs={}):
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
49 pass
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
50
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
51 def load(self, filename=None):
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
52 pass
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
53
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
54 def save(self, filename=None):
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
55 pass
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
56
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
57
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
58 class SimpleXMLSerializer(SimpleSerializer):
568
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 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
61
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
62 Usage::
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
63 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
64 serializer = SimpleXMLSerializer(filename="somefile.xml")
596
0bbe6e8ad9c8 - corrected example in docstring
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 569
diff changeset
65 serializer.set("module_name", "variable_name", "value")
0bbe6e8ad9c8 - corrected example in docstring
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 569
diff changeset
66 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
67 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
68 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
69 self._file = filename
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
70 self._tree = None
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
71 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
72
612
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
73 self._initialized = False
568
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 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
76 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
77 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
78
569
466d76db9701 Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 568
diff changeset
79 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
80
612
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
81 @param filename: The file to load
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
82 @type filename: C{str}
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
83
568
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
84 @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
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 self._file = filename
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 self._file:
569
466d76db9701 Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 568
diff changeset
90 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
91
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
92 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
93 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
94 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
95 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
96 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
97
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
98 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
99 self._validateTree()
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
100
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
101 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
102 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
103 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
104
612
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
105 @param filename: The file to save
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
106 @type filename: C{str}
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
107
568
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
108 @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
109 """
612
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
110 if not self._initialized:
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
111 self.load()
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
112 self._initialized = True
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
113
568
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
114 if filename:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
115 savefile = filename
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
116 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
117 savefile = self._file
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
118
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
119 if not savefile:
569
466d76db9701 Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 568
diff changeset
120 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
121
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
122 """ 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
123 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
124 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
125
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 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
128 """ 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
129
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
130 @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
131 @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
132 @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
133 @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
134 """
612
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
135 if not self._initialized:
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
136 self.load()
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
137 self._initialized = True
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
138
568
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
139 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
140 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
141
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
142 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
143 element = None
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
144 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
145 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
146 element = e
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
147 break
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
148 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
149 return defaultValue
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
150
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
151 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
152 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
153 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
154
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
155 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
156 return defaultValue
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 # Strip value
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
159 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
160 e_strip = False
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
161 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
162
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
163 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
164 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
165 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
166 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
167
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
168 # Return value
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
169 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
170 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
171 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
172 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
173 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
174 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
175 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
176 return False
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
177 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
178 return True
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
179 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
180 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
181 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
182 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
183 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
184 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
185 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
186 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
187
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
188 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
189 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
190 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
191
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
192 @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
193 @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
194 @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
195 @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
196 @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
197 @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
198 """
612
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
199 if not self._initialized:
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
200 self.load()
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
201 self._initialized = True
867aad1c01cd Added the ability to use a custom serializer with the fife.Setting class.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 596
diff changeset
202
568
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
203 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
204 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
205
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
206 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
207 e_type = "str"
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 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
210 e_type = "bool"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
211 value = str(value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
212 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
213 e_type = "int"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
214 value = str(value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
215 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
216 e_type = "float"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
217 value = str(value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
218 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
219 e_type = "unicode"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
220 value = unicode(value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
221 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
222 e_type = "list"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
223 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
224 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
225 e_type = "dict"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
226 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
227 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
228 e_type = "str"
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
229 value = str(value)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
230
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
231 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
232 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
233 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
234 e.text = value
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
235 break
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
236 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
237 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
238 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
239 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
240 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
241 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
242 elm.text = value
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 def _validateTree(self):
569
466d76db9701 Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 568
diff changeset
245 """
466d76db9701 Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 568
diff changeset
246 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
247
466d76db9701 Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 568
diff changeset
248 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
249 """
568
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
250 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
251 if c.tag != "Module":
569
466d76db9701 Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 568
diff changeset
252 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
253 elif c.get("name", "") == "":
569
466d76db9701 Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 568
diff changeset
254 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
255 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
256 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
257 if e.tag != "Setting":
569
466d76db9701 Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 568
diff changeset
258 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
259 elif c.get("name", "") == "":
569
466d76db9701 Some small code cleanups in the extensions.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 568
diff changeset
260 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
261
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
262 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
263 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
264 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
265 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
266
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
267 @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
268 @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
269 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
270 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
271 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
272
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
273 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
274 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
275 return c
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
276
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
277 # Create module
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
278 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
279
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
280 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
281 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
282 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
283 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
284 """
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
285 i = "\n" + level*" "
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
286 if len(elem):
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
287 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
288 elem.text = i + " "
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
289 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
290 elem.tail = i
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
291 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
292 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
293 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
294 elem.tail = i
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
295 else:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
296 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
297 elem.tail = i
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
298
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
299 # FIXME:
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
300 # 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
301 # 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
302 # 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
303 # 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
304 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
305 """ 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
306 return " ; ".join(list)
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
307
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
308 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
309 """ 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
310 return string.split(" ; ")
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
311
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
312 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
313 """ 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
314 serial = ""
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
315 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
316 value = dict[key]
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
317 if serial != "": serial += " ; "
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
318 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
319
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
320 return serial
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
321
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
322 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
323 """ 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
324 dict = {}
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
325 items = serial.split(" ; ")
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
326 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
327 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
328 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
329 return dict
bfbf329e1da8 Forgot to add the simplexml.py file in my last commit.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
330