_
File size: 3,575 Bytes
da3eeba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import configparser
# import json
import os
from types import SimpleNamespace

from ia_ui_items import get_inp_model_ids, get_sam_model_ids


class IAConfig:
    SECTIONS = SimpleNamespace(
        DEFAULT=configparser.DEFAULTSECT,
        USER="USER",
    )

    KEYS = SimpleNamespace(
        SAM_MODEL_ID="sam_model_id",
        INP_MODEL_ID="inp_model_id",
    )

    PATHS = SimpleNamespace(
        INI=os.path.join(os.path.dirname(os.path.realpath(__file__)), "ia_config.ini"),
    )

    global_args = {}

    def __init__(self):
        self.ids_dict = {}
        self.ids_dict[IAConfig.KEYS.SAM_MODEL_ID] = {
            "list": get_sam_model_ids(),
            "index": 1,
        }
        self.ids_dict[IAConfig.KEYS.INP_MODEL_ID] = {
            "list": get_inp_model_ids(),
            "index": 0,
        }


ia_config = IAConfig()


def setup_ia_config_ini():
    ia_config_ini = configparser.ConfigParser(defaults={})
    if os.path.isfile(IAConfig.PATHS.INI):
        ia_config_ini.read(IAConfig.PATHS.INI, encoding="utf-8")

    changed = False
    for key, ids_info in ia_config.ids_dict.items():
        if not ia_config_ini.has_option(IAConfig.SECTIONS.DEFAULT, key):
            if len(ids_info["list"]) > ids_info["index"]:
                ia_config_ini[IAConfig.SECTIONS.DEFAULT][key] = ids_info["list"][ids_info["index"]]
                changed = True
        else:
            if len(ids_info["list"]) > ids_info["index"] and ia_config_ini[IAConfig.SECTIONS.DEFAULT][key] != ids_info["list"][ids_info["index"]]:
                ia_config_ini[IAConfig.SECTIONS.DEFAULT][key] = ids_info["list"][ids_info["index"]]
                changed = True

    if changed:
        with open(IAConfig.PATHS.INI, "w", encoding="utf-8") as f:
            ia_config_ini.write(f)


def get_ia_config(key, section=IAConfig.SECTIONS.DEFAULT):
    setup_ia_config_ini()

    ia_config_ini = configparser.ConfigParser(defaults={})
    ia_config_ini.read(IAConfig.PATHS.INI, encoding="utf-8")

    if ia_config_ini.has_option(section, key):
        return ia_config_ini[section][key]

    section = IAConfig.SECTIONS.DEFAULT
    if ia_config_ini.has_option(section, key):
        return ia_config_ini[section][key]

    return None


def get_ia_config_index(key, section=IAConfig.SECTIONS.DEFAULT):
    value = get_ia_config(key, section)

    ids_dict = ia_config.ids_dict
    if value is None:
        if key in ids_dict.keys():
            ids_info = ids_dict[key]
            return ids_info["index"]
        else:
            return 0
    else:
        if key in ids_dict.keys():
            ids_info = ids_dict[key]
            return ids_info["list"].index(value) if value in ids_info["list"] else ids_info["index"]
        else:
            return 0


def set_ia_config(key, value, section=IAConfig.SECTIONS.DEFAULT):
    setup_ia_config_ini()

    ia_config_ini = configparser.ConfigParser(defaults={})
    ia_config_ini.read(IAConfig.PATHS.INI, encoding="utf-8")

    if ia_config_ini.has_option(section, key) and ia_config_ini[section][key] == value:
        return

    if section != IAConfig.SECTIONS.DEFAULT and not ia_config_ini.has_section(section):
        ia_config_ini[section] = {}

    try:
        ia_config_ini[section][key] = value
    except Exception:
        ia_config_ini[section] = {}
        ia_config_ini[section][key] = value

    with open(IAConfig.PATHS.INI, "w", encoding="utf-8") as f:
        ia_config_ini.write(f)