-
Notifications
You must be signed in to change notification settings - Fork 26
/
meta.py
171 lines (116 loc) · 4.73 KB
/
meta.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
import datetime
from .serialization import CompoundJSONDict, PickledDict, SerializedDict
class GeoMapping(PickledDict):
"""A dictionary that maps location codes to integers. Needed because parameter arrays have integer ``geo`` fields.
File data is stored in ``geomapping.pickle``.
This dictionary does not support setting items directly; instead, use the ``add`` method to add multiple keys."""
filename = "geomapping.pickle"
def __init__(self, *args, **kwargs):
super(GeoMapping, self).__init__(*args, **kwargs)
# At a minimum, "GLO" should always be present
if "GLO" not in self:
self.add(["GLO"])
def add(self, keys):
"""Add a set of keys. These keys can already be in the mapping; only new keys will be added.
Args:
* *keys* (list): The keys to add.
"""
index = max(self.data.values()) if self.data else 0
for i, key in enumerate(keys):
if key not in self.data:
self.data[key] = index + i + 1
self.flush()
def delete(self, keys):
"""Delete a set of keys.
Args:
*keys* (list): The keys to delete.
"""
for key in keys:
del self.data[key]
self.flush()
def __setitem__(self, key, value):
raise NotImplementedError
def __str__(self):
return "Mapping from databases and methods to parameter indices."
def __len__(self):
return len(self.data)
class Databases(SerializedDict):
"""A dictionary for database metadata. This class includes methods to manage database versions. File data is saved in ``databases.json``."""
filename = "databases.json"
def increment_version(self, database, number=None):
"""Increment the ``database`` version. Returns the new version."""
self.data[database]["version"] += 1
if number is not None:
self.data[database]["number"] = number
self.flush()
return self.data[database]["version"]
def version(self, database):
"""Return the ``database`` version"""
return self.data[database].get("version")
def set_modified(self, database):
self[database]["modified"] = datetime.datetime.now().isoformat()
self.flush()
def set_dirty(self, database):
self.set_modified(database)
if self[database].get("dirty"):
pass
else:
self[database]["dirty"] = True
self.flush()
def clean(self):
from . import Database
def _clean():
for x in self:
if self[x].get("dirty"):
Database(x).process()
del self[x]["dirty"]
self.flush()
if not any(self[x].get("dirty") for x in self):
return
else:
return _clean()
def __delitem__(self, name):
from . import Database
try:
Database(name).delete(warn=False)
except:
pass
super(Databases, self).__delitem__(name)
class CalculationSetups(PickledDict):
"""A dictionary for calculation setups.
Keys:
* `inv`: List of functional units, e.g. ``[{(key): amount}, {(key): amount}]``
* `ia`: List of LCIA methods, e.g. ``[(method), (method)]``.
"""
filename = "setups.pickle"
class DynamicCalculationSetups(PickledDict):
"""A dictionary for Dynamic calculation setups.
Keys:
* `inv`: List of functional units, e.g. ``[{(key): amount}, {(key): amount}]``
* `ia`: Dictionary of orst case LCIA method and the relative dynamic LCIA method, e.g. `` [{dLCIA_method_1_worstcase:dLCIA_method_1 , dLCIA_method_2_worstcase:dLCIA_method_2}]``.
"""
filename = "dynamicsetups.pickle"
class Methods(CompoundJSONDict):
"""A dictionary for method metadata. File data is saved in ``methods.json``."""
filename = "methods.json"
class WeightingMeta(Methods):
"""A dictionary for weighting metadata. File data is saved in ``methods.json``."""
filename = "weightings.json"
class NormalizationMeta(Methods):
"""A dictionary for normalization metadata. File data is saved in ``methods.json``."""
filename = "normalizations.json"
class Preferences(PickledDict):
"""A dictionary of project-specific preferences."""
filename = "preferences.pickle"
def __init__(self, *args, **kwargs):
super(Preferences, self).__init__(*args, **kwargs)
# Default preferences
if "use_cache" not in self:
self["use_cache"] = True
geomapping = GeoMapping()
methods = Methods()
normalizations = NormalizationMeta()
preferences = Preferences()
weightings = WeightingMeta()
calculation_setups = CalculationSetups()
dynamic_calculation_setups = DynamicCalculationSetups()