2020-01-07 11:44:53 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2018-02-23 01:58:36 +00:00
|
|
|
|
2020-01-07 11:44:53 +00:00
|
|
|
This file deals with configuration management.
|
|
|
|
"""
|
2020-04-03 09:02:36 +00:00
|
|
|
import os
|
2020-04-03 09:33:30 +00:00
|
|
|
from json import JSONDecodeError
|
2020-04-03 09:02:36 +00:00
|
|
|
|
|
|
|
import ujson as json
|
|
|
|
import logger
|
2020-01-07 11:44:53 +00:00
|
|
|
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/>.
|
2020-01-07 11:44:53 +00:00
|
|
|
"""
|
2019-11-30 08:42:49 +00:00
|
|
|
|
2019-09-11 03:56:20 +00:00
|
|
|
_configfile = filepaths.config_file
|
2018-02-23 01:58:36 +00:00
|
|
|
_config = {}
|
|
|
|
|
2020-01-07 11:44:53 +00:00
|
|
|
|
2018-12-09 17:29:39 +00:00
|
|
|
def get(key, default = None, save = False):
|
2020-01-07 11:44:53 +00:00
|
|
|
"""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]
|
|
|
|
|
2020-01-07 11:44:53 +00:00
|
|
|
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
|
|
|
|
2020-01-07 11:44:53 +00:00
|
|
|
|
2018-02-23 01:58:36 +00:00
|
|
|
def set(key, value = None, savefile = False):
|
2020-01-07 11:44:53 +00:00
|
|
|
"""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
|
|
|
|
2020-01-07 11:44:53 +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()
|
|
|
|
|
2020-01-07 11:44:53 +00:00
|
|
|
|
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
|
|
|
|
2020-01-07 11:44:53 +00:00
|
|
|
|
2018-02-23 01:58:36 +00:00
|
|
|
def check():
|
2020-01-07 11:44:53 +00:00
|
|
|
"""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
|
|
|
|
2020-01-07 11:44:53 +00:00
|
|
|
|
2018-02-23 01:58:36 +00:00
|
|
|
def save():
|
2020-01-07 11:44:53 +00:00
|
|
|
"""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)
|
2020-04-03 09:33:30 +00:00
|
|
|
except JSONDecodeError:
|
2018-02-23 01:58:36 +00:00
|
|
|
logger.warn('Failed to write to configuration file.')
|
|
|
|
|
2020-01-07 11:44:53 +00:00
|
|
|
|
2018-02-23 01:58:36 +00:00
|
|
|
def reload():
|
2020-01-07 11:44:53 +00:00
|
|
|
"""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()))
|
2020-04-03 09:33:30 +00:00
|
|
|
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
|
|
|
|
2020-01-07 11:44:53 +00:00
|
|
|
|
2018-02-23 01:58:36 +00:00
|
|
|
def get_config():
|
2020-01-07 11:44:53 +00:00
|
|
|
"""Gets the entire configuration as an array"""
|
2018-02-23 01:58:36 +00:00
|
|
|
return _config
|
|
|
|
|
2020-01-07 11:44:53 +00:00
|
|
|
|
2018-02-23 01:58:36 +00:00
|
|
|
def set_config(config):
|
2020-01-07 11:44:53 +00:00
|
|
|
"""Sets the configuration to the array in arguments"""
|
2018-02-23 01:58:36 +00:00
|
|
|
global _config
|
|
|
|
_config = config
|
|
|
|
|
2020-01-07 11:44:53 +00:00
|
|
|
|
2018-02-23 01:58:36 +00:00
|
|
|
def get_config_file():
|
2020-01-07 11:44:53 +00:00
|
|
|
"""Returns the absolute path to the configuration file"""
|
2018-02-23 01:58:36 +00:00
|
|
|
return _configfile
|
|
|
|
|
2020-01-07 11:44:53 +00:00
|
|
|
|
2018-02-23 01:58:36 +00:00
|
|
|
def set_config_file(configfile):
|
2020-01-07 11:44:53 +00:00
|
|
|
"""Sets the path to the configuration file."""
|
2018-02-23 01:58:36 +00:00
|
|
|
global _configfile
|
|
|
|
_configfile = os.abs.abspath(configfile)
|