Esempio n. 1
0
File: json.py Progetto: thrau/pymq
    def decode(self, s, _w=json.decoder.WHITESPACE.match):
        doc = super().decode(s, _w)

        cls = None
        if self.target_class:
            cls = self.target_class

        if not isinstance(doc, dict):
            if not cls:
                return doc
            else:
                return deep_from_dict(doc, cls)

        if cls is None:
            if "__type" in doc:
                cls = doc["__type"]
                cls = self._load_class(cls)

        if "__type" in doc:
            del doc["__type"]

        if "__obj" in doc:
            doc = doc["__obj"]

        if cls:
            return deep_from_dict(doc, cls)
        else:
            return doc
Esempio n. 2
0
    def test_cast_exception(self):
        err = deep_from_dict(("failed",), TimeoutError)
        assert isinstance(err, TimeoutError)
        assert ("failed",) == err.args

        err = deep_from_dict("failed", TimeoutError)
        assert isinstance(err, TimeoutError)
        assert ("failed",) == err.args
Esempio n. 3
0
    def test_from_dict_set(self):
        def do_assert(_obj: ClassWithSet):
            assert "foo" in _obj.simple_set
            assert "bar" in _obj.simple_set
            assert type(_obj.simple_set) == set
            assert len(_obj.simple_set) == 2

        do_assert(deep_from_dict({"simple_set": {"foo", "bar"}}, ClassWithSet))
        do_assert(deep_from_dict({"simple_set": ["foo", "bar"]}, ClassWithSet))
        do_assert(deep_from_dict({"simple_set": ("foo", "bar")}, ClassWithSet))
Esempio n. 4
0
 def test_from_dict_base_cases(self):
     assert 1 == deep_from_dict(1, int)
     assert 1 == deep_from_dict("1", int)
     assert "a" == deep_from_dict("a", str)
     assert (1, 2) == deep_from_dict((1, 2), tuple)
     assert [1, 2] == deep_from_dict([1, 2], list)
     assert (1, 2) == deep_from_dict([1, 2], tuple)
     assert {1, 2} == deep_from_dict({1, 2}, set)
     assert {1, 2} == deep_from_dict([1, 2], set)
     assert {"1", "2"} == deep_from_dict(["1", "2"], set)
Esempio n. 5
0
File: base.py Progetto: thrau/pymq
    def _invoke(self, request: RpcRequest) -> Any:
        spec = self._fn_spec

        if not spec.args:
            if request.args:
                raise TypeError(
                    "%s takes 0 positional arguments but %d were given" %
                    (request.fn, len(request.args)))
        else:
            if spec.args[0] == "self":
                spec.args.remove("self")

        args = list()

        logger.debug("converting args %s to spec %s", request.args, spec)

        for i in range(min(len(request.args), len(spec.args))):
            name = spec.args[i]
            value = request.args[i]

            if name in spec.annotations:
                arg_type = spec.annotations[name]
                value = deep_from_dict(value, arg_type)

            args.append(value)

        return self._fn(*args)
Esempio n. 6
0
    def create_client(self, host: str, cfg: ClientConfig, num=1) -> List[ClientDescription]:
        cmd = CreateClientCommand(host, cfg, num)
        stub = self.eventbus.stub(f'WorkerDaemon.create_client:{host}', timeout=3)

        result = stub(cmd)
        # TODO: error handling
        return [deep_from_dict(d, ClientDescription) for d in result]
Esempio n. 7
0
    def test_marshall_rpc_request(self, bus):
        request = pymq.RpcRequest("some_function", "callback_queue", ("simple_arg",))

        request_tuple = deep_to_dict(request)
        assert ("some_function", "callback_queue", ("simple_arg",), None) == request_tuple

        request_unmarshalled: pymq.RpcRequest = deep_from_dict(request_tuple, pymq.RpcRequest)

        assert "some_function" == request_unmarshalled.fn
        assert "callback_queue" == request_unmarshalled.response_channel
        assert ("simple_arg",) == request_unmarshalled.args
Esempio n. 8
0
 def test_from_dict_constructor(self):
     doc = {
         "req_a": "a",
         "req_b": 1,
         "opt_c": True,
     }
     obj = deep_from_dict(doc, DataClass)
     assert isinstance(obj, DataClass)
     assert "a" == obj.req_a
     assert 1 == obj.req_b
     assert obj.opt_c is True
     assert obj.opt_d is None
Esempio n. 9
0
File: base.py Progetto: thrau/pymq
    def _unmarshal(self, response: RpcResponse, raise_error=False):
        if response.error:
            if isinstance(response.result, Exception):
                result = RemoteInvocationError(response.result_type,
                                               *response.result.args)
            else:
                result = RemoteInvocationError(response.result_type,
                                               *response.result)

            if raise_error:
                raise result
            else:
                return result

        return deep_from_dict(response.result,
                              load_class(response.result_type))
Esempio n. 10
0
    def test_from_dict(self):
        doc = {
            "some_int": 42,
            "some_str": "jaffa kree",
            "simple_dict": {"a": 1, "b": 2},
            "complex_dict": {1: {"name": "a", "value": 1}, 2: {"name": "b", "value": 2}},
            "complex_list": [
                {
                    "nested_list": [{"name": "a", "value": 1}, {"name": "b", "value": 2}],
                    "nested_tuple": (1, {"name": "a", "value": 1}),
                },
                {
                    "nested_list": [{"name": "b", "value": 2}, {"name": "c", "value": 3}],
                    "nested_tuple": (2, {"name": "b", "value": 2}),
                },
            ],
        }

        root: RootClass = deep_from_dict(doc, RootClass)

        self.assertEqualsRoot(root)
Esempio n. 11
0
 def test_cast_type(self):
     with pytest.raises(TypeError):
         deep_from_dict("pymq.core.EventBus", type)
Esempio n. 12
0
 def test_from_dict_simple_type(self):
     doc = {"name": "a", "value": "1"}
     obj = deep_from_dict(doc, SimpleNested)
     assert isinstance(obj, SimpleNested)
     assert "a" == obj.name
     assert 1 == obj.value
Esempio n. 13
0
 def test_from_dict_generics(self):
     assert {1, 2} == deep_from_dict(["1", "2"], Set[int])
     assert {1, 2} == deep_from_dict(["1", "2", "1"], Set[int])
     assert [(1, "a"), (2, "b")] == deep_from_dict(
         [["1", "a"], ["2", "b"]], List[Tuple[int, str]]
     )
Esempio n. 14
0
    def test_from_dict_any(self):
        t_int = deep_from_dict({"arg": 1}, ClassWithAny)
        assert 1 == t_int.arg

        t_dict = deep_from_dict({"arg": {"a": 1, "b": 2}}, ClassWithAny)
        assert {"a": 1, "b": 2} == t_dict.arg
Esempio n. 15
0
def deserialize_client_description(doc: str):
    return deep_from_dict(json.loads(doc), cls=ClientDescription)