From be38cfa87af71d1d18eebd28fa477eae487fae9d Mon Sep 17 00:00:00 2001 From: eshrh Date: Sat, 13 Jun 2020 14:58:35 -0400 Subject: [PATCH] Add tags to json and xml exporters (#975) * tag array for json * add tags to entry in xml * xml test * json test * black * removed called --- features/exporting.feature | 3 +++ features/steps/export_steps.py | 25 +++++++++++++++++++++---- jrnl/plugins/json_exporter.py | 1 + jrnl/plugins/xml_exporter.py | 5 +++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/features/exporting.feature b/features/exporting.feature index 7f4df59f..96a9558f 100644 --- a/features/exporting.feature +++ b/features/exporting.feature @@ -9,6 +9,8 @@ Feature: Exporting a Journal And "tags" in the json output should contain "@idea" And "tags" in the json output should contain "@journal" And "tags" in the json output should contain "@dan" + And entry 1 should have an array "tags" with 2 elements + And entry 2 should have an array "tags" with 2 elements Scenario: Exporting using filters should only export parts of the journal Given we use the config "tags.yaml" @@ -89,6 +91,7 @@ Feature: Exporting a Journal Then the output should be a valid XML string And "entries" node in the xml output should have 2 elements And "tags" in the xml output should contain ["@idea", "@journal", "@dan"] + And there should be 7 "tag" elements Scenario: Exporting tags Given we use the config "tags.yaml" diff --git a/features/steps/export_steps.py b/features/steps/export_steps.py index 6c8cd282..2e59d729 100644 --- a/features/steps/export_steps.py +++ b/features/steps/export_steps.py @@ -65,6 +65,16 @@ def check_json_output_path(context, path, value=None): assert struct is not None +@then( + 'entry {entry_number:d} should have an array "{name}" with {items_number:d} elements' +) +def entry_array_count(context, entry_number, name, items_number): + # note that entry_number is 1-indexed. + out = context.stdout_capture.getvalue() + out_json = json.loads(out) + assert len(out_json["entries"][entry_number - 1][name]) == items_number + + @then("the output should be a valid XML string") def assert_valid_xml_string(context): output = context.stdout_capture.getvalue() @@ -72,18 +82,25 @@ def assert_valid_xml_string(context): assert xml_tree, output -@then('"entries" node in the xml output should have {number:d} elements') -def assert_xml_output_entries_count(context, number): +@then('"{item}" node in the xml output should have {number:d} elements') +def assert_xml_output_entries_count(context, item, number): output = context.stdout_capture.getvalue() xml_tree = ElementTree.fromstring(output) xml_tags = (node.tag for node in xml_tree) - assert "entries" in xml_tags, str(list(xml_tags)) + assert item in xml_tags, str(list(xml_tags)) - actual_entry_count = len(xml_tree.find("entries")) + actual_entry_count = len(xml_tree.find(item)) assert actual_entry_count == number, actual_entry_count +@then('there should be {number:d} "{item}" elements') +def count_elements(context, number, item): + output = context.stdout_capture.getvalue() + xml_tree = ElementTree.fromstring(output) + assert len(xml_tree.findall(".//" + item)) == number + + @then('"tags" in the xml output should contain {expected_tags_json_list}') def assert_xml_output_tags(context, expected_tags_json_list): output = context.stdout_capture.getvalue() diff --git a/jrnl/plugins/json_exporter.py b/jrnl/plugins/json_exporter.py index 8dfe057b..9254e51a 100644 --- a/jrnl/plugins/json_exporter.py +++ b/jrnl/plugins/json_exporter.py @@ -20,6 +20,7 @@ class JSONExporter(TextExporter): "body": entry.body, "date": entry.date.strftime("%Y-%m-%d"), "time": entry.date.strftime("%H:%M"), + "tags": entry.tags, "starred": entry.starred, } if hasattr(entry, "uuid"): diff --git a/jrnl/plugins/xml_exporter.py b/jrnl/plugins/xml_exporter.py index 065c929b..4ffcc731 100644 --- a/jrnl/plugins/xml_exporter.py +++ b/jrnl/plugins/xml_exporter.py @@ -35,6 +35,11 @@ class XMLExporter(JSONExporter): if hasattr(entry, "uuid"): entry_el.setAttribute("uuid", entry.uuid) entry_el.setAttribute("starred", entry.starred) + tags = entry.tags + for tag in tags: + tag_el = doc.createElement("tag") + tag_el.setAttribute("name", tag) + entry_el.appendChild(tag_el) entry_el.appendChild(doc.createTextNode(entry.fulltext)) return entry_el