-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
312 lines (262 loc) · 11.6 KB
/
config.py
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Itaka 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
# any later version.
#
# Itaka 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 Itaka; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Copyright 2003-2009 Marc E.
# http://itaka.jardinpresente.com.ar
#
# $Id$
""" Itaka configuration engine """
__version__ = '0.3'
__revision__ = '$Rev$'
import os
import platform
import sys
import ConfigParser
import shutil
import traceback
#: Availability of libnotify
notify_available = False
try:
import pynotify
notify_available = True
if not pynotify.init('Itaka'):
print_w(_('Pynotify module is failing, disabling notifications'))
notify_available = False
except ImportError:
print_w(_('Pynotify module is missing, disabling notifications'))
notify_available = False
config_instance = ConfigParser.ConfigParser()
image_dir = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), 'share/images/')
sound_dir = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), 'share/sounds/')
system = os.name
# We need something more specific
platform = platform.system()
#: Minimum screen height to display all panes of Itaka
min_screen_height = 800
#: To be changed on install to specify where the installed files actually are
image_prefix = '/usr/local/share/itaka/images/'
if os.path.exists(image_prefix):
image_dir = image_prefix
if not os.path.exists(image_dir):
print_e(_('Could not find images directory %s' % (image_dir)))
sys.exit(1)
#: Save path for screenshots (system-specific specified later on)
try:
save_path = os.getcwd()
except:
print "[*] WARNING: Could not get current directory"
""" Console output verbosity
'normal' is for all normal operation mesages and warnings (not including errors)
'debug' is for all messages through self.console.debug
'quiet' is to quiet all errors and warnings. (totally quiet is in conjunction
with 'normal' = False, which quiets normal messages too)
"""
console_verbosity = {'normal': False, 'debug': False, 'quiet': False}
#: Globally acessable configuration values
configuration_values = {}
# Second best choice, TMP/TEMP on most systems
save_path = os.environ.get('TMP') or os.environ.get('TEMP')
# Try APPDATA on Windows or $HOME on POSIX
if (system == 'nt'):
if os.environ.get('APPDATA'):
save_path = os.path.join(os.environ.get('APPDATA'), 'itaka')
elif os.environ.get('HOME'):
save_path = os.path.join(os.environ.get('HOME'), 'itaka')
else:
if os.environ.get('HOME'):
save_path = os.path.join(os.environ.get('HOME'), '.itaka')
#: Default HTML headers and footers for the server.
# Putting <meta http-equiv="Refresh" content="5; url=/"> is very useful for debugging
head_html = '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<title>Itaka</title>
</head>
<body>
<div id="main">
'''
footer_html = '''
</div>
</body>
</html>'''
class ConfigParser:
"""
Itaka configuration engine
"""
def __init__(self, arguments=1):
"""
Configuration engine constructor. It also handles whether the
L{console_verbosity} setting is set to debug.
@type arguments: tuple
@param arguments: A tuple of sys.argv
"""
if len(arguments) > 1 and arguments[-1] in ('-d', '--debug'):
global console_verbosity
console_verbosity = {'normal': True, 'debug': True, 'quiet': False}
print_m(_('Initializing in debug mode'))
#: Default configuration sections and values
# WARNING: Don't forget to update configuration_dict in uigtk.py if you change this!
self.default_options = (
{'server': (
('port', 8000), ('authentication', False),
('username', 'itaka'), ('password', 'password'),
('notify', notify_available)
)},
{'screenshot': (
('format', 'jpeg'), ('quality', 30), ('path', save_path),
('currentwindow', False), ('scale', False),
('scalepercent', 100)
)},
{'log': (
('logtimeformat', '[%d/%b/%Y %H:%M:%S]'),
('logfile', '~/.itaka/access.log')
)},
{'html': (
('html', '<img src="screenshot" alt="If you are seeing this message it means there was an error in Itaka or you are using a text-only browser.">'),
('authfailure', '<p><strong>Sorry, but you cannot access this resource without authorization.</strong></p>')
)}
)
def load(self):
"""
Set up and load configuration
@rtype: dict
@return: Dictionary of configuration values.
"""
self.config_file = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "itaka.conf")
global configuration_values
configuration_values = {}
# Check routine
if system in ('posix'):
if not (os.path.exists(os.path.join(os.environ['HOME'], '.itaka/itaka.conf'))):
self.create(os.path.join(os.environ['HOME'], '.itaka/itaka.conf'))
else:
self.config_file = os.path.join(os.environ['HOME'], '.itaka/itaka.conf')
elif (system == 'nt'):
if not (os.path.exists(os.path.join(os.environ['APPDATA'], 'Itaka/config.ini'))):
self.create(os.path.join(os.environ['APPDATA'], 'Itaka/config.ini'))
else:
self.config_file = os.path.join(os.environ['APPDATA'], 'Itaka/config.ini')
else:
# Generic path
if not os.path.exists(self.config_file):
self.create(self.config_file)
# Read and assign values from the configuration file
try:
config_instance.read(self.config_file)
if console_verbosity['normal']:
print_m(_('Loaded configuration %s') % (self.config_file))
except:
if console_verbosity['normal']: print_e(_('Could not read configuration file (%s)' % (self.config_file)))
if console_verbosity['debug']: traceback.print_exc()
# Get values as a dict and return it
for section in config_instance.sections():
configuration_values[section] = dict(config_instance.items(section))
# Convert 'False' and 'True' into booleans, and numbers into ints
# Add config options that are not there
for option, value in configuration_values[section].iteritems():
if value.strip() == 'True':
configuration_values[section][option] = True
elif value.strip() == 'False':
configuration_values[section][option] = False
elif value.isdigit():
configuration_values[section][option] = int(value)
# Compare it to our default configuration set, to see if there is anything missing
# This is useful for updates, and corrupted files.
# NOTE: The setting of values[section][key] here is purely pragmatical, so we
# dont have to reload
brokenwarning = False
for configdict in self.default_options:
for section in configdict:
if not configuration_values.has_key(section):
if not console_verbosity['quiet'] and not brokenwarning:
print_w(_('Upgrading configuration file.'))
brokenwarning = True
config_instance.add_section(section)
configuration_values[section] = {}
for keyset in configdict[section]:
key, val = keyset
self.update(section, key, val)
configuration_values[section][key] = val
else:
# Check if all the key:vals are in the section
for keyset in configdict[section]:
key, val = keyset
if not configuration_values[section].has_key(key):
if not console_verbosity['quiet'] and not brokenwarning:
print_w(_('Detected old or broken configuration file. Fixing'))
self.update(section, key, val)
configuration_values[section][key] = val
brokenwarning = True
return configuration_values
def save(self, valuesdict):
"""
Saves a dictionary containing the configuration
@type valuesdict: dict
@param valuesdict: Dictionary of configuration
"""
# Unpack the dict into section, option, value
for section in valuesdict.keys():
for key, value in valuesdict[section].items():
config_instance.set(section, key, value)
# Save
try:
config_instance.write(open(self.config_file, 'w'))
if console_verbosity['normal']: print_m(_('Saving configuration'))
except:
if not console_verbosity['quiet']: print_e(_('Could not write configuration file %s' % (self.config_file)))
if console_verbosity['debug']: traceback.print_exc()
def update(self, section, key, value):
"""
Update a specific key's value
@type section: str
@param section: String of the section of the key to update
@type key: str
@param key: String of the key to update
@type value: str/int/bool
@param value: Value of the key to update
"""
config_instance.set(section, key, value)
try:
config_instance.write(open(self.config_file, 'w'))
if console_verbosity['debug']: print_m(_("Updating configuration key '%(key)s' to '%(value)s'") % {'key': key, 'value': value})
except:
if not console_verbosity['quiet']: print_e(_('Could not write configuration file %s' % (self.config_file)))
if console_verbosity['debug']: traceback.print_exc()
def create(self, path):
"""
Create a configuration file from default values
@type path: str
@param path: Path to the configuration file
"""
if console_verbosity['normal']: print_m(_('Creating default configuration'))
# Set default sections and options
for configdict in self.default_options:
for section in configdict:
config_instance.add_section(section)
for keyset in configdict[section]:
key, val = keyset
config_instance.set(section, key, val)
if not (os.path.exists(os.path.dirname(path))):
shutil.os.mkdir(os.path.dirname(path))
try:
config_instance.write(open(path, 'w'))
except:
if not console_verbosity['quiet']: print_e(_('Could not write configuration file %s' % (path)))
if console_verbosity['debug']: traceback.print_exc()
self.config_file = path