Merge pull request #300 from gcgoogle/add-import-plugin-support

Added support for importing entries for 2.0rc1.
This commit is contained in:
Manuel Ebert 2014-10-30 15:03:47 +01:00
commit 163dc04481
5 changed files with 64 additions and 6 deletions

View file

@ -53,3 +53,10 @@ def get_exporter(format):
if hasattr(exporter, "names") and format in exporter.names:
return exporter
return None
def get_importer(format):
for importer in BaseImporter.PLUGINS:
if hasattr(importer, "names") and format in importer.names:
return importer
return None

View file

@ -0,0 +1,32 @@
#!/usr/bin/env python
# encoding: utf-8
from __future__ import absolute_import, unicode_literals
import codecs
import sys
from . import BaseImporter
from .. import util
class JRNLImporter(BaseImporter):
"""This plugin imports entries from other jrnl files."""
names = ["jrnl"]
@staticmethod
def import_(journal, input=None):
"""Imports from an existing file if input is specified, and
standard input otherwise."""
old_cnt = len(journal.entries)
old_entries = journal.entries
if input:
with codecs.open(input, "r", "utf-8") as f:
other_journal_txt = f.read()
else:
try:
other_journal_txt = util.py23_read()
except KeyboardInterrupt:
util.prompt("[Entries NOT imported into journal.]")
sys.exit(0)
journal.import_(other_journal_txt)
new_cnt = len(journal.entries)
util.prompt("[{0} imported to {1} journal]".format(new_cnt - old_cnt, journal.name))
journal.write()