pyruse/pyruse/base.py

56 lines
1.4 KiB
Python
Raw Normal View History

# pyruse is intended as a replacement to both fail2ban and epylog
2018-01-31 12:04:21 +01:00
# Copyright © 20172018 Y. Gablin
# 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 abc
from pyruse import log
class Step(abc.ABC):
def __init__(self):
self.nextStep = None
@abc.abstractmethod
def run(self, entry):
pass
def setNextStep(self, obj):
self.nextStep = obj
class Filter(Step):
def __init__(self):
super().__init__()
self.altStep = None
def setAltStep(self, obj):
self.altStep = obj
@abc.abstractmethod
def filter(self, entry):
pass
def run(self, entry):
try:
nextStep = self.nextStep if self.filter(entry) else self.altStep
except Exception as e:
nextStep = self.altStep
2018-01-31 12:04:21 +01:00
log.error("Error while executing %s: %s." % (type(self), str(e)))
2017-12-15 19:36:50 +01:00
if nextStep:
nextStep.run(entry)
class Action(Step):
def __init__(self):
super().__init__()
@abc.abstractmethod
def act(self, entry):
pass
def run(self, entry):
try:
self.act(entry)
2018-01-31 12:04:21 +01:00
nextStep = self.nextStep
2017-12-15 19:36:50 +01:00
except Exception as e:
2018-01-31 12:04:21 +01:00
nextStep = None
2017-12-15 19:36:50 +01:00
log.error("Error while executing %s: %s." % (type(self), str(e)))
2018-01-31 12:04:21 +01:00
if nextStep:
nextStep.run(entry)