pyruse/pyruse/base.py

57 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
def setStepName(self, name):
self.stepName = name
2017-12-15 19:36:50 +01:00
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
log.error("Error while executing %s (%s): %s." % (type(self), self.stepName, str(e)))
return nextStep
2017-12-15 19:36:50 +01:00
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
log.error("Error while executing %s (%s): %s." % (type(self), self.stepName, str(e)))
return nextStep