jrnl.commands.postconfig_import()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 3
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
"""
2
Functions in this file are standalone commands. All standalone commands are split into
3
two categories depending on whether they require the config to be loaded to be able to
4
run.
5
6
1. "preconfig" commands don't require the config at all, and can be run before the
7
   config has been loaded.
8
2. "postconfig" commands require to config to have already been loaded, parsed, and
9
   scoped before they can be run.
10
11
Also, please note that all (non-builtin) imports should be scoped to each function to
12
avoid any possible overhead for these standalone commands.
13
"""
14
import platform
15
import sys
16
17
18
def preconfig_diagnostic(_):
19
    from jrnl import __version__
20
21
    print(
22
        f"jrnl: {__version__}\n"
23
        f"Python: {sys.version}\n"
24
        f"OS: {platform.system()} {platform.release()}"
25
    )
26
27
28
def preconfig_version(_):
29
    from jrnl import __title__
30
    from jrnl import __version__
31
32
    version_str = f"{__title__} version {__version__}"
33
    print(version_str)
34
35
36
def postconfig_list(config, **kwargs):
37
    from .output import list_journals
38
39
    print(list_journals(config))
40
41
42
def postconfig_import(args, config, **kwargs):
43
    from .Journal import open_journal
44
    from .plugins import get_importer
45
46
    # Requires opening the journal
47
    journal = open_journal(args.journal_name, config)
48
49
    format = args.export if args.export else "jrnl"
50
    get_importer(format).import_(journal, args.filename)
51
52
53 View Code Duplication
def postconfig_encrypt(args, config, original_config, **kwargs):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
54
    """
55
    Encrypt a journal in place, or optionally to a new file
56
    """
57
    from .EncryptedJournal import EncryptedJournal
58
    from .Journal import open_journal
59
    from .config import update_config
60
    from .install import save_config
61
62
    # Open the journal
63
    journal = open_journal(args.journal_name, config)
64
65
    journal.config["encrypt"] = True
66
67
    new_journal = EncryptedJournal.from_journal(journal)
68
    new_journal.write(args.filename)
69
70
    print(
71
        f"Journal encrypted to {args.filename or new_journal.config['journal']}.",
72
        file=sys.stderr,
73
    )
74
75
    # Update the config, if we encrypted in place
76
    if not args.filename:
77
        update_config(
78
            original_config, {"encrypt": True}, args.journal_name, force_local=True
79
        )
80
        save_config(original_config)
81
82
83 View Code Duplication
def postconfig_decrypt(args, config, original_config, **kwargs):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
84
    """ Decrypts into new file. If filename is not set, we encrypt the journal file itself. """
85
    from .Journal import PlainJournal
86
    from .Journal import open_journal
87
    from .config import update_config
88
    from .install import save_config
89
90
    journal = open_journal(args.journal_name, config)
91
    journal.config["encrypt"] = False
92
93
    new_journal = PlainJournal.from_journal(journal)
94
    new_journal.write(args.filename)
95
    print(
96
        f"Journal decrypted to {args.filename or new_journal.config['journal']}.",
97
        file=sys.stderr,
98
    )
99
100
    # Update the config, if we decrypted in place
101
    if not args.filename:
102
        update_config(
103
            original_config, {"encrypt": False}, args.journal_name, force_local=True
104
        )
105
        save_config(original_config)
106