From f138e69b0c05c6222ba4ee9d84bba610c4556bd9 Mon Sep 17 00:00:00 2001 From: Leonid Shamis Date: Wed, 17 Aug 2022 16:26:40 -0700 Subject: [PATCH 1/2] cfg2 --- src/a0/cfg.py | 22 ++++++ tests/test_cfg.py | 168 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 190 insertions(+) diff --git a/src/a0/cfg.py b/src/a0/cfg.py index 4acc90d..51ea9e5 100644 --- a/src/a0/cfg.py +++ b/src/a0/cfg.py @@ -100,3 +100,25 @@ def update_configs(): if tid in obj: del obj[tid] cfg.registry = next_reg + + +class cfg2: + + def __init__(self, topic, jptr=None, type_=None): + self._cfg_reader = Cfg(topic) + self._jptr = jptr + self._type = type_ + + def get(self): + cfg_val = json.loads(self._cfg_reader.read().payload.decode()) + if self._jptr: + cfg_val = jsonpointer.resolve_pointer(cfg_val, self._jptr) + + if not self._type: + return cfg_val + elif type(cfg_val) == dict: + return cfg_val if self._type == dict else self._type(**cfg_val) + elif type(cfg_val) == list: + return cfg_val if self._type == list else self._type(*cfg_val) + else: + return self._type(cfg_val) diff --git a/tests/test_cfg.py b/tests/test_cfg.py index 892d21d..255159f 100644 --- a/tests/test_cfg.py +++ b/tests/test_cfg.py @@ -1,5 +1,6 @@ import a0 import json +import jsonpointer import pytest import threading import time @@ -203,3 +204,170 @@ def thread_main(): assert (await a0.aio_cfg_one("topic")).payload == b"done" t.join() + + +def test_cfg2_int(cfg): + bar = a0.cfg2("topic", "/bar", int) + + assert bar.get() == 3 + + cfg.write( + json.dumps({ + "bar": 4, + "foo": { + "a": "abc", + "b": "bcd", + }, + "bat": [1, 2, 3], + })) + + assert bar.get() == 4 + + cfg.write(json.dumps({ + "x": "y", + })) + + with pytest.raises(jsonpointer.JsonPointerException, + match="member 'bar' not found in {'x': 'y'}"): + bar.get() + + cfg.write(json.dumps({ + "bar": "not an int", + })) + + with pytest.raises( + ValueError, + match=r"invalid literal for int\(\) with base 10: 'not an int'"): + bar.get() + + +def test_cfg2_str(cfg): + a = a0.cfg2("topic", "/foo/a", str) + + assert a.get() == "aaa" + + cfg.write( + json.dumps({ + "bar": 4, + "foo": { + "a": "abc", + "b": "bcd", + }, + "bat": [1, 2, 3], + })) + + assert a.get() == "abc" + + cfg.write(json.dumps({ + "foo": { + "b": "bcd", + }, + })) + + with pytest.raises(jsonpointer.JsonPointerException, + match="member 'a' not found in {'b': 'bcd'}"): + a.get() + + cfg.write(json.dumps({ + "bar": 4, + })) + + with pytest.raises(jsonpointer.JsonPointerException, + match="member 'foo' not found in {'bar': 4}"): + a.get() + + cfg.write(json.dumps({ + "foo": { + "a": [5, 6, 7, 8], + }, + })) + + with pytest.raises(TypeError): + a.get() + + +def test_cfg2_dict(cfg): + foo = a0.cfg2("topic", "/foo", dict) + + assert foo.get() == {"a": "aaa", "b": "bbb"} + + cfg.write( + json.dumps({ + "bar": 4, + "foo": { + "a": "abc", + "b": "bcd", + }, + "bat": [1, 2, 3], + })) + + assert foo.get() == {"a": "abc", "b": "bcd"} + + cfg.write(json.dumps({ + "foo": [1, 2, 3], + })) + + with pytest.raises(TypeError): + foo.get() + + +def test_cfg2_list(cfg): + val = a0.cfg2("topic", "/bat", list) + assert val.get() == [1, 2, 3] + + +def test_cfg2_class(cfg): + + class Foo: + + def __init__(self, a, b): + self.a = a + self.b = b + + foo_cfg = a0.cfg2("topic", "/foo", Foo) + + foo = foo_cfg.get() + assert type(foo) == Foo + assert foo.a == "aaa" + assert foo.b == "bbb" + + cfg.write( + json.dumps({ + "bar": 4, + "foo": { + "a": "abc", + "b": "bcd", + }, + "bat": [1, 2, 3], + })) + + foo = foo_cfg.get() + assert type(foo) == Foo + assert foo.a == "abc" + assert foo.b == "bcd" + + +def test_cfg2_default_type(cfg): + assert type(a0.cfg2("topic", "/bar").get()) == int + assert type(a0.cfg2("topic", "/foo").get()) == dict + assert type(a0.cfg2("topic", "/bat").get()) == list + + assert a0.cfg2("topic", "/bar").get() == 3 + assert a0.cfg2("topic", "/foo").get() == { + "a": "aaa", + "b": "bbb", + } + assert a0.cfg2("topic", "/bat").get() == [1, 2, 3] + + +def test_cfg2_nojptr(cfg): + assert type(a0.cfg2("topic").get()) == dict + + assert a0.cfg2("topic").get() == { + "bar": 3, + "bat": [1, 2, 3], + "foo": { + "a": "aaa", + "b": "bbb" + }, + } From d9227920089089cced080fedb050607ac313b451 Mon Sep 17 00:00:00 2001 From: Leonid Shamis Date: Wed, 17 Aug 2022 16:27:10 -0700 Subject: [PATCH 2/2] version bump --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index a5105d8..a1dfd93 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ ) setuptools.setup(name="alephzero", - version="0.3.17", + version="0.3.18", description="TODO: description", author="Leonid Shamis", author_email="leonid.shamis@gmail.com",