1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
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 # ###################################################
27 class Setting(object):
28 """ Class to store settings
31 def __init__(self
, name
= ''):
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
)
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
):
52 assert(not name
.startswith('_'))
55 def __setattr__(self
, name
, value
):
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
):
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
):
82 for name
in self
._categorys
:
83 self
.__dict
__[name
].delChangeListener(listener
)
84 self
._listener
.remove(listener
)
86 def setDefaults(self
, **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
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
):
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
) + "..."
130 game
.main
.db("UPDATE config.config SET name = REPLACE(name, '_', '.') WHERE name != 'client_id'")
132 game
.main
.db("PRAGMA config.user_version = " + str(Settings
.VERSION
))
133 super(Settings
, self
).__init
__()