menderbot.config   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 12

5 Functions

Rating   Name   Duplication   Size   Complexity  
A has_config() 0 3 1
A get_config_path() 0 5 2
A load_config() 0 11 4
A create_default_config() 0 11 4
A has_llm_consent() 0 2 1
1
from os.path import exists, join
2
3
import yaml
4
5
from menderbot.git_client import git_show_top_level
6
7
8
def get_config_path():
9
    git_path = git_show_top_level()
10
    if not git_path:
11
        return None
12
    return join(git_path, ".menderbot-config.yaml")
13
14
15
DEFAULT_CONFIG_YAML = """
16
# Menderbot may send source code in this repo to the enabled APIs.
17
# If this includes propietary information, make sure you are authorized to do so.
18
# Set consent to yes if this is OK.
19
consent: no
20
apis:
21
    openai:
22
        enabled: yes
23
        api_key_env_var: OPENAI_API_KEY
24
        # organization_env_var: OPENAI_ORGANIZATION
25
        # api_base: https://api.openai.com/v1
26
"""
27
28
29
def has_llm_consent():
30
    return has_config() and load_config()["consent"]
31
32
33
def has_config():
34
    config_path = get_config_path()
35
    return config_path and exists(get_config_path())
36
37
38
def create_default_config(message="Writing default config"):
39
    config_path = get_config_path()
40
    if exists(config_path):
41
        # Should not have been called when file exists
42
        return
43
    if not config_path:
44
        print("Cannot resolve config path. Not in git repo?")
45
        return
46
    with open(config_path, "w", encoding="utf-8") as conf_file:
47
        print(message)
48
        conf_file.write(DEFAULT_CONFIG_YAML)
49
50
51
def load_config() -> dict:
52
    loader = yaml.SafeLoader
53
    config_path = get_config_path()
54
    if not config_path:
55
        print("Cannot resolve config path. Not in git repo?")
56
        # Maybe should raise?
57
        return yaml.load("", Loader=loader)
58
    if not has_config():
59
        create_default_config()
60
    with open(config_path, "rb") as conf_file:
61
        return yaml.load(conf_file, Loader=loader)
62