From b8f0e6a35f7d959edad860b1fa8bce8ddf21c82a Mon Sep 17 00:00:00 2001 From: MinchinWeb Date: Sat, 8 May 2021 11:17:46 -0600 Subject: [PATCH] consolidate demo plugins to common package --- .../workflows/testing_external_plugins.yaml | 9 +++- .../jrnl/contrib/exporter/custom_json.py | 39 +++++++++++++++ .../jrnl/contrib/exporter/rot13.py | 5 +- .../jrnl/contrib}/exporter/testing.py | 1 + .../jrnl/contrib/importer/sample_json.py | 50 +++++++++++++++++++ 5 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 tests/external_plugins_src/jrnl/contrib/exporter/custom_json.py rename {jrnl/plugins => tests/external_plugins_src/jrnl/contrib}/exporter/testing.py (93%) create mode 100644 tests/external_plugins_src/jrnl/contrib/importer/sample_json.py diff --git a/.github/workflows/testing_external_plugins.yaml b/.github/workflows/testing_external_plugins.yaml index 7bcce735..d7bd904c 100644 --- a/.github/workflows/testing_external_plugins.yaml +++ b/.github/workflows/testing_external_plugins.yaml @@ -32,6 +32,11 @@ jobs: python-version: 3.9 steps: + - name: Extract branch name + shell: bash + run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})" + id: extract_branch + - uses: actions/checkout@v2 - name: Set up Python ${{ matrix.python-version }} @@ -49,9 +54,9 @@ jobs: run: | pip install poetry poetry config --local virtualenvs.in-project true - poetry add git+https://github.com/MinchinWeb/jrnl-rot13-exporter.git poetry install --remove-untracked - poetry install + poetry install + poetry run python -m pip install -e "git+${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git@{{ steps.extract_branch.outputs.branch }}#egg=jrnl-demo-plugins&subdirectory=tests/external_plugins_src" - name: Test with pytest if: success() || failure() diff --git a/tests/external_plugins_src/jrnl/contrib/exporter/custom_json.py b/tests/external_plugins_src/jrnl/contrib/exporter/custom_json.py new file mode 100644 index 00000000..1d5ebd56 --- /dev/null +++ b/tests/external_plugins_src/jrnl/contrib/exporter/custom_json.py @@ -0,0 +1,39 @@ +# pelican\contrib\exporter\custom_json.py +import json + +from jrnl.plugins.base import BaseExporter +from jrnl.plugins.util import get_tags_count + +__version__ = "v1.0.0" + + +class Exporter(BaseExporter): + """ + This basic Exporter can convert entries and journals into JSON. + """ + + names = ["json"] + extension = "json" + version = __version__ + + @classmethod + def entry_to_dict(cls, entry): + return { + "title": entry.title, + "body": entry.body, + "date": entry.date.strftime("%Y-%m-%d"), + } + + @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 = { + "entries": [cls.entry_to_dict(e) for e in journal.entries], + } + return json.dumps(result, indent=2) diff --git a/tests/external_plugins_src/jrnl/contrib/exporter/rot13.py b/tests/external_plugins_src/jrnl/contrib/exporter/rot13.py index 508a8f42..d7893f60 100644 --- a/tests/external_plugins_src/jrnl/contrib/exporter/rot13.py +++ b/tests/external_plugins_src/jrnl/contrib/exporter/rot13.py @@ -2,10 +2,11 @@ import codecs from jrnl.plugins.base import BaseExporter -__version__ = "1.0.0" +__version__ = "v1.0.0" + class Exporter(BaseExporter): - names = ["rot13"] + names = ["rot13", "txt"] extension = "txt" version = __version__ diff --git a/jrnl/plugins/exporter/testing.py b/tests/external_plugins_src/jrnl/contrib/exporter/testing.py similarity index 93% rename from jrnl/plugins/exporter/testing.py rename to tests/external_plugins_src/jrnl/contrib/exporter/testing.py index c88314e8..76982c2a 100644 --- a/jrnl/plugins/exporter/testing.py +++ b/tests/external_plugins_src/jrnl/contrib/exporter/testing.py @@ -13,3 +13,4 @@ from jrnl.plugins.base import BaseExporter class Exporter(BaseExporter): names = ["testing", "test"] version = "v0.0.1" + extension = "test" diff --git a/tests/external_plugins_src/jrnl/contrib/importer/sample_json.py b/tests/external_plugins_src/jrnl/contrib/importer/sample_json.py new file mode 100644 index 00000000..b1d27858 --- /dev/null +++ b/tests/external_plugins_src/jrnl/contrib/importer/sample_json.py @@ -0,0 +1,50 @@ +# pelican\contrib\importer\sample_json.py +import json +import sys + +from jrnl import Entry +from jrnl.plugins.base import BaseImporter + +__version__ = "v1.0.0" + + +class Importer(BaseImporter): + """JSON Importer for jrnl.""" + + names = ["json"] + version = __version__ + + @staticmethod + def import_(journal, input=None): + """ + Given a nicely formatted JSON file, will add the + contained Entries to the journal. + """ + + old_cnt = len(journal.entries) + if input: + with open(input, "r", encoding="utf-8") as f: + data = json.loads(f) + else: + try: + data = sys.stdin.read() + except KeyboardInterrupt: + print( + "[Entries NOT imported into journal.]", + file=sys.stderr, + ) + sys.exit(0) + + for json_entry in data: + raw = json_entry["title"] + "/n" + json_entry["body"] + date = json_entry["date"] + entry = Entry.Entry(journal, date, raw) + journal.entries.append(entry) + + new_cnt = len(journal.entries) + print( + "[{} entries imported to '{}' journal]".format( + new_cnt - old_cnt, journal.name + ), + file=sys.stderr, + )