fix bugs in config.py

master
Yves G 2018-01-31 12:59:32 +01:00
parent 91b65d8e1e
commit aa47d9805b
1 changed files with 11 additions and 10 deletions

View File

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