Sample Importer: Handle data consistently whether from file or stdin

Importers: handle writing the journal to disk ourselves (rather than delegating to the plugin)
Plugins: First pass at testing custom importer
This commit is contained in:
MinchinWeb 2021-06-26 22:09:46 -06:00 committed by Jonathan Wren
parent 183795d3f5
commit 7f5fbca658
5 changed files with 33 additions and 10 deletions

View file

@ -0,0 +1,15 @@
{
"entries" :
[
{
"date" : "2013-06-09 15:39",
"title" : "My first entry.",
"body" : "Everything is alright"
},
{
"date" : "2013-06-10 15:40",
"title" : "Life is good.",
"body" : "But I'm better."
}
]
}

View file

@ -5,7 +5,7 @@ Feature: Functionality of Importer and Exporter Plugins
Given We use the config "basic_onefile.yaml" Given We use the config "basic_onefile.yaml"
When We run "jrnl --version" When We run "jrnl --version"
Then the output should contain pyproject.toml version Then the output should contain pyproject.toml version
And The output should contain "<plugin_name> : <version> from jrnl.<source>.<type>.<filename>" And the output should contain "<plugin_name> : <version> from jrnl.<source>.<type>.<filename>"
And the output should not contain ".contrib." And the output should not contain ".contrib."
Examples: Examples:
@ -31,7 +31,8 @@ Feature: Functionality of Importer and Exporter Plugins
Given We use the config "basic_onefile.yaml" Given We use the config "basic_onefile.yaml"
When We run "jrnl --version" When We run "jrnl --version"
Then the output should contain pyproject.toml version Then the output should contain pyproject.toml version
And The output should contain "<plugin_name> : <version> from jrnl.<source>.<type>.<filename>" And the output should contain "<plugin_name> : <version> from jrnl.<source>.<type>.<filename>"
Examples: Examples:
| plugin_name | version | source | type | filename | | plugin_name | version | source | type | filename |
| jrnl | <pyproject.toml version> | plugins | importer | jrnl | | jrnl | <pyproject.toml version> | plugins | importer | jrnl |
@ -65,9 +66,15 @@ Feature: Functionality of Importer and Exporter Plugins
| json | <pyproject.toml version> | plugins | exporter | json | | json | <pyproject.toml version> | plugins | exporter | json |
| txt | <pyproject.toml version> | plugins | exporter | text | | txt | <pyproject.toml version> | plugins | exporter | text |
@skip_only_with_external_plugins
Scenario Outline: Custom JSON Import
Given we use the config "simple.yaml"
When we run "jrnl --import ./features/data/simple_import.json"
Then the journal should contain "My first entry."
And the journal should contain "Life is good."
@skip_only_with_external_plugins @skip_only_with_external_plugins
Scenario Outline: JSON format Scenario Outline: Custom JSON Export
Given we use the config "<config>.yaml" Given we use the config "<config>.yaml"
And we use the password "test" if prompted And we use the password "test" if prompted
When we run "jrnl --format json" When we run "jrnl --format json"

View file

@ -75,6 +75,7 @@ def postconfig_import(args, config, **kwargs):
format = args.export if args.export else "jrnl" format = args.export if args.export else "jrnl"
get_importer(format).import_(journal, args.filename) get_importer(format).import_(journal, args.filename)
journal.write()
def postconfig_encrypt(args, config, original_config, **kwargs): def postconfig_encrypt(args, config, original_config, **kwargs):

View file

@ -37,4 +37,3 @@ class Importer(BaseImporter):
), ),
file=sys.stderr, file=sys.stderr,
) )
journal.write()

View file

@ -27,13 +27,14 @@ class Importer(BaseImporter):
data = json.loads(f) data = json.loads(f)
else: else:
try: try:
data = sys.stdin.read() f = sys.stdin.read()
except KeyboardInterrupt: except KeyboardInterrupt:
print( print(
"[Entries NOT imported into journal.]", "[Entries NOT imported into journal.]",
file=sys.stderr, file=sys.stderr,
) )
sys.exit(0) sys.exit(0)
data = json.loads(f)
for json_entry in data: for json_entry in data:
raw = json_entry["title"] + "/n" + json_entry["body"] raw = json_entry["title"] + "/n" + json_entry["body"]