-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorators.py
130 lines (93 loc) · 3.5 KB
/
decorators.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
import collections
def lazyprop(func):
"""Caches the class method return value.
Stores the result returned by a class instance's method the first time
the method is invoked, and returns the cached value at every subsequent call.
Used to decorate persistence.SqlStructure.apps_names.
"""
attr_name = '_lazy_' + func.__name__
@property
def _lazyprop(self):
if not hasattr(self, attr_name):
setattr(self, attr_name, func(self))
return getattr(self, attr_name)
return _lazyprop
def memoize_models_in_app(func):
"""Caches the list of models names inside an app.
Used to decorate persistence.SqlStructure.apps_models.
* Arguments:
- the decorated function accepts the Django app's name as argument.
"""
memo={}
def helper(self, app_name):
if app_name not in memo:
memo[app_name] = func(self, app_name)
return memo[app_name]
return helper
def memoize_by_string_kwargs(func):
"""Caches class methods whose kwargs reference hashable types.
Here used to decorate those methods whose signature includes strings,
typically app_name, model_name.
"""
memo={}
def helper(self, **kwargs):
hash_ = hash(str(kwargs))
if hash_ not in memo:
memo[hash_] = func(self, **kwargs)
return memo[hash_]
return helper
class Singleton(type):
"""Singleton boilerplate.
Every class which shall be a Singleton, inherits from this one.
"""
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class SqlStructure(object):
"""Represents the bookkeeper of the database structure.
This class stores the hierarchical structure apps -> models -> fields,
as inferred from the SQL files.
This class is a singleton, loaded once for all when easyConfig boots.
* Public methods:
-
* Instance variables:
-
"""
__metaclass__ = Singleton
def __init__(self):
self._apps_models = {
'config': ['init', 'config', 'configkind', 'meetingquorum', 'votationquorum', 'meetingvalue', 'csvfield'],
'reports': ['report', 'rquery', 'rparams', 'routput'],
'members': ['category'],
'stations': ['prefix', 'site', 'station']
}
self._fields = collections.defaultdict(
lambda: collections.defaultdict(list))
self._fields['votations']['anagvotvalue'] = [
'id',
'record_title',
'timestamp_save',
]
self._fields['directions']['monitor_monitor_votation'] = [
'id',
'monitor_id',
'monitorvotation_id',
]
self._fields['directions']['monitorvotation_votationvalue'] = [
'id',
'monitorvotation_id',
'votationvalue_id',
]
@memoize_models_in_app
def app_models(self, app_name):
"""Returns the list of models MA DEI RECORDSET , DELLE STRUTTURE O DEI NOMI ?????
Remark:
"""
return self._apps_models[app_name]
@memoize_by_string_kwargs
def model_fields(self, app_name, model_name):
return self._fields[app_name][model_name]
if __name__ == "__main__":
sql_structure = SqlStructure()