annotate engine/python/fife/extensions/serializers/simplexml.py @ 614:567d53c1c010

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