Repository navigation
Expand file tree
/
Copy pathconfig.py
More file actions
62 lines (46 loc) · 1.64 KB
/
Copy pathconfig.py
File metadata and controls
62 lines (46 loc) · 1.64 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
import json
class NestedConfig:
def __init__(self, name, data):
self.__dict__['name'] = name
self.__dict__['data'] = data
def __getattr__(self, key):
return self.data.get(key)
def __setattr__(self, key, value):
self.data[key] = value
def __len__(self):
return len(self.__dict__['data'])
def __repr__(self):
return json.dumps(self.data, indent=4)
class ConfigMgr:
def __init__(self):
self._initialised = False
def _load_option(self):
self._initialised = True # prevent infinite loop
self.options = {}
# Initialise defaults
n = self.__getattr__('network')
n.proxy = None
n.retries = 0
n.cache_ttl = 60 # seconds to reuse cached GET responses; 0 = no caching, None = never expire
d = self.__getattr__('debug')
d.hide_exceptions = True
d.logging = False
loc = self.__getattr__('locale')
loc.lang = "en-US" # BCP-47 language tag for Yahoo v7/v10 endpoints
loc.region = "US" # ISO 3166-1 alpha-2 country code
def __getattr__(self, key):
if not self._initialised:
self._load_option()
if key not in self.options:
self.options[key] = {}
return NestedConfig(key, self.options[key])
def __contains__(self, key):
if not self._initialised:
self._load_option()
return key in self.options
def __repr__(self):
if not self._initialised:
self._load_option()
all_options = self.options.copy()
return json.dumps(all_options, indent=4)
YfConfig = ConfigMgr()