Skip to content

Commit d6938b5

Browse files
authored
Merge pull request #113 from OpenDataServices/40-change-root-id-default
[#40] Make root_id default to None
2 parents 1656a27 + dcd3b77 commit d6938b5

File tree

8 files changed

+53
-52
lines changed

8 files changed

+53
-52
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Can be converted to/from a spreadsheet like [examples/simple/main.csv](examples/
5656
Using the commands:
5757

5858
```
59-
flatten-tool unflatten -f csv examples/simple --root-id='' -o examples/simple.json
59+
flatten-tool unflatten -f csv examples/simple -o examples/simple.json
6060
flatten-tool flatten -f csv examples/simple.json -o examples/simple
6161
```
6262

@@ -123,7 +123,7 @@ These are also the spreadsheets that flatten-tool's `flatten` (JSON to Spreadshe
123123
Commands used to generate this:
124124

125125
```
126-
flatten-tool unflatten -f csv examples/array_multisheet --root-id='' -o examples/array_multisheet.json
126+
flatten-tool unflatten -f csv examples/array_multisheet -o examples/array_multisheet.json
127127
flatten-tool flatten -f csv examples/array.json -o examples/array_multisheet
128128
```
129129

@@ -137,7 +137,7 @@ New columns for each item of the array:
137137
|7|8|9|10|11|12|
138138

139139
```
140-
flatten-tool unflatten -f csv examples/array_pointer --root-id='' -o examples/array.json
140+
flatten-tool unflatten -f csv examples/array_pointer -o examples/array.json
141141
```
142142

143143
Repeated rows:
@@ -151,7 +151,7 @@ Repeated rows:
151151

152152

153153
```
154-
flatten-tool unflatten -f csv examples/array_repeat_rows --root-id='' -o examples/array.json
154+
flatten-tool unflatten -f csv examples/array_repeat_rows -o examples/array.json
155155
```
156156

157157

@@ -353,7 +353,7 @@ You can also upload the file to http://standard.open-contracting.org/validator/
353353

354354
Download https://raw.githubusercontent.com/open-contracting/standard/1.0/standard/schema/release-schema.json to the current directory.
355355

356-
flatten-tool create-template --output-format all --output-name template --schema release-schema.json --main-sheet-name releases
356+
flatten-tool create-template --root-id=ocid --output-format all --output-name template --schema release-schema.json --main-sheet-name releases
357357

358358
This will create `template.xlsx` and a `template/` directory of csv files.
359359

@@ -368,18 +368,18 @@ And populate this with the package information for your release.
368368

369369
Then, for a populated xlsx template in (in release_populated.xlsx):
370370

371-
flatten-tool unflatten release_populated.xlsx --base-json base.json --input-format xlsx --output-name release.json --root-list-path='releases'
371+
flatten-tool unflatten release_populated.xlsx --root-id=ocid --base-json base.json --input-format xlsx --output-name release.json --root-list-path='releases'
372372

373373
Or for populated CSV files (in the release_populated directory):
374374

375-
flatten-tool unflatten release_populated --base-json base.json --input-format csv --output-name release.json --root-list-path='releases'
375+
flatten-tool unflatten release_populated --root-id=ocid --base-json base.json --input-format csv --output-name release.json --root-list-path='releases'
376376

377377
These produce a release.json file based on the data in the spreadsheets.
378378

379379

380380
### Converting a JSON file to a spreadsheet
381381

382-
flatten-tool flatten input.json --main-sheet-name releases --output-name flattened --root-list-path='releases'
382+
flatten-tool flatten input.json --root-id=ocid --main-sheet-name releases --output-name flattened --root-list-path='releases'
383383

384384
This will create `flattened.xlsx` and a `flattened/` directory of csv files.
385385

@@ -391,9 +391,9 @@ Download
391391
https://raw.githubusercontent.com/ThreeSixtyGiving/standard/master/schema/360-giving-schema.json
392392
to the current directory.
393393

394-
flatten-tool create-template --root-id='' --output-format all --output-name 360giving-template --schema 360-giving-schema.json --main-sheet-name grants --rollup --use-titles
394+
flatten-tool create-template --output-format all --output-name 360giving-template --schema 360-giving-schema.json --main-sheet-name grants --rollup --use-titles
395395

396-
flatten-tool unflatten --root-id='' -o out.json -f xlsx input.xlsx --schema 360-giving-schema.json --convert-titles --root-list-path='grants'
396+
flatten-tool unflatten -o out.json -f xlsx input.xlsx --schema 360-giving-schema.json --convert-titles --root-list-path='grants'
397397

398398

399399
Running the tests

flattentool/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from collections import OrderedDict
1010

1111

12-
def create_template(schema, output_name='template', output_format='all', main_sheet_name='main', flatten=False, rollup=False, root_id='ocid', use_titles=False, **_):
12+
def create_template(schema, output_name='template', output_format='all', main_sheet_name='main', flatten=False, rollup=False, root_id=None, use_titles=False, **_):
1313
"""
1414
Creates template file(s) from given inputs
1515
This function is built to deal with commandline input and arguments
@@ -38,7 +38,7 @@ def spreadsheet_output(spreadsheet_output_class, name):
3838
raise Exception('The requested format is not available')
3939

4040

41-
def flatten(input_name, schema=None, output_name='flattened', output_format='all', main_sheet_name='main', root_list_path='main', rollup=False, root_id='ocid', use_titles=False, **_):
41+
def flatten(input_name, schema=None, output_name='flattened', output_format='all', main_sheet_name='main', root_list_path='main', rollup=False, root_id=None, use_titles=False, **_):
4242
"""
4343
Flatten a nested structure (JSON) to a flat structure (spreadsheet - csv or xlsx).
4444
@@ -102,7 +102,7 @@ def decimal_default(o):
102102

103103
def unflatten(input_name, base_json=None, input_format=None, output_name='unflattened.json',
104104
root_list_path='main', encoding='utf8', timezone_name='UTC',
105-
root_id='ocid', schema='', convert_titles=False, cell_source_map=None,
105+
root_id=None, schema='', convert_titles=False, cell_source_map=None,
106106
heading_source_map=None, **_):
107107
"""
108108
Unflatten a flat structure (spreadsheet - csv or xlsx) into a nested structure (JSON).

flattentool/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def create_parser():
5050
help="\"Roll up\" columns from subsheets into the main sheet if they are specified in a rollUp attribute in the schema.")
5151
parser_create_template.add_argument(
5252
"-r", "--root-id",
53-
help="Root ID of the data format, e.g. ocid for OCDS and blank for 360Giving (use --root-id=''). Defaults to ocid.")
53+
help="Root ID of the data format, e.g. ocid for OCDS")
5454
parser_create_template.add_argument(
5555
"--use-titles",
5656
action='store_true',
@@ -83,7 +83,7 @@ def create_parser():
8383
help="\"Roll up\" columns from subsheets into the main sheet if they are specified in a rollUp attribute in the schema.")
8484
parser_flatten.add_argument(
8585
"-r", "--root-id",
86-
help="Root ID of the data format, e.g. ocid for OCDS and blank for 360Giving (use --root-id=''). Defaults to ocid.")
86+
help="Root ID of the data format, e.g. ocid for OCDS")
8787
parser_flatten.add_argument(
8888
"--use-titles",
8989
action='store_true',
@@ -122,7 +122,7 @@ def create_parser():
122122
help="Name of the timezone, defaults to UTC. Should be in tzdata format, e.g. Europe/London")
123123
parser_unflatten.add_argument(
124124
"-r", "--root-id",
125-
help="Root ID of the data format, e.g. ocid for OCDS and blank for 360Giving (use --root-id=''). Defaults to ocid.")
125+
help="Root ID of the data format, e.g. ocid for OCDS")
126126
parser_unflatten.add_argument(
127127
"-s", "--schema",
128128
help="Path to a relevant schema.")

flattentool/json_input.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
3-
This file contains code that takes an OCDS JSON release file as input (not a
3+
This file contains code that takes an instace of a JSON file as input (not a
44
JSON schema, for that see schema.py).
55
66
"""

flattentool/output.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
class SpreadsheetOutput(object):
2222
# output_name is given a default here, partly to help with tests,
2323
# but should have been defined by the time we get here.
24-
def __init__(self, parser, main_sheet_name='main', output_name='release'):
24+
def __init__(self, parser, main_sheet_name='main', output_name='unflattened'):
2525
self.parser = parser
2626
self.main_sheet_name = main_sheet_name
2727
self.output_name = output_name

flattentool/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def __contains__(self, key):
6262
class SchemaParser(object):
6363
"""Parse the fields of a JSON schema into a flattened structure."""
6464

65-
def __init__(self, schema_filename=None, root_schema_dict=None, rollup=False, root_id='ocid', use_titles=False):
65+
def __init__(self, schema_filename=None, root_schema_dict=None, rollup=False, root_id=None, use_titles=False):
6666
self.sub_sheets = {}
6767
self.main_sheet = Sheet()
6868
self.sub_sheet_mapping = {}

flattentool/tests/test_json_input.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_parse_ids(self):
147147
('a', 'b'),
148148
('c', [OrderedDict([('id', 3), ('d', 'e')]), OrderedDict([('id', 3), ('d', 'e2')])]),
149149
('f', {'g':'h'}) # Check that having nested objects doesn't break ID output
150-
])])
150+
])], root_id='ocid')
151151
parser.parse()
152152
assert list(parser.main_sheet) == [ 'ocid', 'id', 'a', 'f/g' ]
153153
assert parser.main_sheet.lines == [
@@ -186,7 +186,7 @@ def test_parse_ids_subsheet(self):
186186
('f', {'g':'h'}) # Check that having nested objects doesn't break ID output
187187
])
188188
])
189-
])])
189+
])], root_id='ocid')
190190
parser.parse()
191191
assert list(parser.main_sheet) == [ 'ocid', 'id' ]
192192
assert parser.main_sheet.lines == [
@@ -233,7 +233,7 @@ def test_parse_ids_nested(self):
233233
('c', [OrderedDict([('d', 'e')]), OrderedDict([('d', 'e2')])])
234234
])),
235235
('f', {'g':'h'}) # Check that having nested objects doesn't break ID output
236-
])])
236+
])], root_id='ocid')
237237
parser.parse()
238238
assert list(parser.main_sheet) == [ 'ocid', 'id', 'a', 'testnest/id', 'f/g' ]
239239
assert parser.main_sheet.lines == [
@@ -281,7 +281,8 @@ def test_sub_sheet_names(self, tmpdir):
281281
}
282282
}''')
283283
schema_parser = SchemaParser(
284-
schema_filename=test_schema.strpath
284+
schema_filename=test_schema.strpath,
285+
root_id='ocid'
285286
)
286287
schema_parser.parse()
287288
parser = JSONParser(
@@ -342,13 +343,14 @@ def test_rollup(self):
342343
}
343344
},
344345
}
345-
}, rollup=True)
346+
}, rollup=True, root_id='ocid')
346347
schema_parser.parse()
347348
parser = JSONParser(
348349
root_json_dict=[OrderedDict([
349350
('testA', [OrderedDict([('testB', '1'), ('testC', '2')])]),
350351
])],
351-
schema_parser=schema_parser
352+
schema_parser=schema_parser,
353+
root_id='ocid'
352354
)
353355
parser.parse()
354356
assert list(parser.main_sheet) == [ 'testA/0/testB' ]
@@ -393,7 +395,7 @@ def test_rollup_multiple_values(self, recwarn):
393395
}
394396
]
395397
assert len(parser.sub_sheets) == 1
396-
assert set(parser.sub_sheets['testA']) == set(['ocid', 'testA/0/testB', 'testA/0/testC'])
398+
assert set(parser.sub_sheets['testA']) == set(['testA/0/testB', 'testA/0/testC'])
397399
assert parser.sub_sheets['testA'].lines == [
398400
{'testA/0/testB':'1', 'testA/0/testC': '2'},
399401
{'testA/0/testB':'3', 'testA/0/testC': '4'}

flattentool/tests/test_schema_parser.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def test_sub_sheet():
9696
parser.parse()
9797
assert set(parser.main_sheet) == set([])
9898
assert set(parser.sub_sheets) == set(['Atest'])
99-
assert list(parser.sub_sheets['Atest']) == ['ocid', 'Atest/0/Btest']
99+
assert list(parser.sub_sheets['Atest']) == ['Atest/0/Btest']
100100

101101

102102
def object_in_array_example_properties(parent_name, child_name):
@@ -125,7 +125,7 @@ def test_parent_is_object(self):
125125
parser.parse()
126126
assert set(parser.main_sheet) == set(['Atest/id'])
127127
assert set(parser.sub_sheets) == set(['Ate_Btest'])
128-
assert list(parser.sub_sheets['Ate_Btest']) == ['ocid', 'Atest/id', 'Atest/Btest/0/Ctest']
128+
assert list(parser.sub_sheets['Ate_Btest']) == ['Atest/id', 'Atest/Btest/0/Ctest']
129129

130130
def test_parent_is_array(self):
131131
parser = SchemaParser(root_schema_dict={
@@ -139,8 +139,8 @@ def test_parent_is_array(self):
139139
parser.parse()
140140
assert set(parser.main_sheet) == set()
141141
assert set(parser.sub_sheets) == set(['Atest', 'Ate_Btest'])
142-
assert list(parser.sub_sheets['Atest']) == ['ocid', 'Atest/0/id']
143-
assert list(parser.sub_sheets['Ate_Btest']) == ['ocid', 'Atest/0/id', 'Atest/0/Btest/0/Ctest']
142+
assert list(parser.sub_sheets['Atest']) == ['Atest/0/id']
143+
assert list(parser.sub_sheets['Ate_Btest']) == ['Atest/0/id', 'Atest/0/Btest/0/Ctest']
144144

145145
def test_two_parents(self):
146146
parser = SchemaParser(root_schema_dict={
@@ -160,10 +160,10 @@ def test_two_parents(self):
160160
parser.parse()
161161
assert set(parser.main_sheet) == set()
162162
assert set(parser.sub_sheets) == set(['Atest', 'Dtest', 'Ate_Btest', 'Dte_Btest'])
163-
assert list(parser.sub_sheets['Atest']) == ['ocid', 'Atest/0/id']
164-
assert list(parser.sub_sheets['Dtest']) == ['ocid', 'Dtest/0/id']
165-
assert list(parser.sub_sheets['Ate_Btest']) == ['ocid', 'Atest/0/id', 'Atest/0/Btest/0/Ctest']
166-
assert list(parser.sub_sheets['Dte_Btest']) == ['ocid', 'Dtest/0/id', 'Dtest/0/Btest/0/Etest']
163+
assert list(parser.sub_sheets['Atest']) == ['Atest/0/id']
164+
assert list(parser.sub_sheets['Dtest']) == ['Dtest/0/id']
165+
assert list(parser.sub_sheets['Ate_Btest']) == ['Atest/0/id', 'Atest/0/Btest/0/Ctest']
166+
assert list(parser.sub_sheets['Dte_Btest']) == ['Dtest/0/id', 'Dtest/0/Btest/0/Etest']
167167

168168
def test_parent_is_object_nested(self):
169169
parser = SchemaParser(root_schema_dict={
@@ -182,7 +182,7 @@ def test_parent_is_object_nested(self):
182182
parser.parse()
183183
assert set(parser.main_sheet) == set(['Atest/Btest/id'])
184184
assert set(parser.sub_sheets) == set(['Ate_Bte_Btest'])
185-
assert list(parser.sub_sheets['Ate_Bte_Btest']) == ['ocid', 'Atest/Btest/id', 'Atest/Btest/Btest/0/Ctest']
185+
assert list(parser.sub_sheets['Ate_Bte_Btest']) == ['Atest/Btest/id', 'Atest/Btest/Btest/0/Ctest']
186186

187187

188188
class TestSubSheetMainID(object):
@@ -199,7 +199,7 @@ def test_parent_is_object(self):
199199
parser.parse()
200200
assert set(parser.main_sheet) == set(['id', 'Atest/id'])
201201
assert set(parser.sub_sheets) == set(['Ate_Btest'])
202-
assert list(parser.sub_sheets['Ate_Btest']) == ['ocid', 'id', 'Atest/id', 'Atest/Btest/0/Ctest']
202+
assert list(parser.sub_sheets['Ate_Btest']) == ['id', 'Atest/id', 'Atest/Btest/0/Ctest']
203203

204204
def test_parent_is_array(self):
205205
parser = SchemaParser(root_schema_dict={
@@ -215,8 +215,8 @@ def test_parent_is_array(self):
215215
parser.parse()
216216
assert set(parser.main_sheet) == set(['id'])
217217
assert set(parser.sub_sheets) == set(['Atest', 'Ate_Btest'])
218-
assert list(parser.sub_sheets['Atest']) == ['ocid', 'id', 'Atest/0/id']
219-
assert list(parser.sub_sheets['Ate_Btest']) == ['ocid', 'id', 'Atest/0/id', 'Atest/0/Btest/0/Ctest']
218+
assert list(parser.sub_sheets['Atest']) == ['id', 'Atest/0/id']
219+
assert list(parser.sub_sheets['Ate_Btest']) == ['id', 'Atest/0/id', 'Atest/0/Btest/0/Ctest']
220220

221221
def test_two_parents(self):
222222
parser = SchemaParser(root_schema_dict={
@@ -237,10 +237,10 @@ def test_two_parents(self):
237237
parser.parse()
238238
assert set(parser.main_sheet) == set(['id'])
239239
assert set(parser.sub_sheets) == set(['Atest', 'Dtest', 'Ate_Btest', 'Dte_Btest'])
240-
assert list(parser.sub_sheets['Atest']) == ['ocid', 'id', 'Atest/0/id']
241-
assert list(parser.sub_sheets['Dtest']) == ['ocid', 'id', 'Dtest/0/id']
242-
assert list(parser.sub_sheets['Ate_Btest']) == ['ocid', 'id', 'Atest/0/id', 'Atest/0/Btest/0/Ctest']
243-
assert list(parser.sub_sheets['Dte_Btest']) == ['ocid', 'id', 'Dtest/0/id', 'Dtest/0/Btest/0/Etest']
240+
assert list(parser.sub_sheets['Atest']) == ['id', 'Atest/0/id']
241+
assert list(parser.sub_sheets['Dtest']) == ['id', 'Dtest/0/id']
242+
assert list(parser.sub_sheets['Ate_Btest']) == ['id', 'Atest/0/id', 'Atest/0/Btest/0/Ctest']
243+
assert list(parser.sub_sheets['Dte_Btest']) == ['id', 'Dtest/0/id', 'Dtest/0/Btest/0/Etest']
244244

245245
def test_custom_main_sheet_name(self):
246246
parser = SchemaParser(
@@ -258,7 +258,6 @@ def test_custom_main_sheet_name(self):
258258
assert set(parser.main_sheet) == set(['id', 'Atest/id'])
259259
assert set(parser.sub_sheets) == set(['Ate_Btest'])
260260
assert list(parser.sub_sheets['Ate_Btest']) == [
261-
'ocid',
262261
'id',
263262
'Atest/id',
264263
'Atest/Btest/0/Ctest']
@@ -318,7 +317,7 @@ def test_references_sheet_names(tmpdir):
318317
parser = SchemaParser(schema_filename=tmpfile.strpath)
319318
parser.parse()
320319
assert set(parser.sub_sheets) == set(['Atest']) # used to be Btest
321-
assert list(parser.sub_sheets['Atest']) == ['ocid', 'Atest/0/Ctest']
320+
assert list(parser.sub_sheets['Atest']) == ['Atest/0/Ctest']
322321

323322

324323
def test_rollup():
@@ -340,7 +339,7 @@ def test_rollup():
340339
parser.parse()
341340
assert set(parser.main_sheet) == set(['Atest/0/Btest'])
342341
assert set(parser.sub_sheets) == set(['Atest'])
343-
assert set(parser.sub_sheets['Atest']) == set(['ocid', 'Atest/0/Btest', 'Atest/0/Ctest'])
342+
assert set(parser.sub_sheets['Atest']) == set(['Atest/0/Btest', 'Atest/0/Ctest'])
344343

345344

346345
def test_bad_rollup(recwarn):
@@ -370,7 +369,7 @@ def test_bad_rollup(recwarn):
370369

371370
assert set(parser.main_sheet) == set()
372371
assert set(parser.sub_sheets) == set(['Atest'])
373-
assert set(parser.sub_sheets['Atest']) == set(['ocid', 'Atest/0/Ctest'])
372+
assert set(parser.sub_sheets['Atest']) == set(['Atest/0/Ctest'])
374373

375374

376375
def test_sub_sheet_custom_id():
@@ -390,7 +389,7 @@ def test_sub_sheet_custom_id():
390389
assert set(parser.sub_sheets) == set(['Atest'])
391390
assert list(parser.sub_sheets['Atest']) == ['custom', 'Atest/0/Btest']
392391

393-
def test_sub_sheet_no_root_id():
392+
def test_sub_sheet_empty_string_root_id():
394393
parser = SchemaParser(root_schema_dict={
395394
'properties': {
396395
'Atest': {
@@ -432,7 +431,7 @@ def test_use_titles(recwarn):
432431
parser.parse()
433432
assert set(parser.main_sheet) == set(['CTitle'])
434433
assert set(parser.sub_sheets) == set(['Atest'])
435-
assert list(parser.sub_sheets['Atest']) == ['ocid', 'ATitle:BTitle']
434+
assert list(parser.sub_sheets['Atest']) == ['ATitle:BTitle']
436435

437436
# Array title missing
438437
parser = SchemaParser(root_schema_dict={
@@ -458,7 +457,7 @@ def test_use_titles(recwarn):
458457
parser.parse()
459458
assert set(parser.main_sheet) == set(['CTitle'])
460459
assert set(parser.sub_sheets) == set(['Atest'])
461-
assert list(parser.sub_sheets['Atest']) == ['ocid']
460+
assert list(parser.sub_sheets['Atest']) == []
462461
w = recwarn.pop(UserWarning)
463462
assert 'does not have a title' in text_type(w.message)
464463

@@ -486,7 +485,7 @@ def test_use_titles(recwarn):
486485
parser.parse()
487486
assert set(parser.main_sheet) == set([])
488487
assert set(parser.sub_sheets) == set(['Atest'])
489-
assert list(parser.sub_sheets['Atest']) == ['ocid', 'ATitle:BTitle']
488+
assert list(parser.sub_sheets['Atest']) == ['ATitle:BTitle']
490489
w = recwarn.pop(UserWarning)
491490
assert 'does not have a title' in text_type(w.message)
492491

@@ -514,7 +513,7 @@ def test_use_titles(recwarn):
514513
parser.parse()
515514
assert set(parser.main_sheet) == set(['CTitle'])
516515
assert set(parser.sub_sheets) == set(['Atest'])
517-
assert list(parser.sub_sheets['Atest']) == ['ocid']
516+
assert list(parser.sub_sheets['Atest']) == []
518517
w = recwarn.pop(UserWarning)
519518
assert 'does not have a title' in text_type(w.message)
520519

@@ -545,7 +544,7 @@ def test_titles_rollup():
545544
parser.parse()
546545
assert set(parser.main_sheet) == set(['ATitle:BTitle'])
547546
assert set(parser.sub_sheets) == set(['Atest'])
548-
assert set(parser.sub_sheets['Atest']) == set(['ocid', 'ATitle:BTitle', 'ATitle:CTitle'])
547+
assert set(parser.sub_sheets['Atest']) == set(['ATitle:BTitle', 'ATitle:CTitle'])
549548

550549

551550
def test_schema_from_uri(httpserver):

0 commit comments

Comments
 (0)