Exemple #1
0
    def resolve_reference(self, collector, task_func, reference, target):
        """
        If the task func needs a reference and none is specified then complain

        if we have special_reference turned on then:
        * Empty or _ is seen as all serials on the network
        * ``typ:options`` is given to the reference_resolver_register
        * otherwise we return a HardCodedSerials with the provided reference

        Otherwise we just return whatever reference is
        """
        empty = lambda r: r in ("", None, sb.NotSpecified)

        if task_func.needs_reference and empty(reference):
            raise BadOption("This task requires you specify a reference, please do so!", action=self.action)

        if task_func.special_reference:
            if empty(reference) or reference == "_":
                reference = FoundSerials()

            if type(reference) is str:
                if ":" in reference:
                    typ, options = reference.split(":", 1)
                    reference = collector.configuration["reference_resolver_register"].resolve(typ, options, target)

            if not isinstance(reference, SpecialReference):
                return HardCodedSerials(reference)

        return reference
Exemple #2
0
        async def assertFindSerials(self, found, serials, expected, missing):
            broadcast = mock.Mock(name="broadcast")
            find_timeout = mock.Mock(name="find_timeout")

            sender = mock.Mock(name="sender")
            sender.found = {}
            sender.find_specific_serials = pytest.helpers.AsyncMock(name="find_specific_serials")
            sender.find_specific_serials.return_value = (found, missing)

            ref = HardCodedSerials(serials)
            f = await ref.find_serials(sender, broadcast=broadcast, timeout=find_timeout)

            assert f == expected

            sender.find_specific_serials.assert_called_once_with(
                serials, broadcast=broadcast, raise_on_none=False, timeout=find_timeout
            )

            assert ref.missing(f) == missing
Exemple #3
0
    def reference_object(self, reference=sb.NotSpecified):
        """
        * NotSpecified, "", None or _ are seen as all serials on the network
        * ``typ:options`` is given resolved using our resolvers
        * otherwise we return a HardCodedSerials with the provided reference
        """
        if isinstance(reference, SpecialReference):
            return reference

        if reference in ("", "_", None, sb.NotSpecified):
            return FoundSerials()

        if type(reference) is str:
            if ":" in reference:
                typ, options = reference.split(":", 1)
                reference = self.resolve(typ, options)

        if isinstance(reference, SpecialReference):
            return reference

        return HardCodedSerials(reference)
Exemple #4
0
async def find_serials(reference, sender, timeout):
    """
    Return (serials, missing) for all the serials that can be found in the
    provided reference

    If the reference is an empty string, a single underscore or None then we
    find all devices on the network

    if it is a string or a list of strings, we treat those strings as the serials
    to find.

    Otherwise we assume it's a special reference
    """
    if not isinstance(reference, SpecialReference):
        if reference in ("", "_", None, sb.NotSpecified):
            reference = FoundSerials()
        else:
            if isinstance(reference, str):
                reference = reference.split(",")
            reference = HardCodedSerials(reference)

    found, serials = await reference.find(sender, timeout=timeout)
    missing = reference.missing(found)
    return serials, missing
Exemple #5
0
            ref = Reference()
            assert register.reference_object(ref) is ref

        it "returns a FoundSerials instruction if no reference is specified", register:
            for r in ("", None, sb.NotSpecified):
                references = register.reference_object(r)
                assert isinstance(references, FoundSerials), references

            assert isinstance(register.reference_object("_"), FoundSerials)

        it "returns a FoundSerials for an underscore", register:
            references = register.reference_object("_")
            assert isinstance(references, FoundSerials), references

        it "returns the resolved reference if of type typ:options", register:
            ret = HardCodedSerials(["d073d5000001", "d073d5000002"])
            resolver = mock.Mock(name="resolver", return_value=ret)
            register.add("my_resolver", resolver)

            reference = "my_resolver:blah:and,stuff"
            resolved = register.reference_object(reference)
            assert resolved is ret

        it "returns a SpecialReference if our resolver returns not a special reference", register:
            ret = "d073d5000001,d073d5000002"
            wanted = [binascii.unhexlify(ref) for ref in ret.split(",")]

            for reference in (ret, ret.split(",")):
                resolver = mock.Mock(name="resolver", return_value=reference)

                register.add("my_resolver", resolver)
Exemple #6
0
            @hp.memoized_property
            def target1(s):
                return binascii.unhexlify(s.serial1)[:6]

            @hp.memoized_property
            def target2(s):
                return binascii.unhexlify(s.serial2)[:6]

        return V()

    async it "takes in a list of serials":
        serials = "d073d5000001,d073d500000200"
        wanted = [binascii.unhexlify(ref)[:6] for ref in serials.split(",")]

        for s in (serials, serials.split(",")):
            ref = HardCodedSerials(s)
            assert ref.targets == wanted
            assert ref.serials == ["d073d5000001", "d073d5000002"]

    async it "can take in list of unhexlified serials":
        serials = [binascii.unhexlify("d073d500000100"), binascii.unhexlify("d073d5000002")]
        wanted = [binascii.unhexlify(ref)[:6] for ref in ["d073d5000001", "d073d5000002"]]

        ref = HardCodedSerials(serials)
        assert ref.targets == wanted
        assert ref.serials == ["d073d5000001", "d073d5000002"]

    describe "find_serials":

        async def assertFindSerials(self, found, serials, expected, missing):
            broadcast = mock.Mock(name="broadcast")
Exemple #7
0
describe "can send over udp":

    @pytest.fixture()
    async def device(self, final_future, make_device):
        device = make_device(has_udp=True, value_store={"port": 56700})
        async with device.session(final_future):
            yield device

    @pytest.fixture()
    async def sender(self, final_future, device):
        configuration = {"final_future": final_future, "protocol_register": protocol_register}
        async with LanTarget.create(configuration).session() as sender:
            yield sender

    async it "can send and receive messages using lan target target", sender, device:
        reference = HardCodedSerials([device.serial])
        found, serials = await reference.find(sender, timeout=1)
        assert serials == [device.serial]
        assert (
            found[binascii.unhexlify(device.serial)][Services.UDP].port
            == device.io[Services.UDP.name].options.port
        )

        pkts = await sender(DeviceMessages.SetPower(level=65535), device.serial)
        assert len(pkts) == 1
        pkt = pkts[0]
        assert pkt | DeviceMessages.StatePower
        assert pkt.level == 0

        pkts = await sender(DeviceMessages.GetPower(), device.serial)
        assert len(pkts) == 1
Exemple #8
0
    describe "with a value":

        @pytest.fixture()
        def meta(self, collector):
            def resolve(s):
                return DeviceFinder.from_url_str(s)

            collector.configuration["reference_resolver_register"].add("match", resolve)

            return Meta({"collector": collector}, []).at("test")

        @pytest.mark.parametrize(
            "special,mandatory", [(False, False), (False, True), (True, False), (True, True)]
        )
        it "returns as is if not a string", meta, special, mandatory:
            val = HardCodedSerials([])
            assert reference_spec(mandatory=mandatory, special=special).normalise(meta, val) is val

        @pytest.mark.parametrize("mandatory", [False, True])
        it "returns as is if a string and special is False", meta, mandatory:
            val = "stuffandthings"
            assert reference_spec(mandatory=mandatory, special=False).normalise(meta, val) is val

        @pytest.mark.parametrize("mandatory", [False, True])
        it "creates a reference object if special is true and val is a string", meta, mandatory:
            spec = reference_spec(mandatory=mandatory, special=True)

            result = spec.normalise(meta, "match:cap=hev")
            assert isinstance(result, DeviceFinder)
            assert result.fltr.cap == ["hev"]