-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseApp.py
More file actions
executable file
·172 lines (151 loc) · 5.26 KB
/
BaseApp.py
File metadata and controls
executable file
·172 lines (151 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# $Id$
#
# Copyright (C) 2007 Alberto Montañola Lacort
# See the file AUTHORS for more info
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
# Please see the file COPYING for the full license.
#
""" Base Application Classes """
import ConfigParser
class MyConfigParser(ConfigParser.SafeConfigParser):
"""A config parser in the way that I want it to work"""
def writeFile(self,name):
"""
Writes the configuration to the file by name
@param name Destination file name
"""
fo=file(name,"w")
self.write(fo)
fo.close()
def set(self,section,name,value):
"""Control the NoSectionError"""
if not self.has_section(section):
self.add_section(section)
ConfigParser.SafeConfigParser.set(self,section,name,value)
def get(self,section,name,default=None):
"""We want to return default or None instead of an Exception"""
try:
return ConfigParser.SafeConfigParser.get(self,section,name)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
if default!=None:
self.set(section,name,default)
return default
class BaseApplication(object):
"""
Base Aplication, with configuration stuff, etc...
Intended to be used with any type of GUI framework
(Gtk, Qt, wxPython, Tk, CEGUI, etc...)
"""
def __init__(self,config=None):
"""@param config Configuration file"""
self.__configFile=config
self.config=None
self._loadConfig()
def _loadConfig(self,cfg=None):
"""
Load App configuration
@param cfg Configuration file
"""
if cfg==None:
cfg=self.__configFile
self.config=MyConfigParser()
self._setConfigDefaults()
if cfg!=None:
self.config.read(cfg)
def _setConfigDefaults(self):
"""
Sets the configuration defaults
Must be overrided
"""
pass
def _saveConfig(self,cfg=None):
"""
Save App configuration
@param cfg configuration file
"""
if cfg==None:
cfg=self.__configFile
if cfg!=None:
self.config.writeFile(cfg)
def GetCfg(self,section,name,default=None):
"""self.config.get Shortcut (that my be overrided for other unknown purposes)"""
return self.config.get(section,name,default)
def _installGettext(self,lang=None):
"""Installs GetText"""
import gettext
if lang==None:
gettext.install(self.GetCfg("global","app.gettext.domain"),
self.GetCfg("global","app.gettext.locales"),True)
else:
gettext.translation(self.GetCfg("global","app.gettext.domain"),
self.GetCfg("global","app.gettext.locales"),
(lang,)).install(True)
def GetLanguages(self):
"""Gets all avaliable languages"""
try:
return self.__Languages
except:
import dircache
self.__Languages={}
# Dirty hardcoded ugly LangDict
_ = lambda x : x
LangDict={"en":_("English"),"es":_("Spanish"),"ca":_("Catalan")}
for lan in dircache.listdir(self.GetCfg("global","app.gettext.locales")):
if lan.startswith("."): continue
if lan in LangDict:
self.__Languages[lan]=LangDict[lan]
else:
self.__Languages[lan]=lan
return self.__Languages
def SetLanguage(self,lang):
"""
Sets the app language
@param lang Language
"""
#import locale
#locale.setlocale(locale.LC_ALL, (lang,"utf-8"))
self.__Language=lang
self._installGettext(lang)
def GetLanguage(self):
"""
Gets the current language
"""
try:
return self.__Language
except:
import locale
self.__Language=locale.getdefaultlocale()[0][:2]
return self.__Language
def GetAppVersion(self):
"""
Return app Revision
"""
return "$Id$"
if __name__ == "__main__":
a=MyConfigParser()
a.set("global","hola","feo")
assert(a.get("global","hola")=="feo")
assert(a.get("global","hola2")==None)
assert(a.get("global","whola3","xxxx")=="xxxx")
assert(a.get("global","whola3")=="xxxx")
a.set("main","yes","no")
assert(a.get("main","yes")=="no")
assert(a.get("null","wth")==None)