Example #1
0
    def from_config_string(self, obj, value):
        """Perform expansion, mapping and conversion after another."""
        # In case the attribute is set from outside but the conversion should
        # be applied, use the following pattern:
        #
        # attribute = getattr(self.__class__, key, None)
        # if isinstance(attribute, Attribute):
        #     value = attribute.from_config_string(self, value)
        # setattr(self, key, value)
        #

        if isinstance(value, str) and self.expand:
            value = obj.expand(value)
        if isinstance(value, str) and self.map:
            value = obj.map(value)
        if isinstance(value, str) and self.conversion:
            try:
                value = self.conversion(value)
            except Exception as e:
                # Try to detect our own name.
                name = "<unknown>"
                for k in dir(obj):
                    if getattr(obj.__class__, k, None) is self:
                        name = k
                        break
                raise batou.ConversionError(obj, name, value, self.conversion,
                                            e)
        return value
Example #2
0
 def __set__(self, obj, value):
     if isinstance(value, str) and self.expand:
         value = obj.expand(value)
     if isinstance(value, str) and self.map:
         value = obj.map(value)
     if isinstance(value, str) and self.conversion:
         try:
             value = self.conversion(value)
         except Exception as e:
             # Try to detect our own name.
             name = '<unknown>'
             for k in dir(obj):
                 if getattr(obj.__class__, k, None) is self:
                     name = k
                     break
             raise batou.ConversionError(obj, name, value, self.conversion,
                                         e)
     self.instances[obj] = value
Example #3
0
    def configure(self):
        super().configure()

        if isinstance(self.mode, str):
            try:
                self.mode = int(self.mode, 8)
            except ValueError:
                try:
                    self.mode = convert_mode(self.mode)
                except Exception as e:
                    raise batou.ConversionError(self, 'mode', self.mode,
                                                convert_mode, e)

        elif isinstance(self.mode, int):
            pass
        else:
            raise batou.ConfigurationError(
                f'`mode` is required and `{self.mode!r}` is not a valid value.`'
            )
Example #4
0
def test_config_exceptions_orderable(env):
    env.configure()
    c = env.get_root("zeo", env.hosts['localhost']).component

    import sys

    try:
        raise ValueError("Test")
    except Exception:
        exc_type, ex, tb = sys.exc_info()

    exceptions = [
        batou.ConfigurationError("test", None),
        batou.ConfigurationError("test", c),
        batou.ConversionError(c, "key", 123, int, "invalid int"),
        batou.MissingOverrideAttributes(c, ["sadf"]),
        batou.DuplicateComponent(c.root, c.root),
        batou.UnknownComponentConfigurationError(c.root, ex, tb),
        batou.UnusedResources({"asdf": [(c.root, 1)]}),
        batou.UnsatisfiedResources({"asdf": [c.root]}),
        batou.MissingEnvironment(env),
        batou.MissingComponent("asdf", "localhost"),
        batou.SuperfluousSection("asdf"),
        batou.SuperfluousComponentSection("asdf"),
        batou.SuperfluousSecretsSection("asdf"),
        batou.CycleErrorDetected(ValueError()),
        batou.NonConvergingWorkingSet([c]),
        batou.DeploymentError(),
        batou.DuplicateHostMapping("host", "map1", "map2"),
        batou.RepositoryDifferentError("asdf", "bsdf"),
        batou.DuplicateHostError("localhost"),
        batou.InvalidIPAddressError("asdf"), ]

    # Ensure all exceptions can be compared
    for x in exceptions:
        for y in exceptions:
            x.sort_key < y.sort_key