Skip to content

Commit 7fa9693

Browse files
authored
Merge pull request #201 from andylolz/150-xml-root-path
Support user-defined XML root node
2 parents b06203c + 769b272 commit 7fa9693

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

flattentool/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def decimal_default(o):
111111

112112

113113
def unflatten(input_name, base_json=None, input_format=None, output_name=None,
114-
root_list_path='main', encoding='utf8', timezone_name='UTC',
114+
root_list_path=None, encoding='utf8', timezone_name='UTC',
115115
root_id=None, schema='', convert_titles=False, cell_source_map=None,
116116
heading_source_map=None, id_name='id', xml=False,
117117
vertical_orientation=False,
@@ -179,6 +179,9 @@ def unflatten(input_name, base_json=None, input_format=None, output_name=None,
179179
if result:
180180
base.update(result[0])
181181

182+
if root_list_path is None:
183+
root_list_path = base_configuration.get('RootListPath', 'main')
184+
182185
if not metatab_only:
183186
spreadsheet_input_class = INPUT_FORMATS[input_format]
184187
spreadsheet_input = spreadsheet_input_class(
@@ -208,14 +211,15 @@ def unflatten(input_name, base_json=None, input_format=None, output_name=None,
208211
base[root_list_path] = list(result)
209212

210213
if xml:
214+
xml_root_tag = base_configuration.get('XMLRootTag', 'iati-activities')
211215
if output_name is None:
212216
if sys.version > '3':
213-
sys.stdout.buffer.write(toxml(base))
217+
sys.stdout.buffer.write(toxml(base, xml_root_tag))
214218
else:
215-
sys.stdout.write(toxml(base))
219+
sys.stdout.write(toxml(base, xml_root_tag))
216220
else:
217221
with codecs.open(output_name, 'wb') as fp:
218-
fp.write(toxml(base))
222+
fp.write(toxml(base, xml_root_tag))
219223
else:
220224
if output_name is None:
221225
print(json.dumps(base, indent=4, default=decimal_default, ensure_ascii=False))

flattentool/lib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ def parse_sheet_configuration(configuration_list):
1717
configuration['ignore'] = True
1818
if (len(parts) == 1 and parts[0].lower() in ("hashcomments", "hashcomment")):
1919
configuration['hashcomments'] = True
20+
if (len(parts) == 2 and parts[0].lower() == "xmlroottag"):
21+
configuration['XMLRootTag'] = parts[1]
22+
if (len(parts) == 2 and parts[0].lower() == "rootlistpath"):
23+
configuration['RootListPath'] = parts[1]
2024
return configuration
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<iati-organisations><!--XML generated by flatten-tool--><iati-organisation><organisation-identifier>AA-AAA-123456789</organisation-identifier><reporting-org ref="AA-AAA-123456789" secondary-reporter="0" type="40" /></iati-organisation></iati-organisations>
8.48 KB
Binary file not shown.

flattentool/tests/test_unflatten.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,15 @@ def test_unflatten_xml(tmpdir):
6666
id_name='iati-identifier',
6767
xml=True)
6868
assert open('examples/iati/expected.xml').read() == tmpdir.join('output.xml').read()
69+
70+
71+
def test_unflatten_org_xml(tmpdir):
72+
unflatten(
73+
input_name='flattentool/tests/fixtures/xlsx/iati-org.xlsx',
74+
output_name=tmpdir.join('output.xml').strpath,
75+
input_format='xlsx',
76+
id_name='organisation-identifier',
77+
xml=True,
78+
metatab_name='Meta'
79+
)
80+
assert open('flattentool/tests/fixtures/iati-org.xml').read() == tmpdir.join('output.xml').read()

flattentool/xml_output.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ def dict_to_xml(data, tagname, toplevel=True, nsmap=None):
5454
return el
5555

5656

57-
def toxml(data):
57+
def toxml(data, xml_root_tag):
5858
nsmap = {
5959
# This is "bound by definition" - see https://www.w3.org/XML/1998/namespace
6060
'xml': 'http://www.w3.org/XML/1998/namespace'
6161
}
62-
root = dict_to_xml(data, 'iati-activities', nsmap=nsmap)
62+
root = dict_to_xml(data, xml_root_tag, nsmap=nsmap)
6363
comment = ET.Comment('XML generated by flatten-tool')
6464
root.insert(0, comment)
6565
return ET.tostring(root)

0 commit comments

Comments
 (0)