-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsts.py
More file actions
74 lines (64 loc) · 2.68 KB
/
Copy pathconsts.py
File metadata and controls
74 lines (64 loc) · 2.68 KB
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
from platformdirs import user_config_dir
import sys, os, yaml, shutil
TERMINAL = {
"warn": '\033[93m',
"error": '\033[91m',
"green": '\033[92m',
"normal": '\033[0m'
}
found_valid_config = False
try_setting = True
if getattr(sys, 'frozen', False):
BASE_PATH = sys._MEIPASS
else:
BASE_PATH = os.path.abspath(os.path.dirname(__file__))
BASE_CONFIG = os.path.join(BASE_PATH, "basic.yaml")
BASE_FONT = os.path.join(BASE_PATH, "JetBrainsMonoNerdFont-Regular.ttf")
CONFIG_DIR = user_config_dir("OKey")
CONFIG_FILE = os.path.join(CONFIG_DIR, 'config.yaml')
MAIN_FONT = os.path.join(CONFIG_DIR, 'JetBrainsMonoNerdFont-Regular.ttf')
with open(BASE_CONFIG, 'r') as file:
BASE_SETTINGS = yaml.load(file, Loader=yaml.SafeLoader)
def replace_placeholder_path():
if os.path.isfile(CONFIG_FILE):
with open(CONFIG_FILE, 'r') as file:
content = file.read()
new_content = content.replace("PLACEHOLDER_FONT_PATH", MAIN_FONT)
with open(CONFIG_FILE, 'w') as file:
file.write(new_content)
def make_base_config():
os.makedirs(CONFIG_DIR, exist_ok=True) # safely create directory if needed
shutil.copyfile(BASE_CONFIG, CONFIG_FILE)
replace_placeholder_path()
shutil.copyfile(BASE_FONT, MAIN_FONT)
if not os.path.isfile(CONFIG_FILE):
print(f"{CONFIG_FILE} not found. Making basic config...")
make_base_config()
# Replace PLACEHOLDER_FONT_PATH with real path
replace_placeholder_path()
with open(CONFIG_FILE, 'r') as file:
SETTINGS = yaml.load(file, Loader=yaml.SafeLoader)
for setting in BASE_SETTINGS:
if setting not in SETTINGS:
ans = input(f"{setting} not found in config file, backup old config and create a new one(y/n)? ")
while ans.lower() not in ['yes', 'no', 'y', 'n']:
print("Please input y/n/yes/no")
ans = input(f"{setting} not found in config file, backup old config and create a new one(y/n)? ")
if 'y' in ans.lower():
print("Backing up config...")
backup_num = 1
backup_file = os.path.join(CONFIG_DIR, f"config_backup_{backup_num}.yaml")
while os.path.isfile(backup_file):
backup_num += 1
backup_file = os.path.join(CONFIG_DIR, f"config_backup_{backup_num}.yaml")
shutil.copyfile(CONFIG_FILE, backup_file)
print("Making basic config...")
make_base_config()
break
with open(CONFIG_FILE, 'r') as file:
SETTINGS = yaml.load(file, Loader=yaml.SafeLoader)
for setting in BASE_SETTINGS:
if setting not in SETTINGS:
print("Invalid config file found, using defaults for now")
SETTINGS = BASE_SETTINGS
SETTINGS["font_path"] = BASE_FONT