pyruse/pyruse/module.py

42 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 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 importlib
from pyruse import log
_modules = {}
class Module:
def __init__(self, isAction, module, thenRun, elseRun):
self.isAction = isAction
self.isFilter = not isAction
self.module = module
self.thenRun = thenRun
self.elseRun = elseRun
def get(moduleDesc):
if "filter" in moduleDesc:
isAction = False
mod = _getModule("pyruse.filters." + moduleDesc["filter"])
obj = mod.Filter(moduleDesc.get("args", {}))
elseRun = moduleDesc["else"] if "else" in moduleDesc else None
elif "action" in moduleDesc:
isAction = True
mod = _getModule("pyruse.actions." + moduleDesc["action"])
obj = mod.Action(moduleDesc.get("args", {}))
elseRun = None
else:
raise ValueError("Step is neither “filter” nor “action”: %s\n" % str(moduleDesc))
thenRun = moduleDesc["then"] if "then" in moduleDesc else None
return Module(isAction, obj, thenRun, elseRun)
def _getModule(modName):
if modName not in _modules:
try:
module = importlib.import_module(modName)
except ImportError as e:
log.error("Module %s not found.\n" % modName)
raise e
_modules[modName] = module
return _modules[modName]