-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
107 lines (96 loc) · 3.69 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
import yaml
import random
LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
class RecursiveDictionary(dict):
""" taken from https://gist.github.com/114831
RecursiveDictionary provides the methods rec_update and iter_rec_update
that can be used to update member dictionaries rather than overwriting
them."""
__author__ = '[email protected] (Jannis Andrija Schnitzer)'
def rec_update(self, other, **third):
"""Recursively update the dictionary with the contents of other and
third like dict.update() does - but don't overwrite sub-dictionaries.
Example:
>>> d = RecursiveDictionary({'foo': {'bar': 42}})
>>> d.rec_update({'foo': {'baz': 36}})
>>> d
{'foo': {'baz': 36, 'bar': 42}}
"""
try:
iterator = other.iteritems()
except AttributeError:
iterator = other
self.iter_rec_update(iterator)
self.iter_rec_update(third.iteritems())
def iter_rec_update(self, iterator):
for (key, value) in iterator:
if key in self and \
isinstance(self[key], dict) and isinstance(value, dict):
self[key] = RecursiveDictionary(self[key])
self[key].rec_update(value)
else:
self[key] = value
class Config(RecursiveDictionary):
def __init__(self, file=''):
self.defaultvalues()
f = open(file, 'r')
data = f.read()
f.close()
try:
config = yaml.load(data)
except yaml.parser.ParserError, e:
config = {}
print "Error parsing configuration file %s" % file
print e
self.rec_update(config)
self.addhardcodedvalues()
self.port = None
self.defaultsecret = "".join([random.choice(LETTERS)
for i in range(20)])
def defaultvalues(self):
self.update({
'cookies': {},
'web': {},
'logger': {},
'internal': {}
})
def addhardcodedvalues(self):
""" Those are constant values, not default ones """
self.rec_update({
'pyscard': {'module': 'smartcard.scard', 'hard': True},
'clusterscard': {
'module': 'webscard.implementations.clusterscard',
'hard': True,
'free': 'isfree',
'acquire': 'acquire',
'release': 'release',},
'pycsc': {
'module': 'webscard.implementations.pycsc',
'classname': 'PyCSC',},
'empty': {
'module': 'webscard.implementations.empty',
'classname': 'Empty',},
})
# And finally, functions that really make sense in our context
def getimplementations(self):
""" Return a list of the implementations in the current server """
return self['internal'].get('implementations', ['pyscard'])
def gethost(self):
""" The interface where the server is published """
return self['web'].get('host', '0.0.0.0')
def getport(self):
"""
Port on which we runs
It is interesting to set it random if we have Bonjour enabled.
"""
if self.port is not None:
return self.port
if self['web'].get('randomport', False):
# Let's pray the port will indeed be free ...
self.port = random.randint(49152, 65535)
else:
self.port = self['web'].get('port', 3333)
return self.port
def getcookiesecret(self):
""" Secret key that secure the sessions inside the cookies """
return self['cookies'].get('secret', self.defaultsecret)