jrnl.plugins.jrnl_importer.JRNLImporter.import_()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nop 2
dl 0
loc 21
rs 9.55
c 0
b 0
f 0
1
#!/usr/bin/env python
2
# encoding: utf-8
3
4
import sys
5
6
7
class JRNLImporter:
8
    """This plugin imports entries from other jrnl files."""
9
10
    names = ["jrnl"]
11
12
    @staticmethod
13
    def import_(journal, input=None):
14
        """Imports from an existing file if input is specified, and
15
        standard input otherwise."""
16
        old_cnt = len(journal.entries)
17
        if input:
18
            with open(input, "r", encoding="utf-8") as f:
19
                other_journal_txt = f.read()
20
        else:
21
            try:
22
                other_journal_txt = sys.stdin.read()
23
            except KeyboardInterrupt:
24
                print("[Entries NOT imported into journal.]", file=sys.stderr)
25
                sys.exit(0)
26
        journal.import_(other_journal_txt)
27
        new_cnt = len(journal.entries)
28
        print(
29
            "[{} imported to {} journal]".format(new_cnt - old_cnt, journal.name),
30
            file=sys.stderr,
31
        )
32
        journal.write()
33