forked from studio1247/gertrude
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
278 lines (242 loc) · 9.48 KB
/
config.py
File metadata and controls
278 lines (242 loc) · 9.48 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# -*- coding: utf-8 -*-
## This file is part of Gertrude.
##
## Gertrude 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 3 of the License, or
## (at your option) any later version.
##
## Gertrude 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 Gertrude; if not, see <http://www.gnu.org/licenses/>.
import __builtin__
import sys, os.path, shutil, time
import urllib2
import ConfigParser
from functions import *
from data import FileConnection, SharedFileConnection, HttpConnection
CONFIG_FILENAME = "gertrude.ini"
DEFAULT_SECTION = "gertrude"
DEFAULT_DATABASE = "gertrude.db"
DEMO_DATABASE = "demo.db"
class Database(object):
def __init__(self, section=None, filename=DEFAULT_DATABASE):
self.section = section
self.filename = filename
self.connection = FileConnection(filename)
class Section(object):
def __init__(self, database):
self.database = database
self.numfact = None
self.codeclient = None
class Config(object):
def __init__(self):
self.sections = {}
self.options = 0
self.connection = None
def setSection(self, section):
self.default_section = section
self.database = self.sections[section].database
self.connection = self.database.connection
self.numfact = self.sections[section].numfact
self.codeclient = self.sections[section].codeclient
__builtin__.config = Config()
def getOptions(parser):
options = 0
try:
str = parser.get(DEFAULT_SECTION, "options")
if "reservataires" in str:
options |= RESERVATAIRES
if "categories" in str:
options |= CATEGORIES
if "heures-contrat" in str:
options |= HEURES_CONTRAT
if "tablette" in str:
options |= TABLETTE
if "decloture" in str:
options |= DECLOTURE
except:
pass
return options
def getWindowSize(parser):
try:
window_width = int(parser.get(DEFAULT_SECTION, "window-width"))
window_height = int(parser.get(DEFAULT_SECTION, "window-height"))
return window_width, window_height
except:
return (1000, 600)
def getColumnWidth(parser):
try:
result = int(parser.get(DEFAULT_SECTION, "column-width"))
except:
result = 4 # px
return result
def getDefaultDocumentsDirectory():
if sys.platform == 'win32':
try:
from win32com.shell import shell
df = shell.SHGetDesktopFolder()
pidl = df.ParseDisplayName(0, None,
"::{450d8fba-ad25-11d0-98a8-0800361b1103}")[1]
return shell.SHGetPathFromIDList(pidl)
except:
print u"L'extension win32com pour python est recommandée (plateforme windows) !"
return os.getcwd()
else:
return os.getcwd()
def getDocumentsDirectory(parser):
try:
directory = parser.get(DEFAULT_SECTION, "documents-directory")
assert os.path.isdir(directory)
return directory
except:
return getDefaultDocumentsDirectory()
def getBackupsDirectory(parser):
try:
directory = parser.get(DEFAULT_SECTION, "backups-directory")
assert os.path.isdir(directory)
return directory
except:
return ""
def getDefaultSection(parser):
try:
return parser.get(DEFAULT_SECTION, "default-database")
except:
return None
def getField(parser, section, field):
try:
return parser.get(section, field)
except:
try:
return parser.get(DEFAULT_SECTION, field)
except:
return None
def getDatabase(parser, section):
try:
filename = parser.get(section, "database")
except:
if section == DEFAULT_SECTION:
return None
else:
filename = DEFAULT_DATABASE
database = Database(section, filename)
try:
url = parser.get(section, "url")
except:
return database
if url.startswith("http://"):
try:
auth_info = (parser.get(section, "login"), parser.get(section, "password"))
except:
auth_info = None
try:
identity = parser.get(section, "identity")
except:
identity = ""
proxy_info = { }
try:
proxy_info = { 'host' : parser.get(DEFAULT_SECTION, "proxy-host"),
'port' : int(parser.get(DEFAULT_SECTION, "proxy-port")),
}
try:
proxy_user_info = {'user' : parser.get(DEFAULT_SECTION, "proxy-user"),
'pass' : parser.get(DEFAULT_SECTION, "proxy-pass")
}
proxy_info.extend(proxy_user_info)
except:
pass
except:
proxy_info = None
database.connection = HttpConnection(url, filename, identity, auth_info, proxy_info)
elif url.startswith("file://"):
try:
identity = parser.get(section, "identity")
except:
identity = datetime.time()
database.connection = SharedFileConnection(url[7:], filename, identity)
return database
def LoadConfig(progress_handler=default_progress_handler):
progress_handler.display(u"Chargement de la configuration ...")
parser = None
if os.path.isfile(CONFIG_FILENAME):
try:
parser = ConfigParser.SafeConfigParser()
parser.read(CONFIG_FILENAME)
except:
progress_handler.display(u"Fichier %s erroné. Utilisation de la configuration par défaut." % CONFIG_FILENAME)
else:
progress_handler.display(u"Pas de fichier %s. Utilisation de la configuration par défaut." % CONFIG_FILENAME)
config.original_window_size = getWindowSize(parser)
config.window_size = config.original_window_size
config.column_width = getColumnWidth(parser)
config.options = getOptions(parser)
config.original_documents_directory = getDocumentsDirectory(parser)
config.documents_directory = config.original_documents_directory
config.original_backups_directory = getBackupsDirectory(parser)
config.backups_directory = config.original_backups_directory
config.original_default_section = getDefaultSection(parser)
config.default_section = config.original_default_section
if parser:
for section in parser.sections():
database = getDatabase(parser, section)
if database:
config.sections[section] = Section(database)
config.sections[section].numfact = getField(parser, section, "numfact")
config.sections[section].codeclient = getField(parser, section, "codeclient")
if not config.sections:
config.sections[None] = Section(Database())
if len(config.sections) == 1:
config.setSection(config.sections.keys()[0])
def SaveConfig(progress_handler):
parameters = {}
if config.window_size != config.original_window_size:
parameters["window-width"] = str(config.window_size[0])
parameters["window-height"] = str(config.window_size[1])
if config.documents_directory != config.original_documents_directory:
parameters["documents-directory"] = config.documents_directory
if config.backups_directory != config.original_backups_directory:
parameters["backups-directory"] = config.backups_directory
if config.default_section != config.original_default_section:
parameters["default-database"] = config.default_section
if parameters:
try:
parser = ConfigParser.SafeConfigParser()
parser.read(CONFIG_FILENAME)
if not parser.has_section(DEFAULT_SECTION):
parser.add_section(DEFAULT_SECTION)
for key in parameters.keys():
parser.set(DEFAULT_SECTION, key, parameters[key])
parser.write(file(CONFIG_FILENAME, "w"))
except Exception, e:
print e
progress_handler.display(u"Impossible d'enregistrer le répertoire de destination des documents !")
def Load(progress_handler=default_progress_handler):
__builtin__.creche, __builtin__.readonly = config.connection.Load(progress_handler)
return creche is not None
def Save(progress_handler=default_progress_handler):
return config.connection.Save(progress_handler)
def Restore(progress_handler=default_progress_handler):
return config.connection.Restore(progress_handler)
def Update():
return config.connection.Update()
def Exit(progress_handler=default_progress_handler):
SaveConfig(progress_handler)
return config.connection.Exit(progress_handler)
def Liste(progress_handler=default_progress_handler):
result = {}
try:
c = creche
except:
c = None
for value in config.sections.values():
if value.section == config.default_section and c:
for inscrit in c.inscrits:
result[GetPrenomNom(inscrit)] = value
else:
for entry in database.connection.Liste(progress_handler):
result[entry] = value
return result