Skip to content

Commit f4b44c5

Browse files
committed
[#130] Add command line option to default configuration options.
1 parent b10d3f0 commit f4b44c5

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

examples/help/unflatten/expected.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ usage: flatten-tool unflatten [-h] -f INPUT_FORMAT [--xml] [--id-name ID_NAME]
99
[--metatab-schema METATAB_SCHEMA]
1010
[--metatab-only]
1111
[--metatab-vertical-orientation]
12+
[--default-configuration DEFAULT_CONFIGURATION]
1213
input_name
1314

1415
positional arguments:
@@ -60,4 +61,7 @@ optional arguments:
6061
--metatab-vertical-orientation
6162
Read metatab so that headings are in the first column
6263
and data is read vertically. Only for XLSX not CSV
64+
--default-configuration DEFAULT_CONFIGURATION
65+
Comma seperated list of default parsing commands for
66+
all sheets. Only for XLSX not CSV
6367

flattentool/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from flattentool.output import FORMATS_SUFFIX
55
from flattentool.input import FORMATS as INPUT_FORMATS
66
from flattentool.xml_output import toxml
7+
from flattentool.lib import parse_sheet_configuration
78
import sys
89
import json
910
import codecs
@@ -112,6 +113,7 @@ def unflatten(input_name, base_json=None, input_format=None, output_name=None,
112113
vertical_orientation=False,
113114
metatab_name=None, metatab_only=False, metatab_schema='',
114115
metatab_vertical_orientation=False,
116+
default_configuration='',
115117
**_):
116118
"""
117119
Unflatten a flat structure (spreadsheet - csv or xlsx) into a nested structure (JSON).
@@ -130,7 +132,10 @@ def unflatten(input_name, base_json=None, input_format=None, output_name=None,
130132
else:
131133
base = OrderedDict()
132134

133-
base_configuration = {}
135+
136+
base_configuration = parse_sheet_configuration(
137+
[item.strip() for item in default_configuration.split(",")]
138+
)
134139

135140
cell_source_map_data = OrderedDict()
136141
heading_source_map_data = OrderedDict()

flattentool/cli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ def create_parser():
155155
"--metatab-vertical-orientation",
156156
action='store_true',
157157
help="Read metatab so that headings are in the first column and data is read vertically. Only for XLSX not CSV")
158+
parser_unflatten.add_argument(
159+
"--default-configuration",
160+
help="Comma seperated list of default parsing commands for all sheets. Only for XLSX not CSV")
158161

159162
return parser
160163

4.74 KB
Binary file not shown.

flattentool/tests/test_init.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,3 +1169,32 @@ def test_commands_metatab(tmpdir):
11691169

11701170
assert unflattened == {'main': [{'actual': 'actual', 'headings': 'data', 'some': 'some'}, {'actual': 'actual', 'headings': 'Other data', 'some': 'some'}],
11711171
'some': 'data'}
1172+
1173+
def test_commands_single_sheet_default(tmpdir):
1174+
1175+
unflatten(
1176+
'flattentool/tests/fixtures/xlsx/commands_defaulted.xlsx',
1177+
input_format='xlsx',
1178+
output_name=tmpdir.join('command_single_unflattened.json').strpath,
1179+
cell_source_map=tmpdir.join('command_single_source_map.json').strpath,
1180+
heading_source_map=tmpdir.join('command_single_heading_source_map.json').strpath,
1181+
default_configuration="SkipRows 1, headerrows 2",
1182+
)
1183+
1184+
unflattened = json.load(tmpdir.join('command_single_unflattened.json'))
1185+
1186+
assert unflattened == {'main': [{'actual': 'actual', 'headings': 'data', 'some': 'some'}]}
1187+
1188+
1189+
unflatten(
1190+
'flattentool/tests/fixtures/xlsx/commands_defaulted.xlsx',
1191+
input_format='xlsx',
1192+
output_name=tmpdir.join('command_single_unflattened.json').strpath,
1193+
cell_source_map=tmpdir.join('command_single_source_map.json').strpath,
1194+
heading_source_map=tmpdir.join('command_single_heading_source_map.json').strpath,
1195+
default_configuration="SkipRows 1",
1196+
)
1197+
1198+
unflattened = json.load(tmpdir.join('command_single_unflattened.json'))
1199+
1200+
assert unflattened == {'main': [{'actual': 'other', 'headings': 'headings', 'some': 'some'}, {'actual': 'actual', 'headings': 'data', 'some': 'some'}]}

0 commit comments

Comments
 (0)