Skip to content

Commit

Permalink
[#40] Use main-sheet-name as the top level JSON key
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjwebb committed Jan 16, 2015
1 parent bc9b3c7 commit 857eeb9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ will print help information specific to that subcommand.

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

flatten-ocds create-template --output-format all --output-name template --schema release-schema.json --main-sheet-name release
flatten-ocds create-template --output-format all --output-name template --schema release-schema.json --main-sheet-name releases

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

Expand Down
10 changes: 5 additions & 5 deletions flattening_ocds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from collections import OrderedDict


def create_template(schema, output_name='release', output_format='all', main_sheet_name='main', flatten=False, rollup=False, **_):
def create_template(schema, output_name='releases', output_format='all', main_sheet_name='main', flatten=False, rollup=False, **_):
"""
Creates template file(s) from given inputs
This function is built to deal with commandline input and arguments
Expand Down Expand Up @@ -36,7 +36,7 @@ def spreadsheet_output(spreadsheet_output_class, name):
raise Exception('The requested format is not available')


def flatten(input_name, schema=None, output_name='release', output_format='all', main_sheet_name='main', root_list_path='releases', rollup=False, **_):
def flatten(input_name, schema=None, output_name='releases', output_format='all', main_sheet_name='main', root_list_path='releases', rollup=False, **_):
if schema:
schema_parser = SchemaParser(schema_filename=schema, rollup=rollup)
schema_parser.parse()
Expand Down Expand Up @@ -88,8 +88,8 @@ def decimal_default(o):
raise TypeError(repr(o) + " is not JSON serializable")


def unflatten(input_name, base_json=None, input_format=None, output_name='release.json',
main_sheet_name='release', encoding='utf8', timezone_name='UTC', **_):
def unflatten(input_name, base_json=None, input_format=None, output_name='releases.json',
main_sheet_name='releases', encoding='utf8', timezone_name='UTC', **_):
if input_format is None:
raise Exception('You must specify an input format (may autodetect in future')
elif input_format not in INPUT_FORMATS:
Expand All @@ -107,7 +107,7 @@ def unflatten(input_name, base_json=None, input_format=None, output_name='releas
base = json.load(fp, object_pairs_hook=OrderedDict)
else:
base = OrderedDict()
base['releases'] = list(spreadsheet_input.unflatten())
base[main_sheet_name] = list(spreadsheet_input.unflatten())
with open(output_name, 'w') as fp:
json.dump(base, fp, indent=4, default=decimal_default)

4 changes: 2 additions & 2 deletions flattening_ocds/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ def create_parser():
help="A base json file to populate the releases key in.")
parser_unflatten.add_argument(
"-m", "--main-sheet-name",
help="The name of the main sheet. Defaults to release")
help="The name of the main sheet. Defaults to releases")
parser_unflatten.add_argument(
"-e", "--encoding",
help="Encoding of the input file(s) (only relevant for CSV). Defaults to utf8.")
parser_unflatten.add_argument(
"-o", "--output-name",
help="Name of the outputted file. Will have an extension appended as appropriate. Defaults to release")
help="Name of the outputted file. Will have an extension appended as appropriate. Defaults to releases")
parser_unflatten.add_argument(
"--timezone-name",
help="Name of the timezone, defaults to UTC. Should be in tzdata format, e.g. Europe/London")
Expand Down
10 changes: 5 additions & 5 deletions flattening_ocds/tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_unflatten(tmpdir):
output_name=tmpdir.join('release.json').strpath,
main_sheet_name='main')
assert lines_strip_whitespace(tmpdir.join('release.json').read()) == lines_strip_whitespace('''{
"releases": [
"main": [
{
"ocid": "1",
"id": "2",
Expand Down Expand Up @@ -156,7 +156,7 @@ def test_unflatten_empty(tmpdir):
output_name=tmpdir.join('release.json').strpath,
main_sheet_name='main')
assert lines_strip_whitespace(tmpdir.join('release.json').read()) == lines_strip_whitespace('''{
"releases": []
"main": []
}''')


Expand All @@ -173,7 +173,7 @@ def test_unflatten_csv_utf8(tmpdir):
output_name=tmpdir.join('release.json').strpath,
main_sheet_name='main')
reloaded_json = json.load(tmpdir.join('release.json'))
assert reloaded_json == {'releases': [{'ocid': '1', 'id': 'éαГ😼𝒞人'}]}
assert reloaded_json == {'main': [{'ocid': '1', 'id': 'éαГ😼𝒞人'}]}


def test_unflatten_csv_latin1(tmpdir):
Expand All @@ -189,7 +189,7 @@ def test_unflatten_csv_latin1(tmpdir):
output_name=tmpdir.join('release.json').strpath,
main_sheet_name='main')
reloaded_json = json.load(tmpdir.join('release.json'))
assert reloaded_json == {'releases': [{'ocid': '1', 'id': 'é'}]}
assert reloaded_json == {'main': [{'ocid': '1', 'id': 'é'}]}


def test_unflatten_xslx_unicode(tmpdir):
Expand All @@ -199,4 +199,4 @@ def test_unflatten_xslx_unicode(tmpdir):
output_name=tmpdir.join('release.json').strpath,
main_sheet_name='main')
reloaded_json = json.load(tmpdir.join('release.json'))
assert reloaded_json == {'releases': [{'ocid': 1, 'id': 'éαГ😼𝒞人'}]}
assert reloaded_json == {'main': [{'ocid': 1, 'id': 'éαГ😼𝒞人'}]}
6 changes: 3 additions & 3 deletions flattening_ocds/tests/test_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ def test_roundtrip(tmpdir, output_format):
output_name=tmpdir.join('flattened').strpath,
output_format=output_format,
schema='flattening_ocds/tests/fixtures/release-schema.json',
main_sheet_name='release')
main_sheet_name='releases')
unflatten(
input_name=tmpdir.join('flattened').strpath,
output_name=tmpdir.join('roundtrip.json').strpath,
input_format=output_format,
base_json=base_name,
main_sheet_name='release')
main_sheet_name='releases')
original_json = json.load(open(input_name))
roundtripped_json = json.load(tmpdir.join('roundtrip.json'))

# Not currently possible to roundtrib Nones
# Not currently possible to roundtrip Nones
# https://github.com/open-contracting/flattening-ocds/issues/35
for release in roundtripped_json['releases']:
release['tender']['awardCriteriaDetails'] = None
Expand Down

0 comments on commit 857eeb9

Please sign in to comment.