pyruse/pyruse/config.py
2018-01-31 12:59:32 +01:00

35 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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