Ejemplo n.º 1
0
    async def execute_task(self, **kwargs):
        options = sb.set_options(
            indication=sb.required(sb.boolean()),
            duration_s=sb.required(sb.integer_spec()),
        ).normalise(Meta.empty(), self.photons_app.extra_as_json)

        await self.target.send(SetCleanConfig(**options), self.reference,
                               **kwargs)
Ejemplo n.º 2
0
    def make_command(self, meta, val, existing):
        v = sb.set_options(path=sb.required(sb.string_spec()),
                           allow_ws_only=sb.defaulted(sb.boolean(),
                                                      False)).normalise(
                                                          meta, val)

        path = v["path"]
        allow_ws_only = v["allow_ws_only"]

        if path not in self.paths:
            raise NoSuchPath(path, sorted(self.paths))

        val = sb.set_options(body=sb.required(
            sb.set_options(args=sb.dictionary_spec(),
                           command=sb.required(sb.string_spec())))).normalise(
                               meta, val)

        args = val["body"]["args"]
        name = val["body"]["command"]

        if existing:
            name = val["body"]["command"] = f"{existing['path']}:{name}"

        extra_context = {}
        if existing:
            extra_context["_parent_command"] = existing["command"]

        everything = meta.everything
        if isinstance(meta.everything, MergedOptions):
            everything = meta.everything.wrapped()
        everything.update(extra_context)
        meta = Meta(everything, []).at("body")

        available_commands = self.paths[path]

        if name not in available_commands:
            raise BadSpecValue(
                "Unknown command",
                wanted=name,
                available=self.available(available_commands,
                                         allow_ws_only=allow_ws_only),
                meta=meta.at("command"),
            )

        command = available_commands[name]["spec"].normalise(
            meta.at("args"), args)

        if not allow_ws_only and command.__whirlwind_ws_only__:
            raise BadSpecValue(
                "Command is for websockets only",
                wanted=name,
                available=self.available(available_commands,
                                         allow_ws_only=allow_ws_only),
                meta=meta.at("command"),
            )

        return command, name
Ejemplo n.º 3
0
    def normalise_filled(self, meta, val):
        if isinstance(val, list):
            val = tuple(val)

        if isinstance(val, tuple):
            return sb.tuple_spec(sb.optional_spec(sb.string_spec()),
                                 sb.required(sb.string_spec())).normalise(
                                     meta, val)

        task = sb.or_spec(sb.string_spec(),
                          sb.none_spec()).normalise(meta, val)
        if not task:
            task = "list_tasks"

        target = ""
        if ":" in task:
            target, task = val.rsplit(":", 1)

        if "(" not in target:
            return target or sb.NotSpecified, task

        tokens = self.initial_parse(val)

        chosen_task = tokens.pop().string
        tokens.pop()

        collector = meta.everything["collector"]
        target_register = collector.configuration["target_register"]

        target_name = tokens.pop(0).string
        target_name = self.parse_overridden_target(meta, val, collector,
                                                   target_register,
                                                   target_name, tokens)
        return target_name, chosen_task
Ejemplo n.º 4
0
    def normalise_filled(self, meta, val):
        options_spec = sb.set_options(
            filename=sb.required(sb.string_spec()), optional=sb.defaulted(sb.boolean(), False)
        )
        lst_spec = sb.listof(sb.or_spec(sb.string_spec(), options_spec))

        if isinstance(val, list):
            val = {"after": lst_spec.normalise(meta, val)}

        val = sb.set_options(after=lst_spec, before=lst_spec).normalise(meta, val)

        formatted = sb.formatted(sb.string_spec(), formatter=MergedOptionStringFormatter)

        for key in ("after", "before"):
            result = []

            for i, thing in enumerate(val[key]):
                filename = thing
                optional = False
                if isinstance(thing, dict):
                    filename = thing["filename"]
                    optional = thing["optional"]

                filename = formatted.normalise(meta.at(key).indexed_at(i), filename)
                if optional and not os.path.exists(filename):
                    log.warning(hp.lc("Ignoring optional configuration", filename=filename))
                    continue

                if not os.path.exists(filename):
                    raise BadConfiguration(
                        "Specified extra file doesn't exist",
                        source=self.source,
                        filename=filename,
                        meta=meta,
                    )

                result.append(filename)

            val[key] = result

        return self.extras_spec.normalise(meta, val)
Ejemplo n.º 5
0
    it "knows about dictionary_spec":
        self.assertSignature(sb.dictionary_spec(), "dictionary")

    it "knows about string_choice_spec":
        self.assertSignature(sb.string_choice_spec(["one", "two"]), "choice of (one | two)")

    it "knows about optional_spec":
        self.assertSignature(sb.optional_spec(sb.integer_spec()), "integer (optional)")
        self.assertSignature(sb.optional_spec(sb.any_spec()), "(optional)")

    it "knows about defaulted":
        self.assertSignature(sb.defaulted(sb.integer_spec(), 20), "integer (default 20)")
        self.assertSignature(sb.defaulted(sb.any_spec(), True), "(default True)")

    it "knows about required":
        self.assertSignature(sb.required(sb.integer_spec()), "integer (required)")
        self.assertSignature(sb.required(sb.any_spec()), "(required)")

    it "knows about listof":
        self.assertSignature(sb.listof(sb.integer_spec()), "[ integer , ... ]")
        self.assertSignature(sb.listof(sb.any_spec()), "[ <item> , ... ]")

    it "knows about dictof":
        self.assertSignature(sb.dictof(sb.string_spec(), sb.integer_spec()), "{ string : integer }")
        self.assertSignature(sb.dictof(sb.string_spec(), sb.any_spec()), "{ string : <item> }")

    it "knows about container_spec":

        class Container:
            def __init__(self, value):
                pass
Ejemplo n.º 6
0
            assert got == '{"one": 2}'

        it "doesn't loads if not a string and returns as dumps", meta:
            spec = sb.set_options(one=sb.integer_spec())
            spec = scene_spec.json_string_spec(spec, True)
            got = spec.normalise(meta, {"one": 2, "two": 3})
            assert got == '{"one": 2}'

        it "complains if string is not valid json", meta:
            spec = sb.set_options(one=sb.integer_spec())
            spec = scene_spec.json_string_spec(spec, True)
            with assertRaises(BadSpecValue, "Value was not valid json"):
                spec.normalise(meta, "{")

        it "complains if we don't match our spec", meta:
            spec = sb.set_options(one=sb.required(sb.integer_spec()))
            spec = scene_spec.json_string_spec(spec, True)
            with assertRaises(BadSpecValue):
                spec.normalise(meta, '{"two": 3}')

    describe "not storing":
        it "loads if the val is a string", meta:
            spec = sb.set_options(one=sb.integer_spec())
            spec = scene_spec.json_string_spec(spec, False)
            got = spec.normalise(meta, '{"one": 2, "two": 3}')
            assert got == {"one": 2}

        it "doesn't loads if not a string", meta:
            spec = sb.set_options(one=sb.integer_spec())
            spec = scene_spec.json_string_spec(spec, False)
            got = spec.normalise(meta, {"one": 2, "two": 3})
Ejemplo n.º 7
0
 def setup(self):
     self.spec = sb.dictof(
         service_type_spec(),
         sb.set_options(host=sb.required(sb.string_spec()),
                        port=sb.required(sb.integer_spec())),
     )