jrnl.config   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 20

6 Functions

Rating   Name   Duplication   Size   Complexity  
A load_config() 0 4 2
A get_journal_name() 0 14 4
A update_config() 0 11 5
A verify_config_colors() 0 19 4
A is_config_json() 0 4 2
A scope_config() 0 15 3
1
import logging
2
import sys
3
4
import colorama
5
import yaml
6
7
from .color import ERROR_COLOR
8
from .color import RESET_COLOR
9
from .output import list_journals
10
11
12
def scope_config(config, journal_name):
13
    if journal_name not in config["journals"]:
14
        return config
15
    config = config.copy()
16
    journal_conf = config["journals"].get(journal_name)
17
    if type(journal_conf) is dict:
18
        # We can override the default config on a by-journal basis
19
        logging.debug(
20
            "Updating configuration with specific journal overrides %s", journal_conf
21
        )
22
        config.update(journal_conf)
23
    else:
24
        # But also just give them a string to point to the journal file
25
        config["journal"] = journal_conf
26
    return config
27
28
29
def verify_config_colors(config):
30
    """
31
    Ensures the keys set for colors are valid colorama.Fore attributes, or "None"
32
    :return: True if all keys are set correctly, False otherwise
33
    """
34
    all_valid_colors = True
35
    for key, color in config["colors"].items():
36
        upper_color = color.upper()
37
        if upper_color == "NONE":
38
            continue
39
        if not getattr(colorama.Fore, upper_color, None):
40
            print(
41
                "[{2}ERROR{3}: {0} set to invalid color: {1}]".format(
42
                    key, color, ERROR_COLOR, RESET_COLOR
43
                ),
44
                file=sys.stderr,
45
            )
46
            all_valid_colors = False
47
    return all_valid_colors
48
49
50
def load_config(config_path):
51
    """Tries to load a config file from YAML."""
52
    with open(config_path) as f:
53
        return yaml.load(f, Loader=yaml.FullLoader)
54
55
56
def is_config_json(config_path):
57
    with open(config_path, "r", encoding="utf-8") as f:
58
        config_file = f.read()
59
    return config_file.strip().startswith("{")
60
61
62
def update_config(config, new_config, scope, force_local=False):
63
    """Updates a config dict with new values - either global if scope is None
64
    or config['journals'][scope] is just a string pointing to a journal file,
65
    or within the scope"""
66
    if scope and type(config["journals"][scope]) is dict:  # Update to journal specific
67
        config["journals"][scope].update(new_config)
68
    elif scope and force_local:  # Convert to dict
69
        config["journals"][scope] = {"journal": config["journals"][scope]}
70
        config["journals"][scope].update(new_config)
71
    else:
72
        config.update(new_config)
73
74
75
def get_journal_name(args, config):
76
    from . import install
77
78
    args.journal_name = install.DEFAULT_JOURNAL_KEY
79
    if args.text and args.text[0] in config["journals"]:
80
        args.journal_name = args.text[0]
81
        args.text = args.text[1:]
82
    elif install.DEFAULT_JOURNAL_KEY not in config["journals"]:
83
        print("No default journal configured.", file=sys.stderr)
84
        print(list_journals(config), file=sys.stderr)
85
        sys.exit(1)
86
87
    logging.debug("Using journal name: %s", args.journal_name)
88
    return args
89