2021-08-14 19:40:42 +00:00
|
|
|
"""
|
2022-03-05 16:49:35 +00:00
|
|
|
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions.
|
2022-01-12 13:39:23 +00:00
|
|
|
* Copyright (C) 2022 Ad5001
|
2021-08-14 19:40:42 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from os import path, environ, makedirs
|
|
|
|
from platform import system
|
|
|
|
from json import load, dumps
|
2022-01-20 17:19:36 +00:00
|
|
|
from PySide2.QtCore import QLocale, QTranslator
|
|
|
|
|
2021-08-14 19:40:42 +00:00
|
|
|
|
|
|
|
DEFAULT_SETTINGS = {
|
|
|
|
"check_for_updates": True,
|
2021-08-21 17:46:20 +00:00
|
|
|
"reset_redo_stack": True,
|
2021-08-14 19:40:42 +00:00
|
|
|
"last_install_greet": "0",
|
2022-04-22 13:28:02 +00:00
|
|
|
"enable_latex": False
|
2021-08-14 19:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Create config directory
|
|
|
|
CONFIG_PATH = {
|
|
|
|
"Linux": path.join(environ["XDG_CONFIG_HOME"], "LogarithmPlotter") if "XDG_CONFIG_HOME" in environ else path.join(path.expanduser("~"), ".config", "LogarithmPlotter"),
|
|
|
|
"Windows": path.join(path.expandvars('%APPDATA%'), "LogarithmPlotter", "config"),
|
|
|
|
"Darwin": path.join(path.expanduser("~"), "Library", "Application Support", "LogarithmPlotter"),
|
|
|
|
}[system()]
|
|
|
|
|
|
|
|
CONFIG_FILE = path.join(CONFIG_PATH, "config.json")
|
2022-01-20 17:19:36 +00:00
|
|
|
|
|
|
|
initialized = False
|
|
|
|
current_config= DEFAULT_SETTINGS
|
|
|
|
|
2021-08-14 19:40:42 +00:00
|
|
|
|
|
|
|
def init():
|
|
|
|
"""
|
|
|
|
Initializes the config and loads all possible settings from the file if needs be.
|
|
|
|
"""
|
|
|
|
makedirs(CONFIG_PATH, exist_ok=True)
|
|
|
|
|
|
|
|
if path.exists(CONFIG_FILE):
|
|
|
|
cfg_data = load(open(CONFIG_FILE, 'r', -1, 'utf8'))
|
|
|
|
for setting_name in cfg_data:
|
|
|
|
setSetting(setting_name, cfg_data[setting_name])
|
2022-01-20 17:19:36 +00:00
|
|
|
|
2021-08-14 19:40:42 +00:00
|
|
|
def save():
|
|
|
|
"""
|
|
|
|
Saves the config to the path.
|
|
|
|
"""
|
|
|
|
write_file = open(CONFIG_FILE, 'w', -1, 'utf8')
|
2022-01-20 17:19:36 +00:00
|
|
|
write_file.write(dumps(current_config))
|
2021-08-14 19:40:42 +00:00
|
|
|
write_file.close()
|
|
|
|
|
|
|
|
def getSetting(namespace):
|
|
|
|
"""
|
|
|
|
Returns a setting from a namespace.
|
|
|
|
E.g: if the config is {"test": {"foo": 1}}, you can access the "foo" setting
|
|
|
|
by using the "test.foo" namespace.
|
|
|
|
"""
|
|
|
|
names = namespace.split(".")
|
2022-01-20 17:19:36 +00:00
|
|
|
setting = current_config
|
2021-08-14 19:40:42 +00:00
|
|
|
for name in names:
|
|
|
|
if name in setting:
|
|
|
|
setting = setting[name]
|
|
|
|
else:
|
|
|
|
# return namespace # Return original name
|
|
|
|
raise ValueError('Setting ' + namespace + ' doesn\'t exist. Debug: ', setting, name)
|
|
|
|
return setting
|
|
|
|
|
|
|
|
def setSetting(namespace, data):
|
|
|
|
"""
|
|
|
|
Sets a setting at a namespace with data.
|
|
|
|
E.g: if the config is {"test": {"foo": 1}}, you can access the "foo" setting
|
|
|
|
by using the "test.foo" namespace.
|
|
|
|
"""
|
|
|
|
names = namespace.split(".")
|
2022-01-20 17:19:36 +00:00
|
|
|
setting = current_config
|
2021-08-14 19:40:42 +00:00
|
|
|
for name in names:
|
|
|
|
if name != names[-1]:
|
|
|
|
if name in setting:
|
|
|
|
setting = setting[name]
|
|
|
|
else:
|
|
|
|
raise ValueError('Setting {} doesn\'t exist. Debug: {}, {}'.format(namespace, setting, name))
|
|
|
|
else:
|
|
|
|
setting[name] = data
|