From 774355971a5415264b1f42cbb6f5d1236d608615 Mon Sep 17 00:00:00 2001 From: dbxnr Date: Tue, 11 Feb 2020 21:29:55 +0000 Subject: [PATCH] imported journal gets added to config --- jrnl/plugins/dayone2_importer.py | 2 +- jrnl/plugins/json_importer.py | 4 +++- jrnl/plugins/text_exporter.py | 3 ++- jrnl/plugins/util.py | 17 +++++++++++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/jrnl/plugins/dayone2_importer.py b/jrnl/plugins/dayone2_importer.py index 0975304c..5afd131b 100644 --- a/jrnl/plugins/dayone2_importer.py +++ b/jrnl/plugins/dayone2_importer.py @@ -10,7 +10,7 @@ class DayOne2Importer(JSONImporter): extension = "json" def __init__(self, path): - self.type = "Day One 2" + self.type = "DayOne2" self.path = path self.keys = [ "audios", diff --git a/jrnl/plugins/json_importer.py b/jrnl/plugins/json_importer.py index 228be089..be954f70 100644 --- a/jrnl/plugins/json_importer.py +++ b/jrnl/plugins/json_importer.py @@ -6,6 +6,7 @@ from pathlib import Path from jrnl.Journal import PlainJournal from jrnl.plugins.text_exporter import TextExporter +from jrnl.plugins.util import add_journal_to_config class JSONImporter(PlainJournal, TextExporter): @@ -28,7 +29,8 @@ class JSONImporter(PlainJournal, TextExporter): if self.validate_schema(): self.data = self.parse_json() self.create_file(self.filename + ".txt") - return self.export(self.journal, self.filename + ".txt") + new_path = self.export(self.journal, self.filename + ".txt") + add_journal_to_config(self.type, new_path) def import_file(self): """Reads a JSON file and returns a dict.""" diff --git a/jrnl/plugins/text_exporter.py b/jrnl/plugins/text_exporter.py index 6f2a4531..fe1ba987 100644 --- a/jrnl/plugins/text_exporter.py +++ b/jrnl/plugins/text_exporter.py @@ -28,7 +28,8 @@ class TextExporter: try: with open(path, "w", encoding="utf-8") as f: f.write(cls.export_journal(journal)) - return f"[Journal exported to {path}]" + print(f"[Journal exported to {path}]") + return path except IOError as e: return f"[{ERROR_COLOR}ERROR{RESET_COLOR}: {e.filename} {e.strerror}]" diff --git a/jrnl/plugins/util.py b/jrnl/plugins/util.py index a030f8d3..c0bc43fc 100644 --- a/jrnl/plugins/util.py +++ b/jrnl/plugins/util.py @@ -1,5 +1,9 @@ #!/usr/bin/env python # encoding: utf-8 +import yaml + +from jrnl.util import load_config +from jrnl.install import CONFIG_FILE_PATH, CONFIG_FILE_PATH_FALLBACK def get_tags_count(journal): @@ -23,3 +27,16 @@ def oxford_list(lst): return lst[0] + " or " + lst[1] else: return ", ".join(lst[:-1]) + ", or " + lst[-1] + + +def add_journal_to_config(name, path): + data = {} + try: + data = load_config(CONFIG_FILE_PATH) + except FileNotFoundError: + try: + data = load_config(CONFIG_FILE_PATH_FALLBACK) + except FileNotFoundError: + print("Config file not found.") + finally: + data["journals"][name] = path