Onionr/src/config/__init__.py

144 lines
3.4 KiB
Python
Raw Normal View History

"""Onionr - Private P2P Communication.
2018-02-23 01:58:36 +00:00
This file deals with configuration management.
"""
import os
from json import JSONDecodeError
import ujson as json
import logger
import filepaths
from . import onboarding
"""
2018-02-23 01:58:36 +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/>.
"""
2019-09-11 03:56:20 +00:00
_configfile = filepaths.config_file
2018-02-23 01:58:36 +00:00
_config = {}
2018-12-09 17:29:39 +00:00
def get(key, default = None, save = False):
"""Gets the key from configuration, or returns `default`"""
2018-03-03 22:18:57 +00:00
2018-06-14 04:17:58 +00:00
key = str(key).split('.')
data = _config
last = key.pop()
for item in key:
if (not item in data) or (not type(data[item]) == dict):
return default
data = data[item]
if last not in data:
2018-12-09 17:29:39 +00:00
if save:
set(key, default, savefile = True)
2018-06-14 04:17:58 +00:00
return default
return data[last]
2018-02-23 01:58:36 +00:00
2018-02-23 01:58:36 +00:00
def set(key, value = None, savefile = False):
"""Sets the key in configuration to `value`"""
2018-02-23 01:58:36 +00:00
global _config
2018-06-14 04:17:58 +00:00
whole_key = key
2018-06-14 04:17:58 +00:00
key = str(key).split('.')
data = _config
last = key.pop()
for item in key:
if (not item in data) or (not type(data[item]) == dict):
data[item] = dict()
data = data[item]
2018-03-03 22:18:57 +00:00
if value is None:
2018-06-14 04:17:58 +00:00
del data[last]
2018-03-03 22:18:57 +00:00
else:
2018-06-14 04:17:58 +00:00
data[last] = value
2018-02-23 01:58:36 +00:00
if savefile:
save()
2018-02-23 02:25:05 +00:00
def is_set(key):
2018-06-14 04:17:58 +00:00
key = str(key).split('.')
data = _config
last = key.pop()
for item in key:
if (not item in data) or (not type(data[item]) == dict):
return False
data = data[item]
if not last in data:
return False
return True
2018-02-23 02:25:05 +00:00
2018-02-23 01:58:36 +00:00
def check():
"""Checks if the configuration file exists, creates it if not"""
2018-02-23 01:58:36 +00:00
2019-05-12 14:41:36 +00:00
if not os.path.exists(os.path.dirname(get_config_file())):
2019-05-12 14:44:02 +00:00
os.makedirs(os.path.dirname(get_config_file()))
2018-02-23 01:58:36 +00:00
2018-02-23 01:58:36 +00:00
def save():
"""Saves the configuration data to the configuration file"""
2018-02-23 01:58:36 +00:00
check()
try:
with open(get_config_file(), 'w', encoding="utf8") as configfile:
2018-06-14 04:42:19 +00:00
json.dump(get_config(), configfile, indent=2)
except JSONDecodeError:
2018-02-23 01:58:36 +00:00
logger.warn('Failed to write to configuration file.')
2018-02-23 01:58:36 +00:00
def reload():
"""Reloads the configuration data in memory from the file"""
2018-02-23 01:58:36 +00:00
check()
try:
with open(get_config_file(), 'r', encoding="utf8") as configfile:
set_config(json.loads(configfile.read()))
except (FileNotFoundError, JSONDecodeError) as e:
2019-02-02 03:49:11 +00:00
pass
#logger.debug('Failed to parse configuration file.')
2018-02-23 01:58:36 +00:00
2018-02-23 01:58:36 +00:00
def get_config():
"""Gets the entire configuration as an array"""
2018-02-23 01:58:36 +00:00
return _config
2018-02-23 01:58:36 +00:00
def set_config(config):
"""Sets the configuration to the array in arguments"""
2018-02-23 01:58:36 +00:00
global _config
_config = config
2018-02-23 01:58:36 +00:00
def get_config_file():
"""Returns the absolute path to the configuration file"""
2018-02-23 01:58:36 +00:00
return _configfile
2018-02-23 01:58:36 +00:00
def set_config_file(configfile):
"""Sets the path to the configuration file."""
2018-02-23 01:58:36 +00:00
global _configfile
_configfile = os.abs.abspath(configfile)