source: trunk/game/settings.py @ 366

Revision 283, 2.5 KB checked in by orlandov, 10 years ago (diff)

Ticket #96: Patch by amo-ej1 (with addition from or1andov) fixes[s:trac, t:96]

  • Property svn:eol-style set to native
Line 
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
18import shutil
19
20try:
21    import xml.etree.cElementTree as ET
22except:
23    import xml.etree.ElementTree as ET
24
25class Setting(object):
26    def setDefaults(self):
27        shutil.copyfile('settings-dist.xml', 'settings.xml')
28        self.isSetToDefault = True
29        self.changesRequireRestart = True
30
31    def readSetting(self, name, type='int', strip=True, text=False, default=None):
32        if not hasattr(self, 'tree'):
33            self.tree = ET.parse('settings.xml')
34            self.root_element = self.tree.getroot()
35        element = self.root_element.find(name)
36        if element is not None:
37            element_value = element.text
38            if element_value is None:
39                if type == 'int':
40                    return 0
41                elif type == 'list':
42                    list = []
43                    return list
44            else:
45                if type == 'int':
46                    return element_value.strip() if strip else element_value
47                elif type == 'list':
48                    list = []
49                    list_s = []
50                    list = str(element_value.strip()).split(";")
51                    for item in list:
52                        item = item.strip()
53                        if text:
54                            item = item.replace('\\n', '\n')
55                        list_s.append(item)
56                    return list_s
57                elif type == 'bool':
58                    return False if element_value.strip() == 'False' else True
59        else:
60            print 'Setting,', name, 'does not exist!'
61
62        return default
63
64    def setSetting(self, name, value):
65        element = self.root_element.find(name)
66        if element is not None:
67            if value is not element.text:
68                element.text = str(value)
69        else:
70            print 'Setting,', name, 'does not exist!'
71
Note: See TracBrowser for help on using the repository browser.