comparison engine/python/fife/extensions/fife_settings.py @ 543:cb7ec12214a9

Items can now be serialized/deserialized to/from disk. I haven't finished actors yet. This allows for persistent states when you enter/leave maps.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Wed, 02 Jun 2010 21:43:03 +0000
parents 4c7b5eee211c
children 9b549d42f9ea
comparison
equal deleted inserted replaced
542:67f6f3538e88 543:cb7ec12214a9
79 """ 79 """
80 80
81 EMPTY_SETTINGS="""\ 81 EMPTY_SETTINGS="""\
82 <?xml version='1.0' encoding='UTF-8'?> 82 <?xml version='1.0' encoding='UTF-8'?>
83 <Settings> 83 <Settings>
84 <Module name="FIFE">
85
86 </Module>
87 84
88 </Settings> 85 </Settings>
89 """ 86 """
90 87
91 DEFAULT_MODULE = "FIFE" 88 DEFAULT_MODULE = "FIFE"
99 settings = Setting(app_name="myapp") 96 settings = Setting(app_name="myapp")
100 screen_width = settings.get("FIFE", "ScreenWidth", 1024) 97 screen_width = settings.get("FIFE", "ScreenWidth", 1024)
101 screen_height = settings.get("FIFE", "ScreenHeight", 768) 98 screen_height = settings.get("FIFE", "ScreenHeight", 768)
102 """ 99 """
103 100
104 def __init__(self, app_name="", settings_file="", settings_gui_xml="", changes_gui_xml=""): 101 def __init__(self, app_name="", settings_file="", settings_gui_xml="", changes_gui_xml="", copy_dist=True):
105 """ 102 """
106 Initializes the Setting object. 103 Initializes the Setting object.
107 104
108 @param app_name: The applications name. If this parameter is provided 105 @param app_name: The applications name. If this parameter is provided
109 alone it will try to read the settings file from the users home directory. 106 alone it will try to read the settings file from the users home directory.
113 provided it will look for the setting file as you specify it, first looking 110 provided it will look for the setting file as you specify it, first looking
114 in the working directory. It will NOT look in the users home directory. 111 in the working directory. It will NOT look in the users home directory.
115 @type settings_file: C{string} 112 @type settings_file: C{string}
116 @param settings_gui_xml: If you specify this parameter you can customize the look 113 @param settings_gui_xml: If you specify this parameter you can customize the look
117 of the settings dialog box. 114 of the settings dialog box.
118 @note: As of now you MUST have all the elements of the default settings dialog box. 115 @param copy_dist: Copies the settings-dist.xml file to the settings_file location. If
119 At some point we may make it customizable. 116 this is False it will create a new empty xml file.
120 117
121 """ 118 """
122 self._app_name = app_name 119 self._app_name = app_name
123 self._settings_file = settings_file 120 self._settings_file = settings_file
124 self._settings_gui_xml = settings_gui_xml 121 self._settings_gui_xml = settings_gui_xml
138 if self._changes_gui_xml == "": 135 if self._changes_gui_xml == "":
139 self._changes_gui_xml = CHANGES_REQUIRE_RESTART 136 self._changes_gui_xml = CHANGES_REQUIRE_RESTART
140 137
141 138
142 if not os.path.exists(os.path.join(self._appdata, self._settings_file)): 139 if not os.path.exists(os.path.join(self._appdata, self._settings_file)):
143 if os.path.exists('settings-dist.xml'): 140 if os.path.exists('settings-dist.xml') and copy_dist:
144 shutil.copyfile('settings-dist.xml', os.path.join(self._appdata, self._settings_file)) 141 shutil.copyfile('settings-dist.xml', os.path.join(self._appdata, self._settings_file))
145 else: 142 else:
146 #no settings file found 143 #no settings file found
147 tree = ET.parse(StringIO(EMPTY_SETTINGS)) 144 tree = ET.parse(StringIO(EMPTY_SETTINGS))
148 tree.write(os.path.join(self._appdata, self._settings_file), 'UTF-8') 145 tree.write(os.path.join(self._appdata, self._settings_file), 'UTF-8')
202 """ 199 """
203 self._entries.append(entry) 200 self._entries.append(entry)
204 201
205 def loadSettings(self): 202 def loadSettings(self):
206 self._tree = ET.parse(os.path.join(self._appdata, self._settings_file)) 203 self._tree = ET.parse(os.path.join(self._appdata, self._settings_file))
204
207 self._root_element = self._tree.getroot() 205 self._root_element = self._tree.getroot()
208 self.validateTree() 206 self.validateTree()
209 207
210 def setGuiStyle(self, style): 208 def setGuiStyle(self, style):
211 """ Set a custom gui style used for the option dialog. 209 """ Set a custom gui style used for the option dialog.