Imported Upstream version 2008.1+svn1648
[opeanno-debian-packaging.git] / game / settings.py
blob31e9daf9a6bc9f77a80046b533211bdecf253295
1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
3 # team@openanno.org
4 # This file is part of OpenAnno.
6 # OpenAnno is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # ###################################################
22 import game.main
23 import shutil
24 import os.path
25 import simplejson
27 class Setting(object):
28 """ Class to store settings
29 @param name:
30 """
31 def __init__(self, name = ''):
32 self._name = name
33 self._categorys = []
34 self._listener = []
35 try:
36 import config
37 for option in config.__dict__:
38 if option.startswith(name) and '.' not in option[len(name):]:
39 self.__dict__[option[len(name):]] = getattr(config, option)
40 except ImportError:
41 pass
42 for (option, value) in game.main.db("select substr(name, ?, length(name)), value from config.config where substr(name, 1, ?) = ? and substr(name, ?, length(name)) NOT LIKE '%.%'", len(name) + 1, len(name), name, len(name) + 1):
43 if not option in self.__dict__:
44 self.__dict__[option] = simplejson.loads(value)
45 if isinstance(self.__dict__[option], unicode):
46 self.__dict__[option] = str(self.__dict__[option])
48 def __getattr__(self, name):
49 """
50 @param name:
51 """
52 assert(not name.startswith('_'))
53 return None
55 def __setattr__(self, name, value):
56 """
57 @param name:
58 @param value:
59 """
60 self.__dict__[name] = value
61 if not name.startswith('_'):
62 assert(name not in self._categorys)
63 game.main.db("replace into config.config (name, value) values (?, ?)", self._name + name, simplejson.dumps(value))
64 for listener in self._listener:
65 listener(self, name, value)
67 def addChangeListener(self, listener):
68 """
69 @param listener:
70 """
71 for name in self._categorys:
72 self.__dict__[name].addChangeListener(listener)
73 self._listener.append(listener)
74 for name in self.__dict__:
75 if not name.startswith('_'):
76 listener(self, name, getattr(self, name))
78 def delChangeListener(self, listener):
79 """
80 @param listener:
81 """
82 for name in self._categorys:
83 self.__dict__[name].delChangeListener(listener)
84 self._listener.remove(listener)
86 def setDefaults(self, **defaults):
87 """
88 @param **defaults:
89 """
90 for name in defaults:
91 assert(not name.startswith('_'))
92 assert(name not in self._categorys)
93 if not name in self.__dict__:
94 self.__dict__[name] = defaults[name]
95 for listener in self._listener:
96 listener(self, name, defaults[name])
98 def addCategorys(self, *categorys):
99 """Adds one or more setting categories
101 The new categories can be accessed via
102 settingsObj.NEWCATEGORY
103 @param *categorys:
105 for category in categorys:
106 self._categorys.append(category)
107 inst = Setting(self._name + category + '.')
108 self.__dict__[category] = inst
109 for listener in self._listener:
110 inst.addChangeListener(listener)
112 class Settings(Setting):
113 VERSION = 2
115 @param config:
117 def __init__(self, config = 'config.sqlite'):
118 if not os.path.exists(config):
119 shutil.copyfile('content/config.sqlite', config)
120 game.main.db("ATTACH ? AS config", config)
121 version = game.main.db("PRAGMA config.user_version")[0][0]
122 if version > Settings.VERSION:
123 print "Error: Config version not supported, creating empty config which wont be saved."
124 game.main.db("DETACH config")
125 game.main.db("ATTACH ':memory:' AS config")
126 game.main.db("CREATE TABLE config.config (name TEXT PRIMARY KEY NOT NULL, value TEXT NOT NULL)")
127 elif version < Settings.VERSION:
128 print "Upgrading Config from Version " + str(version) + " to Version " + str(Settings.VERSION) + "..."
129 if version == 1:
130 game.main.db("UPDATE config.config SET name = REPLACE(name, '_', '.') WHERE name != 'client_id'")
131 version = 2
132 game.main.db("PRAGMA config.user_version = " + str(Settings.VERSION))
133 super(Settings, self).__init__()