imported journal gets added to config

This commit is contained in:
dbxnr 2020-02-11 21:29:55 +00:00
parent 0c73448516
commit 774355971a
4 changed files with 23 additions and 3 deletions

View file

@ -10,7 +10,7 @@ class DayOne2Importer(JSONImporter):
extension = "json" extension = "json"
def __init__(self, path): def __init__(self, path):
self.type = "Day One 2" self.type = "DayOne2"
self.path = path self.path = path
self.keys = [ self.keys = [
"audios", "audios",

View file

@ -6,6 +6,7 @@ from pathlib import Path
from jrnl.Journal import PlainJournal from jrnl.Journal import PlainJournal
from jrnl.plugins.text_exporter import TextExporter from jrnl.plugins.text_exporter import TextExporter
from jrnl.plugins.util import add_journal_to_config
class JSONImporter(PlainJournal, TextExporter): class JSONImporter(PlainJournal, TextExporter):
@ -28,7 +29,8 @@ class JSONImporter(PlainJournal, TextExporter):
if self.validate_schema(): if self.validate_schema():
self.data = self.parse_json() self.data = self.parse_json()
self.create_file(self.filename + ".txt") 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): def import_file(self):
"""Reads a JSON file and returns a dict.""" """Reads a JSON file and returns a dict."""

View file

@ -28,7 +28,8 @@ class TextExporter:
try: try:
with open(path, "w", encoding="utf-8") as f: with open(path, "w", encoding="utf-8") as f:
f.write(cls.export_journal(journal)) f.write(cls.export_journal(journal))
return f"[Journal exported to {path}]" print(f"[Journal exported to {path}]")
return path
except IOError as e: except IOError as e:
return f"[{ERROR_COLOR}ERROR{RESET_COLOR}: {e.filename} {e.strerror}]" return f"[{ERROR_COLOR}ERROR{RESET_COLOR}: {e.filename} {e.strerror}]"

View file

@ -1,5 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # 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): def get_tags_count(journal):
@ -23,3 +27,16 @@ def oxford_list(lst):
return lst[0] + " or " + lst[1] return lst[0] + " or " + lst[1]
else: else:
return ", ".join(lst[:-1]) + ", or " + lst[-1] 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