|
2 | 2 | from flattentool.json_input import JSONParser
|
3 | 3 | from flattentool.output import FORMATS as OUTPUT_FORMATS
|
4 | 4 | from flattentool.output import FORMATS_SUFFIX
|
5 |
| -from flattentool.input import FORMATS as INPUT_FORMATS |
| 5 | +from flattentool.input import FORMATS as INPUT_FORMATS, WITH_CELLS |
6 | 6 | import json
|
7 | 7 | import codecs
|
8 | 8 | from decimal import Decimal
|
@@ -104,7 +104,8 @@ def decimal_default(o):
|
104 | 104 |
|
105 | 105 | def unflatten(input_name, base_json=None, input_format=None, output_name='releases.json',
|
106 | 106 | main_sheet_name='releases', encoding='utf8', timezone_name='UTC',
|
107 |
| - root_id='ocid', schema='', convert_titles=False, **_): |
| 107 | + root_id='ocid', schema='', convert_titles=False, cell_source_map=None, |
| 108 | + heading_source_map=None, **_): |
108 | 109 | """
|
109 | 110 | Unflatten a flat structure (spreadsheet - csv or xlsx) into a nested structure (JSON).
|
110 | 111 |
|
@@ -132,7 +133,20 @@ def unflatten(input_name, base_json=None, input_format=None, output_name='releas
|
132 | 133 | base = json.load(fp, object_pairs_hook=OrderedDict)
|
133 | 134 | else:
|
134 | 135 | base = OrderedDict()
|
135 |
| - base[main_sheet_name] = list(spreadsheet_input.unflatten()) |
136 |
| - with codecs.open(output_name, 'w', encoding='utf-8') as fp: |
137 |
| - json.dump(base, fp, indent=4, default=decimal_default, ensure_ascii=False) |
| 136 | + if WITH_CELLS: |
| 137 | + result, cell_source_map_data, heading_source_map_data = spreadsheet_input.fancy_unflatten() |
| 138 | + base[main_sheet_name] = list(result) |
| 139 | + with codecs.open(output_name, 'w', encoding='utf-8') as fp: |
| 140 | + json.dump(base, fp, indent=4, default=decimal_default, ensure_ascii=False) |
| 141 | + if cell_source_map: |
| 142 | + with codecs.open(cell_source_map, 'w', encoding='utf-8') as fp: |
| 143 | + json.dump(cell_source_map_data, fp, indent=4, default=decimal_default, ensure_ascii=False) |
| 144 | + if heading_source_map: |
| 145 | + with codecs.open(heading_source_map, 'w', encoding='utf-8') as fp: |
| 146 | + json.dump(heading_source_map_data, fp, indent=4, default=decimal_default, ensure_ascii=False) |
| 147 | + else: |
| 148 | + result = spreadsheet_input.unflatten() |
| 149 | + base[main_sheet_name] = list(result) |
| 150 | + with codecs.open(output_name, 'w', encoding='utf-8') as fp: |
| 151 | + json.dump(base, fp, indent=4, default=decimal_default, ensure_ascii=False) |
138 | 152 |
|
0 commit comments