flyingcircusio / batou

batou is a universal, fractal deployment utility using Python.
https://batou.readthedocs.org
Other
47 stars 12 forks source link

Inputvalidation of feature() of a component #378

Open frlan opened 1 year ago

frlan commented 1 year ago

In some deployments we are making heavy usage of "feature" which ends up in sometime fairly complex Python code that might or might not is good understandable:

        if "single" in self.features and len(self.features) != 1:
            raise ValueError("Feature 'single' excludes all other features")
        if "http" in self.features or "single" in self.features:
            # do something 
        if "cron" in self.features:
            # do something 
        elif "single" in self.features:
            pass
        else:
            # do something 

As we kind of know which values feature can include at definition we add something like that to make it easier to understand:

    # :single :cron :http :docroot
    features = ()

It would be nice if Attribute() (or a new construct) would support validation of possible feature flags.

elikoga commented 1 year ago

Ist

class AttributeWithValidation(Attribute):
    """An attribute descriptor that validates the value before setting it.

    The validation function is called with the component and the value.
    It should raise an exception if the value is not valid.

    :param callable validation: The validation function.
    """

    def __init__(self, validation=None, **kwargs):
        super().__init__(**kwargs)
        if validation is None:
            validation = lambda obj, value: None
        self.validation = validation

    def __set__(self, obj, value):
        self.validation(obj, value)
        super().__set__(obj, value)

eine Lösung? Falls ja kann man drüber nachdenken das in Attribute hinzuzufügen

Zum Ausprobieren einfach als Ersatz für Attribute verwenden und den Parameter validation angeben

zagy commented 3 months ago

@frlan ^

elikoga commented 2 months ago

https://github.com/flyingcircusio/batou/blob/00f2bdda3f5e0c05d2cf5aa9eaca3be58fec7cb6/src/batou/deploy.py#L304-L325

This block of code is a bit unwieldy/hard to understand. The high level api asyncio.run is included in python 3.7 and could be used to simplify this.

Also I'd look into why the while pending loop at the bottom is necessary