mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-20 21:18:32 +02:00
add some tests, black formatting
This commit is contained in:
parent
608f920d8f
commit
0c73448516
7 changed files with 68 additions and 34 deletions
17
features/data/journals/not_dayone2.json
Normal file
17
features/data/journals/not_dayone2.json
Normal file
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,14 +2,26 @@ Feature: Day One 2.0 implementation details.
|
||||||
|
|
||||||
|
|
||||||
Scenario: Loading a Day One 2.0 journal
|
Scenario: Loading a Day One 2.0 journal
|
||||||
Given we use the config "dayone2.yaml"
|
Given we use the config "basic.yaml"
|
||||||
When we run "jrnl -n 2"
|
When we run "jrnl --import dayone2 features/data/journals/dayone2.json"
|
||||||
Then we should get no error
|
Then we should get no error
|
||||||
and the output should be
|
and the output should contain "Journal exported to"
|
||||||
"""
|
|
||||||
2020-01-10 12:21 Entry Number Two.
|
|
||||||
| And a bit of text over here.
|
|
||||||
|
|
||||||
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.
|
||||||
|
"""
|
|
@ -385,12 +385,6 @@ def open_journal(name, config, legacy=False):
|
||||||
|
|
||||||
sys.exit(1)
|
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 not config["encrypt"]:
|
||||||
if legacy:
|
if legacy:
|
||||||
return LegacyJournal(name, **config).open()
|
return LegacyJournal(name, **config).open()
|
||||||
|
|
|
@ -133,8 +133,8 @@ def parse_args(args=None):
|
||||||
metavar="TYPE",
|
metavar="TYPE",
|
||||||
dest="import_",
|
dest="import_",
|
||||||
choices=plugins.IMPORT_FORMATS,
|
choices=plugins.IMPORT_FORMATS,
|
||||||
help="Import entries into your journal. TYPE can be one of "\
|
help="Import entries into your journal. TYPE can be one of "
|
||||||
"{0}".format(plugins.util.oxford_list(plugins.IMPORT_FORMATS)),
|
"{0}".format(plugins.util.oxford_list(plugins.IMPORT_FORMATS)),
|
||||||
default=False,
|
default=False,
|
||||||
)
|
)
|
||||||
exporting.add_argument(
|
exporting.add_argument(
|
||||||
|
|
|
@ -22,8 +22,7 @@ __exporters = [
|
||||||
FancyExporter,
|
FancyExporter,
|
||||||
] + template_exporters
|
] + template_exporters
|
||||||
|
|
||||||
__importers = [DayOne2Importer,
|
__importers = [DayOne2Importer, JRNLImporter]
|
||||||
JRNLImporter]
|
|
||||||
|
|
||||||
__exporter_types = {name: plugin for plugin in __exporters for name in plugin.names}
|
__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}
|
__importer_types = {name: plugin for plugin in __importers for name in plugin.names}
|
||||||
|
|
|
@ -10,10 +10,18 @@ class DayOne2Importer(JSONImporter):
|
||||||
extension = "json"
|
extension = "json"
|
||||||
|
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
self.type = 'Day One 2'
|
self.type = "Day One 2"
|
||||||
self.path = path
|
self.path = path
|
||||||
self.keys = ['audios', 'creationDate', 'photos',
|
self.keys = [
|
||||||
'starred', 'tags', 'text', 'timeZone', 'uuid']
|
"audios",
|
||||||
|
"creationDate",
|
||||||
|
"photos",
|
||||||
|
"starred",
|
||||||
|
"tags",
|
||||||
|
"text",
|
||||||
|
"timeZone",
|
||||||
|
"uuid",
|
||||||
|
]
|
||||||
JSONImporter.__init__(self)
|
JSONImporter.__init__(self)
|
||||||
self.convert_journal()
|
self.convert_journal()
|
||||||
|
|
||||||
|
@ -21,14 +29,16 @@ class DayOne2Importer(JSONImporter):
|
||||||
print(self._convert())
|
print(self._convert())
|
||||||
|
|
||||||
def validate_schema(self):
|
def validate_schema(self):
|
||||||
for key in self.json['entries'][0]:
|
try:
|
||||||
try:
|
for key in self.json["entries"][0]:
|
||||||
assert key in self.keys
|
try:
|
||||||
|
assert key in self.keys
|
||||||
|
except AssertionError:
|
||||||
|
print(f"{self.path} is not the expected Day One 2 format.")
|
||||||
|
return True
|
||||||
|
|
||||||
except AssertionError:
|
except KeyError:
|
||||||
raise KeyError(f"{self.path} is not the expected Day One 2 format.")
|
print(f"{self.path} is not the expected Day One 2 format.")
|
||||||
print(f"{self.path} validated as Day One 2.")
|
|
||||||
return True
|
|
||||||
|
|
||||||
def parse_json(self):
|
def parse_json(self):
|
||||||
entries = []
|
entries = []
|
||||||
|
|
|
@ -19,24 +19,26 @@ class JSONImporter(PlainJournal, TextExporter):
|
||||||
self.json = self.import_file()
|
self.json = self.import_file()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.type} journal with {len(self.journal)} " \
|
return (
|
||||||
f"entries located at {self.path}"
|
f"{self.type} journal with {len(self.journal)} "
|
||||||
|
f"entries located at {self.path}"
|
||||||
|
)
|
||||||
|
|
||||||
def _convert(self):
|
def _convert(self):
|
||||||
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')
|
return self.export(self.journal, self.filename + ".txt")
|
||||||
|
|
||||||
def import_file(self):
|
def import_file(self):
|
||||||
"""Reads a JSON file and returns a dict."""
|
"""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:
|
try:
|
||||||
with open(self.path) as f:
|
with open(self.path) as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
print(f"{self.path} is not valid JSON.")
|
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.")
|
print(f"{self.path} must be a JSON file.")
|
||||||
else:
|
else:
|
||||||
print(f"{self.path} does not exist.")
|
print(f"{self.path} does not exist.")
|
||||||
|
|
Loading…
Add table
Reference in a new issue