ods-tools 3.2.1__tar.gz → 3.2.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {ods_tools-3.2.1 → ods_tools-3.2.3}/PKG-INFO +1 -1
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools/__init__.py +1 -1
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools/main.py +31 -5
- ods_tools-3.2.3/ods_tools/odtf/__init__.py +6 -0
- ods_tools-3.2.3/ods_tools/odtf/config.py +585 -0
- ods_tools-3.2.3/ods_tools/odtf/connector/__init__.py +11 -0
- ods_tools-3.2.3/ods_tools/odtf/connector/base.py +64 -0
- ods_tools-3.2.3/ods_tools/odtf/connector/csv.py +100 -0
- ods_tools-3.2.3/ods_tools/odtf/controller.py +133 -0
- ods_tools-3.2.3/ods_tools/odtf/data/__init__.py +50 -0
- ods_tools-3.2.3/ods_tools/odtf/errors.py +10 -0
- ods_tools-3.2.3/ods_tools/odtf/files/__init__.py +0 -0
- ods_tools-3.2.3/ods_tools/odtf/files/csv.py +43 -0
- ods_tools-3.2.3/ods_tools/odtf/files/yaml.py +26 -0
- ods_tools-3.2.3/ods_tools/odtf/log.py +40 -0
- ods_tools-3.2.3/ods_tools/odtf/mapping/__init__.py +10 -0
- ods_tools-3.2.3/ods_tools/odtf/mapping/base.py +298 -0
- ods_tools-3.2.3/ods_tools/odtf/mapping/errors.py +28 -0
- ods_tools-3.2.3/ods_tools/odtf/mapping/file.py +572 -0
- ods_tools-3.2.3/ods_tools/odtf/notset.py +14 -0
- ods_tools-3.2.3/ods_tools/odtf/runner/__init__.py +8 -0
- ods_tools-3.2.3/ods_tools/odtf/runner/base.py +304 -0
- ods_tools-3.2.3/ods_tools/odtf/runner/pandas.py +448 -0
- ods_tools-3.2.3/ods_tools/odtf/transformers/__init__.py +6 -0
- ods_tools-3.2.3/ods_tools/odtf/transformers/errors.py +21 -0
- ods_tools-3.2.3/ods_tools/odtf/transformers/grammar.py +78 -0
- ods_tools-3.2.3/ods_tools/odtf/transformers/transform.py +607 -0
- ods_tools-3.2.3/ods_tools/odtf/transformers/transform_utils.py +44 -0
- ods_tools-3.2.3/ods_tools/odtf/validator_base.py +198 -0
- ods_tools-3.2.3/ods_tools/odtf/validator_pandas.py +106 -0
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools/oed/oed_schema.py +20 -2
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools/oed/setting_schema.py +27 -2
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools.egg-info/PKG-INFO +1 -1
- ods_tools-3.2.3/ods_tools.egg-info/SOURCES.txt +46 -0
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools.egg-info/requires.txt +3 -0
- {ods_tools-3.2.1 → ods_tools-3.2.3}/setup.py +1 -1
- ods_tools-3.2.1/ods_tools.egg-info/SOURCES.txt +0 -19
- {ods_tools-3.2.1 → ods_tools-3.2.3}/README.md +0 -0
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools/data/__init__.py +0 -0
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools/oed/__init__.py +0 -0
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools/oed/common.py +0 -0
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools/oed/exposure.py +0 -0
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools/oed/forex.py +0 -0
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools/oed/source.py +0 -0
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools/oed/validator.py +0 -0
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools.egg-info/dependency_links.txt +0 -0
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools.egg-info/entry_points.txt +0 -0
- {ods_tools-3.2.1 → ods_tools-3.2.3}/ods_tools.egg-info/top_level.txt +0 -0
- {ods_tools-3.2.1 → ods_tools-3.2.3}/setup.cfg +0 -0
|
@@ -18,6 +18,7 @@ from ods_tools.oed import (
|
|
|
18
18
|
ModelSettingSchema,
|
|
19
19
|
AnalysisSettingSchema,
|
|
20
20
|
)
|
|
21
|
+
from ods_tools.odtf.controller import transform_format
|
|
21
22
|
|
|
22
23
|
|
|
23
24
|
def get_oed_exposure(config_json=None, oed_dir=None, **kwargs):
|
|
@@ -92,9 +93,27 @@ def convert(**kwargs):
|
|
|
92
93
|
oed_exposure.save(path=path, **kwargs)
|
|
93
94
|
|
|
94
95
|
|
|
96
|
+
def transform(**kwargs):
|
|
97
|
+
"""Wrapper function for transform command.
|
|
98
|
+
Transform location and account data to a new format (ex: AIR to OED)"""
|
|
99
|
+
path_to_config_file = kwargs['config_file']
|
|
100
|
+
try:
|
|
101
|
+
transform_result = transform_format(path_to_config_file)
|
|
102
|
+
if not kwargs.get('nocheck'):
|
|
103
|
+
for output_file in transform_result:
|
|
104
|
+
if output_file[1] == 'location' and os.path.isfile(output_file[0]):
|
|
105
|
+
check(location=output_file[0])
|
|
106
|
+
elif output_file[1] == 'account' and os.path.isfile(output_file[0]):
|
|
107
|
+
check(account=output_file[0])
|
|
108
|
+
except OdsException as e:
|
|
109
|
+
logger.error("Transformation failed:")
|
|
110
|
+
logger.error(e)
|
|
111
|
+
|
|
112
|
+
|
|
95
113
|
command_action = {
|
|
96
114
|
'check': check,
|
|
97
115
|
'convert': convert,
|
|
116
|
+
'transform': transform,
|
|
98
117
|
}
|
|
99
118
|
|
|
100
119
|
|
|
@@ -150,15 +169,22 @@ check_command.add_argument('-v', '--logging-level', help='logging level (debug:1
|
|
|
150
169
|
default=30, type=int)
|
|
151
170
|
|
|
152
171
|
|
|
172
|
+
transform_description = """
|
|
173
|
+
Transform data format to/from OED.
|
|
174
|
+
"""
|
|
175
|
+
transform_command = command_parser.add_parser('transform', description=transform_description,
|
|
176
|
+
formatter_class=argparse.RawTextHelpFormatter)
|
|
177
|
+
transform_command.add_argument('--config-file', help='Path to the config file', required=True)
|
|
178
|
+
transform_command.add_argument('-v', '--logging-level', help='logging level (debug:10, info:20, warning:30, error:40, critical:50)',
|
|
179
|
+
default=30, type=int)
|
|
180
|
+
transform_command.add_argument('--nocheck', help='if True, OED file will not be checked after transformation', default=False)
|
|
181
|
+
|
|
182
|
+
|
|
153
183
|
def main():
|
|
154
184
|
"""command line interface for ODS conversion between csv and parquet"""
|
|
155
185
|
kwargs = vars(main_parser.parse_args())
|
|
156
|
-
ch = logging.StreamHandler()
|
|
157
|
-
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
|
158
|
-
ch.setFormatter(formatter)
|
|
159
|
-
logger.addHandler(ch)
|
|
160
186
|
logging_level = kwargs.pop('logging_level')
|
|
161
|
-
|
|
187
|
+
logging.basicConfig(level=logging_level, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
162
188
|
|
|
163
189
|
command_action[kwargs.pop('command')](** kwargs)
|
|
164
190
|
|
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import re
|
|
3
|
+
from functools import reduce
|
|
4
|
+
from itertools import chain
|
|
5
|
+
from typing import Any, Dict, Iterable, List, Tuple, TypeVar, Union
|
|
6
|
+
|
|
7
|
+
import yaml
|
|
8
|
+
|
|
9
|
+
from .errors import ConfigurationError
|
|
10
|
+
from .files.yaml import read_yaml
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
T = TypeVar("T")
|
|
14
|
+
ConfigSource = Dict[str, Any]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class NotFoundType:
|
|
18
|
+
def get(self, *args, **kwargs):
|
|
19
|
+
return self
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
NotFound = NotFoundType()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def deep_merge_dictionary_items(
|
|
26
|
+
first: Union[Dict, "Config"], second: Union[Dict, "Config"]
|
|
27
|
+
) -> ConfigSource:
|
|
28
|
+
"""
|
|
29
|
+
Merges the 2 dictionary entries, the process is as follows:
|
|
30
|
+
|
|
31
|
+
1. If the element from the first dictionary is not found use the
|
|
32
|
+
element from the second independent from type
|
|
33
|
+
2. If the element in the second dictionary is not a dictionary and has
|
|
34
|
+
been found stop here and use it
|
|
35
|
+
3. If the second element isn't found and the first has use the first
|
|
36
|
+
value
|
|
37
|
+
4. If both elements have been found then use the merge of the 2
|
|
38
|
+
|
|
39
|
+
:param first: The first dict or the value from the first dict to try
|
|
40
|
+
to merge
|
|
41
|
+
:param second: The second dict or the value from the second dict to
|
|
42
|
+
try to merge
|
|
43
|
+
|
|
44
|
+
:return: The merged result
|
|
45
|
+
"""
|
|
46
|
+
if isinstance(first, Config):
|
|
47
|
+
first = first.config
|
|
48
|
+
|
|
49
|
+
if isinstance(second, Config):
|
|
50
|
+
second = second.config
|
|
51
|
+
|
|
52
|
+
if first is NotFound or (
|
|
53
|
+
second is not NotFound and not isinstance(second, dict)
|
|
54
|
+
):
|
|
55
|
+
# if the first value was not found or the second value has been
|
|
56
|
+
# found and is not a dictionary, use it
|
|
57
|
+
return second
|
|
58
|
+
elif first is not NotFound and second is NotFound:
|
|
59
|
+
# else if we have no value from second and first is found use it
|
|
60
|
+
return first
|
|
61
|
+
else:
|
|
62
|
+
# otherwise, continue merging deeper
|
|
63
|
+
return {
|
|
64
|
+
key: deep_merge_dictionary_items(
|
|
65
|
+
first.get(key, NotFound), second.get(key, NotFound)
|
|
66
|
+
)
|
|
67
|
+
for key in chain(first.keys(), second.keys())
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def get_json_path(
|
|
72
|
+
config: Dict[str, Any], path: str, fallback: Any = NotFound
|
|
73
|
+
) -> Any:
|
|
74
|
+
"""
|
|
75
|
+
Gets a property from the configuration from it's path in the config.
|
|
76
|
+
The path should be dotted path into the config. For example, with the
|
|
77
|
+
config::
|
|
78
|
+
|
|
79
|
+
{
|
|
80
|
+
"foo": {
|
|
81
|
+
"bar": "baz"
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
The path `"foo.bar"` would return `"baz"` and the path `"foo"` would
|
|
86
|
+
return `{"bar": "baz"}`.
|
|
87
|
+
|
|
88
|
+
If the path isn't found in the config the `fallback` value is used if
|
|
89
|
+
one is provided, if no fallback is provided a `KeyError` is raised.
|
|
90
|
+
|
|
91
|
+
:param path: The path of the requested value into the config.
|
|
92
|
+
:param fallback: The value to use if the path isn't found.
|
|
93
|
+
|
|
94
|
+
:return: The found path ot fallback if provided
|
|
95
|
+
"""
|
|
96
|
+
res = reduce(
|
|
97
|
+
lambda conf, path_part: conf.get(path_part, NotFound),
|
|
98
|
+
path.lower().split("."),
|
|
99
|
+
config,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
if res is not NotFound:
|
|
103
|
+
return res
|
|
104
|
+
|
|
105
|
+
if fallback is not NotFound:
|
|
106
|
+
return fallback
|
|
107
|
+
|
|
108
|
+
raise KeyError(path)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class Config:
|
|
112
|
+
"""
|
|
113
|
+
Configuration class that loads the config file, environment,
|
|
114
|
+
command line and overrides and merges them into a single map.
|
|
115
|
+
"""
|
|
116
|
+
|
|
117
|
+
TEMPLATE_TRANSFORMATION_PATH = "template_transformation"
|
|
118
|
+
TRANSFORMATIONS_PATH = "transformations"
|
|
119
|
+
ACC_TRANSFORMATION_LABEL = "acc"
|
|
120
|
+
ACC_TRANSFORMATION_PATH = (
|
|
121
|
+
f"{TRANSFORMATIONS_PATH}.{ACC_TRANSFORMATION_LABEL}"
|
|
122
|
+
)
|
|
123
|
+
LOC_TRANSFORMATION_LABEL = "loc"
|
|
124
|
+
LOC_TRANSFORMATION_PATH = (
|
|
125
|
+
f"{TRANSFORMATIONS_PATH}.{LOC_TRANSFORMATION_LABEL}"
|
|
126
|
+
)
|
|
127
|
+
RI_TRANSFORMATION_LABEL = "ri"
|
|
128
|
+
RI_TRANSFORMATION_PATH = (
|
|
129
|
+
f"{TRANSFORMATIONS_PATH}.{RI_TRANSFORMATION_LABEL}"
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
TRANSFORMATION_PATH_SUB = re.compile(r"^transformations\.[^.]+")
|
|
133
|
+
|
|
134
|
+
def __init__(
|
|
135
|
+
self,
|
|
136
|
+
argv: Dict[str, Any] = None,
|
|
137
|
+
config_path: str = None,
|
|
138
|
+
overrides: Dict[str, Any] = None,
|
|
139
|
+
env: Dict[str, str] = None,
|
|
140
|
+
):
|
|
141
|
+
self.path = (
|
|
142
|
+
os.path.abspath(config_path)
|
|
143
|
+
if config_path and os.path.exists(config_path)
|
|
144
|
+
else None
|
|
145
|
+
)
|
|
146
|
+
self.argv = argv
|
|
147
|
+
self.env = env
|
|
148
|
+
self.overrides = overrides
|
|
149
|
+
self.config = self.merge_config_sources(
|
|
150
|
+
*self.get_config_sources(
|
|
151
|
+
config_path=config_path,
|
|
152
|
+
env=env,
|
|
153
|
+
argv=argv,
|
|
154
|
+
overrides=overrides,
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
def __eq__(self, other):
|
|
159
|
+
return self.config in [other, getattr(other, "config", {})]
|
|
160
|
+
|
|
161
|
+
def __bool__(self):
|
|
162
|
+
return bool(self.config)
|
|
163
|
+
|
|
164
|
+
def get_config_sources(
|
|
165
|
+
self,
|
|
166
|
+
config_path: str = None,
|
|
167
|
+
env: Dict[str, str] = None,
|
|
168
|
+
argv: Dict[str, Any] = None,
|
|
169
|
+
overrides: ConfigSource = None,
|
|
170
|
+
) -> Iterable[ConfigSource]:
|
|
171
|
+
"""
|
|
172
|
+
Returns all the provided config sources in order of increasing
|
|
173
|
+
precedence, the order is:
|
|
174
|
+
|
|
175
|
+
1. Config file
|
|
176
|
+
2. Environment
|
|
177
|
+
3. Command line
|
|
178
|
+
4. Hard coded overrides
|
|
179
|
+
|
|
180
|
+
:param config_path: The path to the config file
|
|
181
|
+
:param env: The systems environment dict
|
|
182
|
+
:param argv: Dict of config paths to values
|
|
183
|
+
:param overrides: hard coded override values for the config
|
|
184
|
+
|
|
185
|
+
:return: An iterable of normalised configs from the various sources
|
|
186
|
+
"""
|
|
187
|
+
if config_path and os.path.exists(config_path):
|
|
188
|
+
yield self.normalise_property_names(read_yaml(config_path))
|
|
189
|
+
|
|
190
|
+
if env:
|
|
191
|
+
yield self.normalise_property_names(self.process_env_options(env))
|
|
192
|
+
|
|
193
|
+
if argv:
|
|
194
|
+
yield self.normalise_property_names(self.process_cli_options(argv))
|
|
195
|
+
|
|
196
|
+
if overrides:
|
|
197
|
+
yield self.normalise_property_names(overrides)
|
|
198
|
+
|
|
199
|
+
def _get_config_dict_from_path_value_pairs(
|
|
200
|
+
self,
|
|
201
|
+
paths_and_values,
|
|
202
|
+
separator,
|
|
203
|
+
):
|
|
204
|
+
conf = {}
|
|
205
|
+
for path, value in paths_and_values:
|
|
206
|
+
*parts, final = path.lower().split(separator)
|
|
207
|
+
reduce(lambda c, part: c.setdefault(part, {}), parts, conf)[
|
|
208
|
+
final
|
|
209
|
+
] = value
|
|
210
|
+
|
|
211
|
+
return conf
|
|
212
|
+
|
|
213
|
+
def process_cli_options(self, argv):
|
|
214
|
+
return self._get_config_dict_from_path_value_pairs(argv.items(), ".")
|
|
215
|
+
|
|
216
|
+
def process_env_options(self, env):
|
|
217
|
+
env_search = "CONVERTER_".lower()
|
|
218
|
+
|
|
219
|
+
return self._get_config_dict_from_path_value_pairs(
|
|
220
|
+
(
|
|
221
|
+
(
|
|
222
|
+
k.lower().replace(env_search, ""),
|
|
223
|
+
yaml.load(v, yaml.SafeLoader),
|
|
224
|
+
)
|
|
225
|
+
for k, v in env.items()
|
|
226
|
+
if k.lower().startswith(env_search)
|
|
227
|
+
),
|
|
228
|
+
"_",
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
@classmethod
|
|
232
|
+
def validate(cls, config_path: str):
|
|
233
|
+
"""
|
|
234
|
+
Basic validation check of the config file.
|
|
235
|
+
"""
|
|
236
|
+
config = read_yaml(config_path)
|
|
237
|
+
# If transformations key not in yaml file flag as invalid.
|
|
238
|
+
if 'transformations' not in config:
|
|
239
|
+
raise ConfigurationError("Invalid config file - transformation key required")
|
|
240
|
+
|
|
241
|
+
@classmethod
|
|
242
|
+
def merge_config_sources(
|
|
243
|
+
cls,
|
|
244
|
+
first: ConfigSource = None,
|
|
245
|
+
second: ConfigSource = None,
|
|
246
|
+
*others: ConfigSource,
|
|
247
|
+
) -> ConfigSource:
|
|
248
|
+
"""
|
|
249
|
+
Merges any number of config sources the later sources taking precedence
|
|
250
|
+
over the former.
|
|
251
|
+
|
|
252
|
+
:param first: The first source
|
|
253
|
+
:param second: The second source
|
|
254
|
+
:param others: The other sources
|
|
255
|
+
|
|
256
|
+
:return: The merged config source
|
|
257
|
+
"""
|
|
258
|
+
first = first or {}
|
|
259
|
+
second = second or {}
|
|
260
|
+
|
|
261
|
+
merged = deep_merge_dictionary_items(first, second)
|
|
262
|
+
|
|
263
|
+
if others:
|
|
264
|
+
return cls.merge_config_sources(merged, *others)
|
|
265
|
+
|
|
266
|
+
return merged
|
|
267
|
+
|
|
268
|
+
def normalise_property_names(self, d: ConfigSource) -> ConfigSource:
|
|
269
|
+
"""
|
|
270
|
+
Converts all names in all levels of the dictionary to lowercase
|
|
271
|
+
|
|
272
|
+
:param d: The dictionary to normalise
|
|
273
|
+
|
|
274
|
+
:return: The normalised dictionary
|
|
275
|
+
"""
|
|
276
|
+
return dict(map(lambda kv: self.normalise_property(*kv), d.items()))
|
|
277
|
+
|
|
278
|
+
def normalise_property(self, key: str, value: Any) -> Tuple[str, Any]:
|
|
279
|
+
"""
|
|
280
|
+
Gets the normalised (lowercase) name with the normalised value.
|
|
281
|
+
If the value is a dictionary it is normalised otherwise the value
|
|
282
|
+
is returned unchanged.
|
|
283
|
+
|
|
284
|
+
:param key: The key name for the entry
|
|
285
|
+
:param value: The value for the entry
|
|
286
|
+
|
|
287
|
+
:return: A 2-tuple of the normalised key and value of the entry
|
|
288
|
+
"""
|
|
289
|
+
if isinstance(value, dict):
|
|
290
|
+
return key.lower(), self.normalise_property_names(value)
|
|
291
|
+
else:
|
|
292
|
+
return key.lower(), value
|
|
293
|
+
|
|
294
|
+
def get(self, path: str, fallback: Any = NotFound) -> Any:
|
|
295
|
+
"""
|
|
296
|
+
Gets a property from the configuration from it's path in the config.
|
|
297
|
+
The path should be dotted path into the config. For example, with the
|
|
298
|
+
config::
|
|
299
|
+
|
|
300
|
+
{
|
|
301
|
+
"foo": {
|
|
302
|
+
"bar": "baz"
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
The path `"foo.bar"` would return `"baz"` and the path `"foo"` would
|
|
307
|
+
return `{"bar": "baz"}`.
|
|
308
|
+
|
|
309
|
+
If the path isn't found in the config the `fallback` value is used if
|
|
310
|
+
one is provided, if no fallback is provided a `KeyError` is raised.
|
|
311
|
+
|
|
312
|
+
:param path: The path of the requested value into the config.
|
|
313
|
+
:param fallback: The value to use if the path isn't found.
|
|
314
|
+
|
|
315
|
+
:return: The found path ot fallback if provided
|
|
316
|
+
"""
|
|
317
|
+
return get_json_path(self.config, path, fallback=fallback)
|
|
318
|
+
|
|
319
|
+
def get_template_resolved_value(self, path: str, fallback: Any = NotFound):
|
|
320
|
+
"""
|
|
321
|
+
Gets the value for a given path reverting to the transformation
|
|
322
|
+
template if the value is not found.
|
|
323
|
+
|
|
324
|
+
:param path: The path of the requested value into the config.
|
|
325
|
+
:param fallback: The value to use if the path isn't found.
|
|
326
|
+
|
|
327
|
+
:return: The found path ot fallback if provided
|
|
328
|
+
"""
|
|
329
|
+
|
|
330
|
+
try:
|
|
331
|
+
return self.get(path)
|
|
332
|
+
except KeyError:
|
|
333
|
+
pass
|
|
334
|
+
|
|
335
|
+
try:
|
|
336
|
+
template_property_path = self.TRANSFORMATION_PATH_SUB.sub(
|
|
337
|
+
self.TEMPLATE_TRANSFORMATION_PATH,
|
|
338
|
+
path,
|
|
339
|
+
)
|
|
340
|
+
return self.get(template_property_path)
|
|
341
|
+
except KeyError:
|
|
342
|
+
pass
|
|
343
|
+
|
|
344
|
+
if fallback is not NotFound:
|
|
345
|
+
return fallback
|
|
346
|
+
|
|
347
|
+
raise KeyError(path)
|
|
348
|
+
|
|
349
|
+
def set(self, path: str, value: Any):
|
|
350
|
+
"""
|
|
351
|
+
Sets a property in the configuration by it's path in the config.
|
|
352
|
+
The path should be dotted path into the config. For example, setting
|
|
353
|
+
the path `"foo.bar"` to `"baz"` the following config will be created::
|
|
354
|
+
|
|
355
|
+
{
|
|
356
|
+
"foo": {
|
|
357
|
+
"bar": "baz"
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
:param path: The path of the value to set.
|
|
362
|
+
:param value: The value to set.
|
|
363
|
+
"""
|
|
364
|
+
block = self.config
|
|
365
|
+
|
|
366
|
+
path_parts = path.lower().split(".")
|
|
367
|
+
for path_part in path_parts[:-1]:
|
|
368
|
+
block = block.setdefault(path_part, {})
|
|
369
|
+
|
|
370
|
+
block[path_parts[-1]] = value
|
|
371
|
+
|
|
372
|
+
def delete(self, path):
|
|
373
|
+
"""
|
|
374
|
+
Removes a specific path from the config
|
|
375
|
+
|
|
376
|
+
:param path: The path to remove.
|
|
377
|
+
"""
|
|
378
|
+
block = self.config
|
|
379
|
+
|
|
380
|
+
path_parts = path.lower().split(".")
|
|
381
|
+
for path_part in path_parts[:-1]:
|
|
382
|
+
if path_part not in block:
|
|
383
|
+
return
|
|
384
|
+
block = block[path_part]
|
|
385
|
+
|
|
386
|
+
if path_parts[-1] in block:
|
|
387
|
+
del block[path_parts[-1]]
|
|
388
|
+
|
|
389
|
+
def to_yaml(self):
|
|
390
|
+
"""
|
|
391
|
+
Generates a yaml string representation of the normalised config
|
|
392
|
+
|
|
393
|
+
:return: The yaml representation
|
|
394
|
+
"""
|
|
395
|
+
return yaml.safe_dump(self.config)
|
|
396
|
+
|
|
397
|
+
def absolute_path(self, p):
|
|
398
|
+
"""
|
|
399
|
+
Gets the absolute path relative to the config files directory.
|
|
400
|
+
|
|
401
|
+
:param p: The path relative to the config file
|
|
402
|
+
|
|
403
|
+
:return: The absolute path if both the config path and `p` are set,
|
|
404
|
+
if `p` is `None`, `None` is returned. If the config path is `None`,
|
|
405
|
+
`p` is returned without modification
|
|
406
|
+
"""
|
|
407
|
+
if p is not None:
|
|
408
|
+
return os.path.abspath(
|
|
409
|
+
os.path.join(os.path.dirname(self.path or "."), p)
|
|
410
|
+
)
|
|
411
|
+
|
|
412
|
+
return p
|
|
413
|
+
|
|
414
|
+
def save(self, new_filename=None):
|
|
415
|
+
"""
|
|
416
|
+
Writes the configuration to a yaml file.
|
|
417
|
+
|
|
418
|
+
:param new_filename: The filename to use. If not set the current
|
|
419
|
+
filename in the configuration is used.
|
|
420
|
+
"""
|
|
421
|
+
with open(new_filename or self.path, "w") as f:
|
|
422
|
+
f.write(self.to_yaml())
|
|
423
|
+
|
|
424
|
+
def keys(self):
|
|
425
|
+
"""
|
|
426
|
+
Gets an iterable keys.
|
|
427
|
+
"""
|
|
428
|
+
return self.config.keys()
|
|
429
|
+
|
|
430
|
+
def items(self):
|
|
431
|
+
"""
|
|
432
|
+
Gets an iterable of (key, value) tuples.
|
|
433
|
+
"""
|
|
434
|
+
return self.config.items()
|
|
435
|
+
|
|
436
|
+
def get_transformation_configs(self) -> List["TransformationConfig"]:
|
|
437
|
+
"""
|
|
438
|
+
Generates all the configs for running specific transformations
|
|
439
|
+
resolved with the template transformation
|
|
440
|
+
|
|
441
|
+
:return: A list of transformation configs
|
|
442
|
+
"""
|
|
443
|
+
return [
|
|
444
|
+
TransformationConfig(self, file_type)
|
|
445
|
+
for file_type in self.get(
|
|
446
|
+
self.TRANSFORMATIONS_PATH, fallback={}
|
|
447
|
+
).keys()
|
|
448
|
+
]
|
|
449
|
+
|
|
450
|
+
@property
|
|
451
|
+
def has_template(self) -> bool:
|
|
452
|
+
"""
|
|
453
|
+
Checks if the config has a template transformation
|
|
454
|
+
"""
|
|
455
|
+
return self.TEMPLATE_TRANSFORMATION_PATH in self
|
|
456
|
+
|
|
457
|
+
@property
|
|
458
|
+
def has_acc(self):
|
|
459
|
+
"""
|
|
460
|
+
Checks if the config has an account transformation
|
|
461
|
+
"""
|
|
462
|
+
return self.ACC_TRANSFORMATION_PATH in self
|
|
463
|
+
|
|
464
|
+
@property
|
|
465
|
+
def has_loc(self):
|
|
466
|
+
"""
|
|
467
|
+
Checks if the config has a location transformation
|
|
468
|
+
"""
|
|
469
|
+
return self.LOC_TRANSFORMATION_PATH in self
|
|
470
|
+
|
|
471
|
+
@property
|
|
472
|
+
def has_ri(self):
|
|
473
|
+
"""
|
|
474
|
+
Checks if the config has a reinsurance transformation
|
|
475
|
+
"""
|
|
476
|
+
return self.RI_TRANSFORMATION_PATH in self
|
|
477
|
+
|
|
478
|
+
def uses_template_value(self, property_path):
|
|
479
|
+
"""
|
|
480
|
+
Checks if a given path uses the value from the template transformation.
|
|
481
|
+
"""
|
|
482
|
+
template_property_path = self.TRANSFORMATION_PATH_SUB.sub(
|
|
483
|
+
self.TEMPLATE_TRANSFORMATION_PATH,
|
|
484
|
+
property_path,
|
|
485
|
+
)
|
|
486
|
+
try:
|
|
487
|
+
template_value = self.get(template_property_path)
|
|
488
|
+
except KeyError:
|
|
489
|
+
# if the property isn't in the template then we cant be
|
|
490
|
+
# using the template value
|
|
491
|
+
return False
|
|
492
|
+
|
|
493
|
+
try:
|
|
494
|
+
config_value = self.get(property_path)
|
|
495
|
+
except KeyError:
|
|
496
|
+
# if the property is in the template but not in the config
|
|
497
|
+
# we must be using the template path
|
|
498
|
+
return True
|
|
499
|
+
|
|
500
|
+
# if it is set in both specify we are using the template if
|
|
501
|
+
# the 2 values are the same
|
|
502
|
+
return template_value == config_value
|
|
503
|
+
|
|
504
|
+
def __contains__(self, item):
|
|
505
|
+
try:
|
|
506
|
+
self.get(item)
|
|
507
|
+
return True
|
|
508
|
+
except KeyError:
|
|
509
|
+
return False
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
class TransformationConfig:
|
|
513
|
+
def __init__(self, config: Config, file_type: str):
|
|
514
|
+
self.root_config = config
|
|
515
|
+
self.file_type = file_type
|
|
516
|
+
|
|
517
|
+
# merge the template transformation with the overrides
|
|
518
|
+
self.config = deep_merge_dictionary_items(
|
|
519
|
+
self.root_config.get(
|
|
520
|
+
self.root_config.TEMPLATE_TRANSFORMATION_PATH, fallback={}
|
|
521
|
+
),
|
|
522
|
+
self.root_config.get(
|
|
523
|
+
f"{self.root_config.TRANSFORMATIONS_PATH}.{file_type}",
|
|
524
|
+
fallback={},
|
|
525
|
+
),
|
|
526
|
+
)
|
|
527
|
+
|
|
528
|
+
def absolute_path(self, p):
|
|
529
|
+
"""
|
|
530
|
+
Gets the absolute path relative to the config files directory.
|
|
531
|
+
|
|
532
|
+
:param p: The path relative to the config file
|
|
533
|
+
|
|
534
|
+
:return: The absolute path if both the config path and `p` are set,
|
|
535
|
+
if `p` is `None`, `None` is returned. If the config path is `None`,
|
|
536
|
+
`p` is returned without modification
|
|
537
|
+
"""
|
|
538
|
+
return self.root_config.absolute_path(p)
|
|
539
|
+
|
|
540
|
+
def keys(self):
|
|
541
|
+
"""
|
|
542
|
+
Gets an iterable keys.
|
|
543
|
+
"""
|
|
544
|
+
return self.config.keys()
|
|
545
|
+
|
|
546
|
+
def items(self):
|
|
547
|
+
"""
|
|
548
|
+
Gets an iterable of (key, value) tuples.
|
|
549
|
+
"""
|
|
550
|
+
return self.config.items()
|
|
551
|
+
|
|
552
|
+
@property
|
|
553
|
+
def path(self):
|
|
554
|
+
"""
|
|
555
|
+
Returns the path of the root config
|
|
556
|
+
"""
|
|
557
|
+
return self.root_config.path
|
|
558
|
+
|
|
559
|
+
def get(self, path: str, fallback: Any = NotFound) -> Any:
|
|
560
|
+
"""
|
|
561
|
+
Gets a property from the configuration from it's path in the config.
|
|
562
|
+
The path should be dotted path into the config. For example, with the
|
|
563
|
+
config::
|
|
564
|
+
|
|
565
|
+
{
|
|
566
|
+
"foo": {
|
|
567
|
+
"bar": "baz"
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
The path `"foo.bar"` would return `"baz"` and the path `"foo"` would
|
|
572
|
+
return `{"bar": "baz"}`.
|
|
573
|
+
|
|
574
|
+
If the path isn't found in the config the `fallback` value is used if
|
|
575
|
+
one is provided, if no fallback is provided a `KeyError` is raised.
|
|
576
|
+
|
|
577
|
+
:param path: The path of the requested value into the config.
|
|
578
|
+
:param fallback: The value to use if the path isn't found.
|
|
579
|
+
|
|
580
|
+
:return: The found path ot fallback if provided
|
|
581
|
+
"""
|
|
582
|
+
return get_json_path(self.config, path, fallback=fallback)
|
|
583
|
+
|
|
584
|
+
def __eq__(self, other):
|
|
585
|
+
return self.config == other.config
|