forked from MathieuDuponchelle/Kerious-Resource-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
400 lines (330 loc) · 13.5 KB
/
settings.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# PiTiVi , Non-linear video editor
#
# settings.py
#
# Copyright (c) 2005, Edward Hervey <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301, USA.
"""
Settings
"""
import os
import gst
from ConfigParser import SafeConfigParser, ParsingError
import xdg.BaseDirectory as xdg_dirs # Freedesktop directories spec
from signallable import Signallable
from loggable import Loggable
def get_bool_env(var):
value = os.getenv(var)
if not value:
return False
value = value.lower()
if value == 'False':
return False
if value == '0':
return False
else:
return bool(value)
def get_env_by_type(type_, var):
"""
Returns the environment variable.
@arg type_: The type of the variable
@type type_: C{type}
@arg var: The name of the environment variable.
@type var: C{str}
@returns: The content of the environment variable, or C{None} if it doesn't
exist.
"""
if var is None:
return None
if type_ == bool:
return get_bool_env(var)
else:
value = os.getenv(var)
if value:
return type_(os.getenv(var))
return None
def get_env_default(var, default):
value = os.getenv(var)
if value:
return value
return default
def get_dir(path, autocreate=True):
if autocreate and not os.path.exists(path):
os.makedirs(path)
return path
def get_dirs(glob):
return [d for d in glob.split(os.path.pathsep) if os.path.exists(d)]
def get_env_dir(var, default, autocreate=True):
return get_dir(get_env_default(var, default))
def get_env_dirs(var, default):
return get_dirs(get_env_default(var, default))
def xdg_config_home(autocreate=True):
"""Get the directory for storing the user's pitivi configuration"""
return get_dir(os.path.join(xdg_dirs.xdg_config_home, "pitivi"), autocreate)
def xdg_data_home(autocreate=True):
"""Get the directory for storing the user's data: presets, plugins, etc."""
return get_dir(os.path.join(xdg_dirs.xdg_data_home, "pitivi"), autocreate)
def xdg_cache_home(autocreate=True):
"""Get the user cache directory"""
return get_dir(os.path.join(xdg_dirs.xdg_cache_home, "pitivi"), autocreate)
def xdg_data_dirs():
return get_env_dirs(xdg_dirs.xdg_data_dirs)
def xdg_config_dirs():
return get_env_dirs(xdg_dirs.xdg_config_dirs)
class ConfigError(Exception):
pass
class Notification(object):
"""A descriptor to help with the implementation of signals"""
def __init__(self, attrname):
self.attrname = "_" + attrname
self.signame = attrname + "Changed"
def __get__(self, instance, unused):
return getattr(instance, self.attrname)
def __set__(self, instance, value):
setattr(instance, self.attrname, value)
instance.emit(self.signame)
class GlobalSettings(Signallable):
"""
Global PiTiVi settings.
The settings object loads settings from three different sources: the
global configuration, the local configuration file, and the environment.
Modules declare which settings they wish to access by calling the
addConfigOption() class method during initialization.
@cvar options: A dictionnary of available settings.
@cvar environment: A list of the controlled environment variables.
"""
options = {}
environment = set()
defaults = {}
__signals__ = {}
def __init__(self, **kwargs):
Signallable.__init__(self)
self._config = SafeConfigParser()
self._readSettingsFromGlobalConfiguration()
self._readSettingsFromConfigurationFile()
self._readSettingsFromEnvironmentVariables()
def _readSettingsFromGlobalConfiguration(self):
# ideally, this should read settings from GConf for ex
pass
def _readSettingsFromConfigurationFile(self):
# This reads the configuration from the user configuration file
try:
conf_file_path = os.path.join(xdg_config_home(), "pitivi.conf")
self._config.read(conf_file_path)
except ParsingError:
return
for (section, attrname, typ, key, env,
value) in self.iterAllOptions():
if not self._config.has_section(section):
continue
if key and self._config.has_option(section, key):
if typ == int or typ == long:
try:
value = self._config.getint(section, key)
except ValueError:
# In previous configurations we incorrectly stored
# ints using float values.
value = int(self._config.getfloat(section, key))
elif typ == float:
value = self._config.getfloat(section, key)
elif typ == bool:
value = self._config.getboolean(section, key)
else:
value = self._config.get(section, key)
setattr(self, attrname, value)
def _readSettingsFromEnvironmentVariables(self):
for (section, attrname, typ, key, env,
value) in self.iterAllOptions():
var = get_env_by_type(typ, env)
if var is not None:
setattr(self, attrname, value)
def _writeSettingsToConfigurationFile(self):
conf_file_path = os.path.join(xdg_config_home(), "pitivi.conf")
for (section, attrname, typ, key, env_var,
value) in self.iterAllOptions():
if not self._config.has_section(section):
self._config.add_section(section)
if key:
if value is not None:
self._config.set(section, key, str(value))
else:
self._config.remove_option(section, key)
try:
file = open(conf_file_path, 'w')
except IOError, OSError:
return
self._config.write(file)
file.close()
def storeSettings(self):
"""
Write settings to the user's local configuration file. Note that only
those settings which were added with a section and a key value are
stored.
"""
self._writeSettingsToConfigurationFile()
def iterAllOptions(self):
"""
Iterate over all registered options
@return: an iterator which yields a tuple of (attrname, type, key,
environment, value for each option)
"""
for section, options in self.options.iteritems():
for attrname, (typ, key, environment) in options.iteritems():
yield section, attrname, typ, key, environment, getattr(self, attrname)
def isDefault(self, attrname):
return getattr(self, attrname) == self.defaults[attrname]
def setDefault(self, attrname):
setattr(self, attrname, self.defaults[attrname])
@classmethod
def addConfigOption(cls, attrname, type_=None, section=None, key=None,
environment=None, default=None, notify=False,):
"""
Add a configuration option.
This function should be called during module initialization, before
the config file is read. Only options registered before the config
file is read will be loaded.
see pitivi/mainwindow.py, pitivi/ui/medialibrary.py for examples of
usage.
@param attrname: the attribute of this class which represents the option
@type attrname: C{str}
@param type_: the type of the attribute. not necessary if default is
given.
@type type_: a builtin or class
@param section: The section of the config file under which this option is
saved. This section must have been added with addConfigSection(). Not
necessary if key is not given.
@param key: the key under which this option is to be saved. Can be none if
this option should not be saved.
@type key: C{str}
@param notify: whether or not this attribute should emit notification
signals when modified (default is False).
@type notify: C{boolean}
"""
if section and not section in cls.options:
raise ConfigError("You must add the section \"%s\" first." %
section)
if key and not section:
raise ConfigError("You must specify a section for key \"%s\"" %
key)
if section and key in cls.options[section]:
raise ConfigError("Option \"%s\" is already in use.")
if hasattr(cls, attrname):
raise ConfigError("Settings attribute \"%s\" is already in use.")
if environment and environment in cls.environment:
raise ConfigError("Settings environment varaible \"%s\" is"
"already in use.")
if not type_ and default == None:
raise ConfigError("Settings attribute \"%s\" has must have a"
" type or a default." % attrname)
if not type_:
type_ = type(default)
if notify:
setattr(cls, attrname, Notification(attrname))
setattr(cls, "_" + attrname, default)
cls.__signals__[attrname + 'Changed'] = []
else:
setattr(cls, attrname, default)
if section and key:
cls.options[section][attrname] = type_, key, environment
cls.environment.add(environment)
cls.defaults[attrname] = default
@classmethod
def addConfigSection(cls, section):
"""
Add a section to the local config file.
@param section: The section name. This section must not already exist.
@type section: C{str}
"""
if section in cls.options:
raise ConfigError("Duplicate Section \"%s\"." % section)
cls.options[section] = {}
class MultimediaSettings(Signallable, Loggable):
"""
Multimedia rendering and previewing settings
Signals:
'settings-changed' : the settings have changed
'encoders-changed' : The encoders or muxer have changed
@ivar render_scale: The scale to be applied to the video width and height
when rendering.
@type render_scale: int
"""
__signals__ = {
"settings-changed": None,
"encoders-changed": None,
}
# Audio/Video settings for processing/export
# TODO : Add PAR/DAR for video
# TODO : switch to using GstFraction internally where appliable
def __init__(self, **unused_kw):
Loggable.__init__(self)
self.videowidth = 720
self.videoheight = 576
self.render_scale = 100
self.videorate = gst.Fraction(25, 1)
self.videopar = gst.Fraction(16, 15)
self.audiochannels = 2
self.audiorate = 44100
self.audiodepth = 16
self.vencoder = None
self.aencoder = None
self.muxer = "oggmux"
# A (muxer -> containersettings) map.
self._containersettings_cache = {}
# A (vencoder -> vcodecsettings) map.
self._vcodecsettings_cache = {}
# A (aencoder -> acodecsettings) map.
self._acodecsettings_cache = {}
def copy(self):
ret = MultimediaSettings()
ret.videowidth = self.videowidth
ret.videoheight = self.videoheight
ret.render_scale = self.render_scale
ret.videorate = gst.Fraction(self.videorate.num, self.videorate.denom)
ret.videopar = gst.Fraction(self.videopar.num, self.videopar.denom)
ret.audiochannels = self.audiochannels
ret.audiorate = self.audiorate
ret.audiodepth = self.audiodepth
ret.vencoder = self.vencoder
ret.aencoder = self.aencoder
ret.muxer = self.muxer
ret.containersettings = dict(self.containersettings)
ret.acodecsettings = dict(self.acodecsettings)
ret.vcodecsettings = dict(self.vcodecsettings)
return ret
def getDAR(self):
return gst.Fraction(self.videowidth, self.videoheight) * self.videopar
def __str__(self):
"""
Redefine __str__ to allow printing the project audio/video settings.
This is used for debugging, do not make these strings translatable.
"""
msg = "\n\n"
msg += "\tVideo: " + str(self.videowidth) + "x" + str(self.videoheight) +\
" " + str(self.videorate) + " fps, " + str(self.videopar) + " PAR"
msg += "\n\t\tEncoder: " + str(self.vencoder)
if self.vcodecsettings:
msg += "\n\t\tCodec settings: " + str(self.vcodecsettings)
msg += "\n\tAudio: " + str(self.audiochannels) + " channels, " +\
str(self.audiorate) + " Hz, " + str(self.audiodepth) + " bits"
msg += "\n\t\tEncoder: " + str(self.aencoder)
if self.acodecsettings:
msg += "\n\t\tCodec settings: " + str(self.acodecsettings)
msg += "\n\tMuxer: " + str(self.muxer)
if self.containersettings:
msg += "\n\t\t" + str(self.containersettings)
msg += "\n\n"
return msg