40 lines
977 B
Python
40 lines
977 B
Python
|
|
from pathlib import Path
|
||
|
|
import os, subprocess
|
||
|
|
import configparser
|
||
|
|
|
||
|
|
|
||
|
|
class ConfigHandler:
|
||
|
|
def __init__(self):
|
||
|
|
self.config = configparser.ConfigParser()
|
||
|
|
|
||
|
|
self.config_dir = self.ensure_hidden_config_dir()
|
||
|
|
self.load_settings(self.config_dir)
|
||
|
|
|
||
|
|
def ensure_hidden_config_dir(self):
|
||
|
|
config_dir = Path.home() / ".berry"
|
||
|
|
config_dir.mkdir(parents=True, exist_ok=True)
|
||
|
|
|
||
|
|
# If Windows, apply hidden attribute
|
||
|
|
if os.name == "nt":
|
||
|
|
subprocess.run(["attrib", "+h", str(config_dir)], shell=True)
|
||
|
|
|
||
|
|
return config_dir
|
||
|
|
|
||
|
|
def load_settings(self):
|
||
|
|
if os.path.isfile(self.config_dir / "config.ini"):
|
||
|
|
with open(self.config_dir / "config.ini", "r") as configfile:
|
||
|
|
self.config.read(configfile)
|
||
|
|
return
|
||
|
|
|
||
|
|
self.config["editor"] = {
|
||
|
|
"word_wrap": True
|
||
|
|
}
|
||
|
|
self.config["plugins"] = {
|
||
|
|
"enabled": True
|
||
|
|
}
|
||
|
|
self.config["appearance"] = {
|
||
|
|
"colour_theme": "textual-dark"
|
||
|
|
}
|
||
|
|
|
||
|
|
with open(self.config_dir / "config.ini") as configfile:
|
||
|
|
self.config.write(configfile)
|