Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 22 additions & 0 deletions src/a0/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
168 changes: 168 additions & 0 deletions tests/test_cfg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import a0
import json
import jsonpointer
import pytest
import threading
import time
Expand Down Expand Up @@ -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"
},
}