From 2777e6770c8ce7799edc138467780ed4af285957 Mon Sep 17 00:00:00 2001 From: dbxnr Date: Wed, 12 Feb 2020 03:09:59 +0000 Subject: [PATCH] fix broken jrnl format import --- ...{dayone2.feature => import_dayone2.feature} | 0 features/import_jrnl.feature | 7 +++++++ jrnl/cli.py | 12 +++++++----- jrnl/plugins/__init__.py | 4 ++-- jrnl/plugins/dayone2_importer.py | 2 +- jrnl/plugins/jrnl_importer.py | 18 +++++++++++------- 6 files changed, 28 insertions(+), 15 deletions(-) rename features/{dayone2.feature => import_dayone2.feature} (100%) create mode 100644 features/import_jrnl.feature diff --git a/features/dayone2.feature b/features/import_dayone2.feature similarity index 100% rename from features/dayone2.feature rename to features/import_dayone2.feature diff --git a/features/import_jrnl.feature b/features/import_jrnl.feature new file mode 100644 index 00000000..d63fcf48 --- /dev/null +++ b/features/import_jrnl.feature @@ -0,0 +1,7 @@ +Feature: Import jrnl file + + + Scenario: Importing a jrnl file + Given we use the config "basic.yaml" + When we run "jrnl --import jrnl features/data/journals/tags.journal" + Then we should see the message "[2 imported to default journal]" diff --git a/jrnl/cli.py b/jrnl/cli.py index ee1d6ecc..c641aaa2 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -346,11 +346,6 @@ def run(manual_args=None): else: sys.exit() - # Import mode - if args.import_: - root_config = install.load_or_install_jrnl() - return plugins.get_importer(args.import_, args.text, root_config) - # This is where we finally open the journal! try: journal = open_journal(journal_name, config) @@ -358,6 +353,13 @@ def run(manual_args=None): print(f"[Interrupted while opening journal]", file=sys.stderr) sys.exit(1) + # Import mode + if args.import_: + root_config = install.load_or_install_jrnl() + return plugins.get_importer( + args.import_, args.text, root_config, journal=journal + ) + # Writing mode if mode_compose: raw = " ".join(args.text).strip() diff --git a/jrnl/plugins/__init__.py b/jrnl/plugins/__init__.py index 7ce3185e..5314f72f 100644 --- a/jrnl/plugins/__init__.py +++ b/jrnl/plugins/__init__.py @@ -38,8 +38,8 @@ def get_exporter(format): return None -def get_importer(file_format, path, root_config): +def get_importer(file_format, path, root_config, journal): for importer in __importers: if hasattr(importer, "names") and file_format in importer.names: - return importer(path, root_config) + return importer(path, root_config, journal) return None diff --git a/jrnl/plugins/dayone2_importer.py b/jrnl/plugins/dayone2_importer.py index 727f9797..4385e7ba 100644 --- a/jrnl/plugins/dayone2_importer.py +++ b/jrnl/plugins/dayone2_importer.py @@ -9,7 +9,7 @@ class DayOne2Importer(JSONImporter): names = ["dayone2"] extension = "json" - def __init__(self, path, root_config): + def __init__(self, path, root_config, journal): self.type = "dayone2" self.path = path self.keys = [ diff --git a/jrnl/plugins/jrnl_importer.py b/jrnl/plugins/jrnl_importer.py index d7dd3bf1..331892c9 100644 --- a/jrnl/plugins/jrnl_importer.py +++ b/jrnl/plugins/jrnl_importer.py @@ -2,7 +2,9 @@ # encoding: utf-8 import sys -from .. import util + +from pathlib import Path +from jrnl import util class JRNLImporter: @@ -10,17 +12,19 @@ class JRNLImporter: names = ["jrnl"] - def __init__(self, input): - self.input = input - self.import_(self.input) + def __init__(self, path, root_config, journal): + self.path = Path(path[0]) + self.root_config = root_config + self.journal = journal + self.import_(self.journal) - def import_(self, journal, input=None): + def import_(self, journal): """Imports from an existing file if input is specified, and standard input otherwise.""" old_cnt = len(journal.entries) old_entries = journal.entries - if self.input: - with open(input, "r", encoding="utf-8") as f: + if self.journal: + with open(self.path, "r", encoding="utf-8") as f: other_journal_txt = f.read() else: try: