Skip to content

Commit 72c8ec7

Browse files
committed
[#151] Basic XML to spreadsheet conversion
1 parent f712220 commit 72c8ec7

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

flattentool/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def spreadsheet_output(spreadsheet_output_class, name):
4141
raise Exception('The requested format is not available')
4242

4343

44-
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, **_):
44+
def flatten(input_name, schema=None, output_name='flattened', output_format='all', main_sheet_name='main',
45+
root_list_path='main', rollup=False, root_id=None, use_titles=False, xml=False, id_name='id', **_):
4546
"""
4647
Flatten a nested structure (JSON) to a flat structure (spreadsheet - csv or xlsx).
4748
@@ -61,7 +62,9 @@ def flatten(input_name, schema=None, output_name='flattened', output_format='all
6162
root_list_path=root_list_path,
6263
schema_parser=schema_parser,
6364
root_id=root_id,
64-
use_titles=use_titles)
65+
use_titles=use_titles,
66+
xml=xml,
67+
id_name=id_name)
6568
parser.parse()
6669

6770
def spreadsheet_output(spreadsheet_output_class, name):

flattentool/cli.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ def create_parser():
6868
parser_flatten.add_argument(
6969
"-f", "--output-format",
7070
help="Type of template you want to create. Defaults to all available options")
71+
parser_flatten.add_argument(
72+
"--xml",
73+
action='store_true',
74+
help="Use XML as the output format")
75+
parser_flatten.add_argument(
76+
"--id-name",
77+
help="String to use for the identifier key, defaults to 'id'")
7178
parser_flatten.add_argument(
7279
"-m", "--main-sheet-name",
7380
help="The name of the main sheet, as seen in the first tab of the spreadsheet for example. Defaults to main")

flattentool/json_input.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from flattentool.sheet import Sheet
1616
from warnings import warn
1717
import codecs
18+
import xmltodict
1819

1920
BASIC_TYPES = [six.text_type, bool, int, Decimal, type(None)]
2021

@@ -45,12 +46,14 @@ class JSONParser(object):
4546
# Named for consistency with schema.SchemaParser, but not sure it's the most appropriate name.
4647
# Similarily with methods like parse_json_dict
4748

48-
def __init__(self, json_filename=None, root_json_dict=None, schema_parser=None, root_list_path=None, root_id='ocid', use_titles=False):
49+
def __init__(self, json_filename=None, root_json_dict=None, schema_parser=None, root_list_path=None,
50+
root_id='ocid', use_titles=False, xml=False, id_name='id'):
4951
self.sub_sheets = {}
5052
self.main_sheet = Sheet()
5153
self.root_list_path = root_list_path
5254
self.root_id = root_id
5355
self.use_titles = use_titles
56+
self.id_name = id_name
5457
if schema_parser:
5558
self.main_sheet = schema_parser.main_sheet
5659
self.sub_sheets = schema_parser.sub_sheets
@@ -60,6 +63,11 @@ def __init__(self, json_filename=None, root_json_dict=None, schema_parser=None,
6063
else:
6164
self.rollup = False
6265

66+
if xml:
67+
with codecs.open(json_filename, 'rb') as xml_file:
68+
root_json_dict = xmltodict.parse(xml_file, cdata_key='')['iati-activities']
69+
json_filename = None
70+
6371
if json_filename is None and root_json_dict is None:
6472
raise ValueError('Etiher json_filename or root_json_dict must be supplied')
6573

@@ -114,8 +122,8 @@ def parse_json_dict(self, json_dict, sheet, json_key=None, parent_name='', flatt
114122
if self.root_id and self.root_id in json_dict:
115123
parent_id_fields[sheet_key(sheet, self.root_id)] = json_dict[self.root_id]
116124

117-
if 'id' in json_dict:
118-
parent_id_fields[sheet_key(sheet, parent_name+'id')] = json_dict['id']
125+
if self.id_name in json_dict:
126+
parent_id_fields[sheet_key(sheet, parent_name+self.id_name)] = json_dict[self.id_name]
119127

120128

121129
for key, value in json_dict.items():

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup
22
import sys
33

4-
install_requires = ['jsonref', 'schema', 'openpyxl>=2,<2.4', 'six', 'pytz']
4+
install_requires = ['jsonref', 'schema', 'openpyxl>=2,<2.4', 'six', 'pytz', 'xmltodict']
55

66
if sys.version < '3':
77
install_requires.append('unicodecsv')

0 commit comments

Comments
 (0)