source: trunk/game/settings.py @ 475

Revision 475, 2.4 KB checked in by maximinus_parpg, 10 years ago (diff)

Cleaned up run.py and settings.py.
settings.py was very ugly, almost unchanged from rio_de_hola.
Some unused code removed.

  • Property svn:eol-style set to native
RevLine 
[143]1#!/usr/bin/python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
[26]18try:
[30]19    import xml.etree.cElementTree as ET
[26]20except:
[30]21    import xml.etree.ElementTree as ET
[26]22
23class Setting(object):
[475]24    def __init__(self):
25        self.root_element = None
[26]26
[283]27    def readSetting(self, name, type='int', strip=True, text=False, default=None):
[30]28        if not hasattr(self, 'tree'):
[475]29            tree = ET.parse('settings.xml')
30            self.root_element = tree.getroot()
[30]31        element = self.root_element.find(name)
32        if element is not None:
33            element_value = element.text
34            if element_value is None:
35                if type == 'int':
36                    return 0
37                elif type == 'list':
38                    list = []
39                    return list
40            else:
41                if type == 'int':
42                    return element_value.strip() if strip else element_value
43                elif type == 'list':
44                    list = []
45                    list_s = []
46                    list = str(element_value.strip()).split(";")
47                    for item in list:
48                        item = item.strip()
49                        if text:
50                            item = item.replace('\\n', '\n')
51                        list_s.append(item)
52                    return list_s
53                elif type == 'bool':
54                    return False if element_value.strip() == 'False' else True
55        else:
56            print 'Setting,', name, 'does not exist!'
[283]57        return default
58
[30]59    def setSetting(self, name, value):
60        element = self.root_element.find(name)
61        if element is not None:
62            if value is not element.text:
63                element.text = str(value)
64        else:
65            print 'Setting,', name, 'does not exist!'
Note: See TracBrowser for help on using the repository browser.