python-table-converter 0.2.0__tar.gz → 0.2.2__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.
- {python_table_converter-0.2.0 → python_table_converter-0.2.2}/PKG-INFO +1 -1
- {python_table_converter-0.2.0 → python_table_converter-0.2.2}/pyproject.toml +1 -1
- python_table_converter-0.2.2/table_converter/__init__.py +2 -0
- {python_table_converter-0.2.0 → python_table_converter-0.2.2}/table_converter/core/config.py +25 -11
- {python_table_converter-0.2.0 → python_table_converter-0.2.2}/table_converter/core/convert.py +20 -19
- python_table_converter-0.2.0/table_converter/__init__.py +0 -2
- {python_table_converter-0.2.0 → python_table_converter-0.2.2}/LICENSE +0 -0
- {python_table_converter-0.2.0 → python_table_converter-0.2.2}/README.md +0 -0
- {python_table_converter-0.2.0 → python_table_converter-0.2.2}/table_converter/cli.py +0 -0
- {python_table_converter-0.2.0 → python_table_converter-0.2.2}/table_converter/commands/convert_tables.py +0 -0
{python_table_converter-0.2.0 → python_table_converter-0.2.2}/table_converter/core/config.py
RENAMED
|
@@ -10,10 +10,18 @@ import yaml
|
|
|
10
10
|
type FlatFieldMap = Mapping[str, str]
|
|
11
11
|
type FieldMap = Mapping[str, str|FieldMap]
|
|
12
12
|
|
|
13
|
+
@dataclasses.dataclass
|
|
14
|
+
class ProcessConfig:
|
|
15
|
+
assign_constants: FlatFieldMap = dataclasses.field(default_factory=OrderedDict)
|
|
16
|
+
split_by_newline: FlatFieldMap = dataclasses.field(default_factory=OrderedDict)
|
|
17
|
+
|
|
18
|
+
def __setitem__(self, key, value):
|
|
19
|
+
setattr(self, key, value)
|
|
20
|
+
|
|
13
21
|
@dataclasses.dataclass
|
|
14
22
|
class Config:
|
|
15
|
-
|
|
16
|
-
|
|
23
|
+
map: FieldMap = dataclasses.field(default_factory=OrderedDict)
|
|
24
|
+
process: ProcessConfig = dataclasses.field(default_factory=ProcessConfig)
|
|
17
25
|
|
|
18
26
|
def flatten(
|
|
19
27
|
mapping: FieldMap,
|
|
@@ -33,6 +41,7 @@ def flatten(
|
|
|
33
41
|
def setup_config(
|
|
34
42
|
config_path: str | None = None,
|
|
35
43
|
):
|
|
44
|
+
config = Config()
|
|
36
45
|
if config_path:
|
|
37
46
|
if config_path.endswith('.yaml'):
|
|
38
47
|
yaml.add_constructor(
|
|
@@ -40,17 +49,22 @@ def setup_config(
|
|
|
40
49
|
lambda loader, node: OrderedDict(loader.construct_pairs(node)),
|
|
41
50
|
)
|
|
42
51
|
with open(config_path, 'r') as f:
|
|
43
|
-
|
|
44
|
-
#ic(data)
|
|
45
|
-
config = Config(
|
|
46
|
-
#**data,
|
|
47
|
-
map = flatten(data.get('map', {})),
|
|
48
|
-
)
|
|
52
|
+
loaded = yaml.load(f, yaml.Loader)
|
|
49
53
|
else:
|
|
50
54
|
raise ValueError(
|
|
51
55
|
'Only YAML configuration files are supported.'
|
|
52
56
|
)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
57
|
+
ic(loaded)
|
|
58
|
+
if 'map' in loaded:
|
|
59
|
+
config.map = flatten(loaded['map'])
|
|
60
|
+
#if 'process' in loaded:
|
|
61
|
+
dict_process = loaded.get('process')
|
|
62
|
+
if isinstance(dict_process, Mapping):
|
|
63
|
+
for process_key in [
|
|
64
|
+
'assign_constants',
|
|
65
|
+
'split_by_newline',
|
|
66
|
+
]:
|
|
67
|
+
dict_subprocess = dict_process.get(process_key)
|
|
68
|
+
if isinstance(dict_subprocess, Mapping):
|
|
69
|
+
config.process[process_key] = flatten(loaded['process'][process_key])
|
|
56
70
|
return config
|
{python_table_converter-0.2.0 → python_table_converter-0.2.2}/table_converter/core/convert.py
RENAMED
|
@@ -147,15 +147,21 @@ def map_formats(
|
|
|
147
147
|
new_row = OrderedDict(row)
|
|
148
148
|
for column in dict_formats.keys():
|
|
149
149
|
template = dict_formats[column]
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
150
|
+
params = {}
|
|
151
|
+
params.update(row['__debug__'])
|
|
152
|
+
params.update(row)
|
|
153
|
+
formatted = None
|
|
154
|
+
while formatted is None:
|
|
155
|
+
try:
|
|
156
|
+
formatted = template.format(**params)
|
|
157
|
+
except KeyError as e:
|
|
158
|
+
#ic(e)
|
|
159
|
+
#ic(e.args)
|
|
160
|
+
#ic(e.args[0])
|
|
161
|
+
key = e.args[0]
|
|
162
|
+
params[key] = '__undefined__'
|
|
163
|
+
except:
|
|
164
|
+
raise
|
|
159
165
|
set_field_value(new_row, f'__debug__.{column}', formatted)
|
|
160
166
|
return new_row
|
|
161
167
|
|
|
@@ -247,7 +253,6 @@ def convert(
|
|
|
247
253
|
df_list = []
|
|
248
254
|
dict_constants: OrderedDict | None = None
|
|
249
255
|
dict_formats: OrderedDict | None = None
|
|
250
|
-
dict_split_by_newline: OrderedDict | None = None
|
|
251
256
|
dict_assign_ids= None
|
|
252
257
|
root_id_stat = create_id_stat_node()
|
|
253
258
|
config = setup_config(config_path)
|
|
@@ -271,23 +276,19 @@ def convert(
|
|
|
271
276
|
else:
|
|
272
277
|
raise ValueError(f'Invalid template assignment: {field}')
|
|
273
278
|
if pickup_columns:
|
|
274
|
-
if config.map is None:
|
|
275
|
-
config.map = OrderedDict()
|
|
276
279
|
fields = pickup_columns.split(',')
|
|
277
280
|
for field in fields:
|
|
278
281
|
if '=' in field:
|
|
279
282
|
dst, value = field.split('=')
|
|
280
|
-
|
|
283
|
+
config.map[dst] = value
|
|
281
284
|
else:
|
|
282
|
-
|
|
285
|
+
config.map[field] = field
|
|
283
286
|
if fields_to_split_by_newline:
|
|
284
|
-
#list_fields_to_split_by_newline = fields_to_split_by_newline.split(',')
|
|
285
|
-
dict_split_by_newline = OrderedDict()
|
|
286
287
|
fields = fields_to_split_by_newline.split(',')
|
|
287
288
|
for field in fields:
|
|
288
289
|
if '=' in field:
|
|
289
290
|
dst, src = field.split('=')
|
|
290
|
-
|
|
291
|
+
config.process.split_by_newline[dst] = src
|
|
291
292
|
else:
|
|
292
293
|
raise ValueError(f'Invalid split by newline: {field}')
|
|
293
294
|
if fields_to_assign_ids:
|
|
@@ -330,8 +331,8 @@ def convert(
|
|
|
330
331
|
new_row = map_constants(new_row, dict_constants)
|
|
331
332
|
if config.map:
|
|
332
333
|
new_row = remap_columns(new_row, config.map)
|
|
333
|
-
if
|
|
334
|
-
new_row = apply_fields_split_by_newline(new_row,
|
|
334
|
+
if config.process.split_by_newline:
|
|
335
|
+
new_row = apply_fields_split_by_newline(new_row, config.process.split_by_newline)
|
|
335
336
|
if dict_assign_ids:
|
|
336
337
|
new_row = assign_id(new_row, dict_assign_ids, root_id_stat)
|
|
337
338
|
if dict_formats:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|