This repository was archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig.py
More file actions
100 lines (86 loc) · 3.03 KB
/
config.py
File metadata and controls
100 lines (86 loc) · 3.03 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
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
# Copyright (C) Fabulously Optimized 2023
# Licensed under the MIT License. The full license text can be found at https://github.com/Fabulously-Optimized/vanilla-installer/blob/main/LICENSE.md.
"""
The Configuration System for Vanilla Installer.
"""
import logging
import os
import platform
from pathlib import Path
import minecraft_launcher_lib as mll
import tomlkit
from tomlkit import toml_file
from vanilla_installer import log
logger = log.setup_logging()
FILE_PATH = str(Path("vanilla_installer.toml").resolve())
def init():
"""
Initialise the config file with the default values.
This should **only** be run if the config file does not exist, as it will delete the file
if it already exists, which will overwrite any user-set settings - which you don't want to do for obvious reasons.
"""
config_file = tomlkit.document()
# TODO: Add actual docs and URL for this
config_file.add(tomlkit.comment("Read more about this file at <insert_wiki_url>"))
config_file.add(tomlkit.nl())
config = tomlkit.table()
# maybe call darkdetect from here?
# might want to just leave this alone to be honest
config.add("theme", "dark")
config.add(
tomlkit.comment(
"Whether to use FO's directory or ask the user for a custom one."
)
)
config.add("fo_dir", True)
config.add(tomlkit.comment("This is only used is fo_dir (above) is false."))
config.add("path", mll.utils.get_minecraft_directory())
if platform.system() == "Windows":
font = "Inter Regular"
else:
font = "Inter"
config.add(
tomlkit.comment(
"Only Inter (Inter Regular on Windows) and OpenDyslexic are currently supported."
)
)
config.add("font", font)
config_file.add("config", config)
file = toml_file.TOMLFile(FILE_PATH)
try:
file.write(config_file)
except FileExistsError:
logger.exception("Config file already exists, overwriting.")
os.remove(FILE_PATH)
file.write(config_file)
def read() -> dict:
"""
Read from the config file.
Returns:
dict: The config file, reformatted into a dict-like format.
"""
config_file = toml_file.TOMLFile(FILE_PATH)
try:
return config_file.read()
except FileNotFoundError:
logger.exception("No config file found, (re-)initializing.")
init()
return config_file.read()
def write(key: str, value: str) -> None:
"""
Write to the config file.
This is a mostly internal function used to write to the config file from a user interface.
Args:
key (str): The key to write to.
value (str): The value to write to `key`.
"""
config_file = read()
try:
# as this is the only category we use right now, this is hardcoded
config_file["config"][key]
except KeyError as e:
logger.critical("Could not find key.")
raise KeyError("Invalid key.") from e
# read 5 lines above
config_file["config"][key] = value
toml_file.TOMLFile(FILE_PATH).write(config_file)