jrnl/jrnl/plugins/exporter/json.py
MinchinWeb 79c37401c4 jrnl.__version__ magic works! (#1296)
* `jrnl.__version__` magic works!

Adjust version imports
Update version on GitHub release flow
Fix version imports & black issue

* escape strings in workflow

Co-authored-by: Jonathan Wren <jonathan@nowandwren.com>
2021-07-17 14:58:15 -07:00

67 lines
2.4 KiB
Python

# encoding: utf-8
# Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html
import json
from jrnl import __version__
from jrnl.plugins.base import BaseExporter
from jrnl.plugins.util import get_tags_count
class Exporter(BaseExporter):
"""This Exporter can convert entries and journals into json."""
names = ["json"]
extension = "json"
version = __version__
@classmethod
def entry_to_dict(cls, entry):
entry_dict = {
"title": entry.title,
"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"):
entry_dict["uuid"] = entry.uuid
if (
hasattr(entry, "creator_device_agent")
or hasattr(entry, "creator_generation_date")
or hasattr(entry, "creator_host_name")
or hasattr(entry, "creator_os_agent")
or hasattr(entry, "creator_software_agent")
):
entry_dict["creator"] = {}
if hasattr(entry, "creator_device_agent"):
entry_dict["creator"]["device_agent"] = entry.creator_device_agent
if hasattr(entry, "creator_generation_date"):
entry_dict["creator"]["generation_date"] = str(
entry.creator_generation_date
)
if hasattr(entry, "creator_host_name"):
entry_dict["creator"]["host_name"] = entry.creator_host_name
if hasattr(entry, "creator_os_agent"):
entry_dict["creator"]["os_agent"] = entry.creator_os_agent
if hasattr(entry, "creator_software_agent"):
entry_dict["creator"]["software_agent"] = entry.creator_software_agent
return entry_dict
@classmethod
def export_entry(cls, entry):
"""Returns a json representation of a single entry."""
return json.dumps(cls.entry_to_dict(entry), indent=2) + "\n"
@classmethod
def export_journal(cls, journal):
"""Returns a json representation of an entire journal."""
tags = get_tags_count(journal)
result = {
"tags": {tag: count for count, tag in tags},
"entries": [cls.entry_to_dict(e) for e in journal.entries],
}
return json.dumps(result, indent=2)