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
|
||||
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.
|
||||
"""
|
|
@ -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()
|
||||
|
|
|
@ -133,7 +133,7 @@ def parse_args(args=None):
|
|||
metavar="TYPE",
|
||||
dest="import_",
|
||||
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)),
|
||||
default=False,
|
||||
)
|
||||
|
|
|
@ -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}
|
||||
|
|
|
@ -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,15 +29,17 @@ class DayOne2Importer(JSONImporter):
|
|||
print(self._convert())
|
||||
|
||||
def validate_schema(self):
|
||||
for key in self.json['entries'][0]:
|
||||
try:
|
||||
for key in self.json["entries"][0]:
|
||||
try:
|
||||
assert key in self.keys
|
||||
|
||||
except AssertionError:
|
||||
raise KeyError(f"{self.path} is not the expected Day One 2 format.")
|
||||
print(f"{self.path} validated as Day One 2.")
|
||||
print(f"{self.path} is not the expected Day One 2 format.")
|
||||
return True
|
||||
|
||||
except KeyError:
|
||||
print(f"{self.path} is not the expected Day One 2 format.")
|
||||
|
||||
def parse_json(self):
|
||||
entries = []
|
||||
for entry in self.json["entries"]:
|
||||
|
|
|
@ -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)} " \
|
||||
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.")
|
||||
|
|
Loading…
Add table
Reference in a new issue