-
Notifications
You must be signed in to change notification settings - Fork 2
/
optiondictionary.py
67 lines (61 loc) · 2.64 KB
/
optiondictionary.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
#-----------------------------------------------------------
#
# QGIS Combo Manager is a python module to easily manage a combo
# box with a layer list and eventually relate it with one or
# several combos with list of corresponding fields.
#
# Copyright : (C) 2013 Denis Rouzaud
# Email : [email protected]
#
#-----------------------------------------------------------
#
# licensed under the terms of GNU GPL 2
#
# 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 progsram; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#---------------------------------------------------------------------
class OptionDictionary(dict):
"""
Conveninent class for options
use OptionDictionary(availableOptions, userOptions)
- availableOptions: a dictionary of {option_name: default_values}. default_value can be:
- list-of-possible-values (default listed first)
- or a single default value (no check)
- or a type (no check, default is an empty instance of type)
- userOptions: the dictionary given as argument in your method {option_name: value}
"""
def __init__(self, availableOptions, userOptions):
dict.__init__(self)
# check user options
for key in userOptions:
if key not in availableOptions:
raise NameError("Option '%s' does not exist" % key)
if type(availableOptions[key]) in (list, tuple) and userOptions[key] not in availableOptions[key]:
raise NameError("Invalid value '%s' for option '%s'" % (userOptions[key], key))
# create dictionary
for key, value in availableOptions.iteritems():
try:
self[key] = userOptions[key]
except KeyError:
if type(value) in (list, tuple):
self[key] = value[0]
elif type(value) == type:
self[key] = value()
else:
self[key] = value
def __getattr__(self, item):
return self[item]
def __setattr__(self, key, value):
self[key] = value