Skip to content

Commit

Permalink
[#45] Add option to use titles when creating a template
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjwebb committed Jan 19, 2015
1 parent 902ce14 commit 90131d1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ However, Python 2 can not load CSVs that contain the NULL character. This includ

There is work currently in progress to convert this codebase to also flatten 360 giving files.

flatten-ocds create-template --output-format all --output-name 360giving-template --schema 360-giving-schema.json --main-sheet-name activity
flatten-ocds create-template --output-format all --output-name 360giving-template --schema 360-giving-schema.json --main-sheet-name grants --rollup --use-titles

flatten-ocds unflatten --root-id='' -o out.json -f xlsx --main-sheet-name=grants input.xlsx --schema 360-giving-schema.json --convert-titles
4 changes: 2 additions & 2 deletions flattening_ocds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
from collections import OrderedDict


def create_template(schema, output_name='releases', output_format='all', main_sheet_name='main', flatten=False, rollup=False, root_id='ocid', **_):
def create_template(schema, output_name='releases', output_format='all', main_sheet_name='main', flatten=False, rollup=False, root_id='ocid', use_titles=False, **_):
"""
Creates template file(s) from given inputs
This function is built to deal with commandline input and arguments
but to also be called from elswhere in future
"""

parser = SchemaParser(schema_filename=schema, main_sheet_name=main_sheet_name, rollup=rollup, root_id=root_id)
parser = SchemaParser(schema_filename=schema, main_sheet_name=main_sheet_name, rollup=rollup, root_id=root_id, use_titles=use_titles)
parser.parse()

def spreadsheet_output(spreadsheet_output_class, name):
Expand Down
4 changes: 4 additions & 0 deletions flattening_ocds/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def create_parser():
parser_create_template.add_argument(
"-r", "--root-id",
help="Root ID of the data format, e.g. ocid for OCDS and blank for 360Giving (use --root-id=''). Defaults to ocid.")
parser_create_template.add_argument(
"--use-titles",
action='store_true',
help="Convert titles. Requires a schema to be specified.")

parser_flatten = subparsers.add_parser(
'flatten',
Expand Down
13 changes: 10 additions & 3 deletions flattening_ocds/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_property_type_set(property_schema_dict):
class SchemaParser(object):
"""Parse the fields of a JSON schema into a flattened structure."""

def __init__(self, schema_filename=None, root_schema_dict=None, main_sheet_name='main', rollup=False, root_id='ocid'):
def __init__(self, schema_filename=None, root_schema_dict=None, main_sheet_name='main', rollup=False, root_id='ocid', use_titles=False):
self.sub_sheets = {}
self.main_sheet = []
self.sub_sheet_mapping = {}
Expand All @@ -46,6 +46,7 @@ def __init__(self, schema_filename=None, root_schema_dict=None, main_sheet_name=
self.root_id = root_id
self.main_sheet_titles = {}
self.sub_sheet_titles = {}
self.use_titles = use_titles

if root_schema_dict is None and schema_filename is None:
raise ValueError('One of schema_filename or root_schema_dict must be supplied')
Expand All @@ -60,7 +61,10 @@ def __init__(self, schema_filename=None, root_schema_dict=None, main_sheet_name=
def parse(self):
fields = self.parse_schema_dict(self.main_sheet_name, self.root_schema_dict)
for field, title in fields:
self.main_sheet.append(field)
if self.use_titles:
self.main_sheet.append(title)
else:
self.main_sheet.append(field)
if title:
self.main_sheet_titles[title] = field

Expand Down Expand Up @@ -110,7 +114,10 @@ def parse_schema_dict(self, parent_name, schema_dict, parent_id_fields=None):
property_schema_dict['items'],
parent_id_fields=id_fields)
for field, child_title in fields:
sub_sheet.add_field(field)
if self.use_titles:
sub_sheet.add_field(title)
else:
sub_sheet.add_field(field)
if child_title:
self.sub_sheet_titles[sub_sheet_name][child_title] = field
if self.rollup and 'rollUp' in property_schema_dict and field in property_schema_dict['rollUp']:
Expand Down

0 comments on commit 90131d1

Please sign in to comment.