Onionr/onionr/config.py

152 lines
3.6 KiB
Python
Raw Normal View History

2018-02-23 01:58:36 +00:00
'''
Onionr - Private P2P Communication
2018-02-23 01:58:36 +00:00
This file deals with configuration management.
'''
'''
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/>.
'''
import os, json, logger
2019-03-29 03:33:14 +00:00
# set data dir
dataDir = os.environ.get('ONIONR_HOME', os.environ.get('DATA_DIR', 'data/'))
if not dataDir.endswith('/'):
dataDir += '/'
2018-11-11 02:10:58 +00:00
_configfile = os.path.abspath(dataDir + 'config.json')
2018-02-23 01:58:36 +00:00
_config = {}
2018-12-09 17:29:39 +00:00
def get(key, default = None, save = False):
2018-02-23 01:58:36 +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]
if not last 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
def set(key, value = None, savefile = False):
'''
Sets the key in configuration to `value`
'''
global _config
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
'''
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
def save():
'''
Saves the configuration data to the configuration file
'''
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)
2019-04-27 15:43:16 +00:00
except json.JSONDecodeError:
2018-02-23 01:58:36 +00:00
logger.warn('Failed to write to configuration file.')
def reload():
'''
Reloads the configuration data in memory from the file
'''
check()
try:
with open(get_config_file(), 'r', encoding="utf8") as configfile:
set_config(json.loads(configfile.read()))
except:
2019-02-02 03:49:11 +00:00
pass
#logger.debug('Failed to parse configuration file.')
2018-02-23 01:58:36 +00:00
def get_config():
'''
Gets the entire configuration as an array
'''
return _config
def set_config(config):
'''
Sets the configuration to the array in arguments
'''
global _config
_config = config
def get_config_file():
'''
Returns the absolute path to the configuration file
'''
return _configfile
def set_config_file(configfile):
'''
Sets the path to the configuration file
'''
global _configfile
_configfile = os.abs.abspath(configfile)