diff --git a/features/data/journals/not_dayone2.json b/features/data/journals/not_dayone2.json new file mode 100644 index 00000000..97bf79d1 --- /dev/null +++ b/features/data/journals/not_dayone2.json @@ -0,0 +1,17 @@ +{ + "articles": [ + { + "audio_files": [], + "creation_date": "2020-01-10T12:22:12Z", + "photo": [], + "favourites": false, + "tag": [], + "body": "Entry Number One.", + "time_zone": "Europe/London", + "id": "EBB5A5F4057F461E8F176E18AB7E0493" + } + ], + "metadata": { + "version": "1.2" + } +} \ No newline at end of file diff --git a/features/dayone2.feature b/features/dayone2.feature index 5c809479..fdc1e346 100644 --- a/features/dayone2.feature +++ b/features/dayone2.feature @@ -2,14 +2,26 @@ Feature: Day One 2.0 implementation details. Scenario: Loading a Day One 2.0 journal - Given we use the config "dayone2.yaml" - When we run "jrnl -n 2" + Given we use the config "basic.yaml" + When we run "jrnl --import dayone2 features/data/journals/dayone2.json" Then we should get no error - and the output should be - """ - 2020-01-10 12:21 Entry Number Two. - | And a bit of text over here. + and the output should contain "Journal exported to" - 2020-01-10 12:22 Entry Number One. - """ + Scenario: Day One 2.0 schema validation fails + Given we use the config "basic.yaml" + When we run "jrnl --import dayone2 features/data/journals/not_dayone2.json" + Then we should get no error + and the output should contain "not the expected Day One 2 format." + Scenario: Verify conversion to jrnl + Given we use the config "basic.yaml" + When we run "jrnl --import dayone2 features/data/journals/dayone2.json" + When we run "jrnl -ls" + Then the output should be + """ + 2013-06-09 15:39 My first entry. + | Everything is alright + + 2013-06-10 15:40 Life is good. + | But I'm better. + """ \ No newline at end of file diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 11788d3d..e5bf4ecc 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -385,12 +385,6 @@ def open_journal(name, config, legacy=False): sys.exit(1) - elif config["journal"].strip("/").endswith(".json"): - # Loads a Day One v2.0 journal - from .dayone2 import DayOne2 - - return DayOne2(**config).open() - if not config["encrypt"]: if legacy: return LegacyJournal(name, **config).open() diff --git a/jrnl/cli.py b/jrnl/cli.py index 8104259d..2ba696a4 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -133,8 +133,8 @@ def parse_args(args=None): metavar="TYPE", dest="import_", choices=plugins.IMPORT_FORMATS, - help="Import entries into your journal. TYPE can be one of "\ - "{0}".format(plugins.util.oxford_list(plugins.IMPORT_FORMATS)), + help="Import entries into your journal. TYPE can be one of " + "{0}".format(plugins.util.oxford_list(plugins.IMPORT_FORMATS)), default=False, ) exporting.add_argument( diff --git a/jrnl/plugins/__init__.py b/jrnl/plugins/__init__.py index cb1c1ff9..aa53d248 100644 --- a/jrnl/plugins/__init__.py +++ b/jrnl/plugins/__init__.py @@ -22,8 +22,7 @@ __exporters = [ FancyExporter, ] + template_exporters -__importers = [DayOne2Importer, - JRNLImporter] +__importers = [DayOne2Importer, JRNLImporter] __exporter_types = {name: plugin for plugin in __exporters for name in plugin.names} __importer_types = {name: plugin for plugin in __importers for name in plugin.names} diff --git a/jrnl/plugins/dayone2_importer.py b/jrnl/plugins/dayone2_importer.py index dea3df7e..0975304c 100644 --- a/jrnl/plugins/dayone2_importer.py +++ b/jrnl/plugins/dayone2_importer.py @@ -10,10 +10,18 @@ class DayOne2Importer(JSONImporter): extension = "json" def __init__(self, path): - self.type = 'Day One 2' + self.type = "Day One 2" self.path = path - self.keys = ['audios', 'creationDate', 'photos', - 'starred', 'tags', 'text', 'timeZone', 'uuid'] + self.keys = [ + "audios", + "creationDate", + "photos", + "starred", + "tags", + "text", + "timeZone", + "uuid", + ] JSONImporter.__init__(self) self.convert_journal() @@ -21,14 +29,16 @@ class DayOne2Importer(JSONImporter): print(self._convert()) def validate_schema(self): - for key in self.json['entries'][0]: - try: - assert key in self.keys + try: + for key in self.json["entries"][0]: + try: + assert key in self.keys + except AssertionError: + print(f"{self.path} is not the expected Day One 2 format.") + return True - except AssertionError: - raise KeyError(f"{self.path} is not the expected Day One 2 format.") - print(f"{self.path} validated as Day One 2.") - return True + except KeyError: + print(f"{self.path} is not the expected Day One 2 format.") def parse_json(self): entries = [] diff --git a/jrnl/plugins/json_importer.py b/jrnl/plugins/json_importer.py index 6ccb3b63..228be089 100644 --- a/jrnl/plugins/json_importer.py +++ b/jrnl/plugins/json_importer.py @@ -19,24 +19,26 @@ class JSONImporter(PlainJournal, TextExporter): self.json = self.import_file() def __str__(self): - return f"{self.type} journal with {len(self.journal)} " \ - f"entries located at {self.path}" + return ( + f"{self.type} journal with {len(self.journal)} " + f"entries located at {self.path}" + ) def _convert(self): if self.validate_schema(): self.data = self.parse_json() - self.create_file(self.filename + '.txt') - return self.export(self.journal, self.filename + '.txt') + self.create_file(self.filename + ".txt") + return self.export(self.journal, self.filename + ".txt") def import_file(self): """Reads a JSON file and returns a dict.""" - if os.path.exists(self.path) and Path(self.path).suffix == '.json': + if os.path.exists(self.path) and Path(self.path).suffix == ".json": try: with open(self.path) as f: return json.load(f) except json.JSONDecodeError: print(f"{self.path} is not valid JSON.") - elif Path(self.path).suffix != '.json': + elif Path(self.path).suffix != ".json": print(f"{self.path} must be a JSON file.") else: print(f"{self.path} does not exist.")