Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tags to json and xml exporters #975

Merged
merged 6 commits into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions features/exporting.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
25 changes: 21 additions & 4 deletions features/steps/export_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,42 @@ def check_json_output_path(context, path, value):
assert struct == value, struct


@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()
xml_tree = ElementTree.fromstring(output)
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()
Expand Down
1 change: 1 addition & 0 deletions jrnl/plugins/json_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def entry_to_dict(cls, entry):
"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"):
Expand Down
5 changes: 5 additions & 0 deletions jrnl/plugins/xml_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def entry_to_xml(cls, entry, doc):
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

Expand Down