2017-12-16 18:59:33 +01:00
|
|
|
|
# pyruse is intended as a replacement to both fail2ban and epylog
|
2018-01-31 12:59:32 +01:00
|
|
|
|
# Copyright © 2017–2018 Y. Gablin
|
2017-12-16 18:59:33 +01:00
|
|
|
|
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
|
2017-12-15 19:36:50 +01:00
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
from pyruse import log
|
|
|
|
|
|
|
|
|
|
class Config:
|
2018-01-31 12:59:32 +01:00
|
|
|
|
CONF_NAME = "pyruse.json"
|
2017-12-15 19:36:50 +01:00
|
|
|
|
_paths = None
|
|
|
|
|
|
2018-01-31 12:59:32 +01:00
|
|
|
|
# __main__ must be the first to create a Config object, then paths are remembered
|
2017-12-15 19:36:50 +01:00
|
|
|
|
def __init__(self, paths = None):
|
|
|
|
|
if paths is None:
|
|
|
|
|
paths = Config._paths
|
|
|
|
|
Config._paths = paths
|
|
|
|
|
for p in paths:
|
2018-01-31 12:59:32 +01:00
|
|
|
|
confpath = os.path.join(p, Config.CONF_NAME)
|
2017-12-15 19:36:50 +01:00
|
|
|
|
try:
|
2018-01-31 12:59:32 +01:00
|
|
|
|
with open(confpath) as conffile:
|
2017-12-15 19:36:50 +01:00
|
|
|
|
conf = json.load(conffile, object_pairs_hook = OrderedDict)
|
2018-01-31 12:59:32 +01:00
|
|
|
|
self.conf = conf
|
|
|
|
|
break
|
2017-12-15 19:36:50 +01:00
|
|
|
|
except IOError:
|
2018-01-31 12:59:32 +01:00
|
|
|
|
log.debug("IOError while opening %s\n" % confpath)
|
2017-12-15 19:36:50 +01:00
|
|
|
|
except json.JSONDecodeError:
|
2018-01-31 12:59:32 +01:00
|
|
|
|
log.debug("JSONDecodeError while opening %s\n" % confpath)
|
|
|
|
|
else:
|
2017-12-15 19:36:50 +01:00
|
|
|
|
raise FileNotFoundError("File `%s` not found in either of %s." \
|
2018-01-31 12:59:32 +01:00
|
|
|
|
% (Config.CONF_NAME, str(paths)))
|
2017-12-15 19:36:50 +01:00
|
|
|
|
|
|
|
|
|
def asMap(self):
|
|
|
|
|
return self.conf
|