def test_set_string(tmpdir): 'Test set for type string' tmpdir.join('test').write(""" config testing 'testing' """) u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath) u.set('test', 'testing', 'foo', 'value') u.set('test', 'testing', 'list', ('f', 'o', 'o')) assert u.get('test', 'testing', 'foo') == 'value' assert u.get('test', 'testing', 'list') == ('f', 'o', 'o')
def test_context(tmpdir): 'Test context with EUci' tmpdir.join('test').write(""" config testing 'testing' option one '0' option two '1' """) with euci.EUci(confdir=tmpdir.strpath) as u: assert not u.get('test', 'testing', 'one', dtype=bool) assert u.get('test', 'testing', 'two', dtype=bool)
def test_get_string(tmpdir): 'Test get for no dtype (string in default)' tmpdir.join('test').write(""" config str 'str' option foo 'value' list list 'value1' list list 'value2' """) u = euci.EUci(confdir=tmpdir.strpath) assert u.get('test', 'str', 'foo') == 'value' assert u.get('test', 'str', 'list') == ('value1', 'value2')
def test_set_boolean(tmpdir): 'Test set for type boolean' tmpdir.join('test').write(""" config testing 'testing' """) u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath) u.set('test', 'testing', 'true', True) u.set('test', 'testing', 'false', False) u.set('test', 'testing', 'list', (True, False, True, False)) assert u.get('test', 'testing', 'true') == '1' assert u.get('test', 'testing', 'false') == '0' assert u.get('test', 'testing', 'list') == ('1', '0', '1', '0')
def test_set_integer(tmpdir): 'Test set for type int' tmpdir.join('test').write(""" config testing 'testing' """) u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath) u.set('test', 'testing', 'plus', 42) u.set('test', 'testing', 'minus', -42) u.set('test', 'testing', 'primes', (2, 3, 5, 7)) assert u.get('test', 'testing', 'plus') == '42' assert u.get('test', 'testing', 'minus') == '-42' assert u.get('test', 'testing', 'primes') == ('2', '3', '5', '7')
def test_get_default(tmpdir): 'Test get method with default value' tmpdir.join('test').write(""" config testing 'testing' """) u = euci.EUci(confdir=tmpdir.strpath) with pytest.raises(euci.UciExceptionNotFound): u.get('test', 'str', 'foo') assert u.get('test', 'str', 'foo', default='value') == 'value' assert u.get('test', 'str', 'foo', dtype=bool, default='true') assert u.get('test', 'str', 'foo', dtype=bool, default=True) assert u.get('test', 'str', 'foo', dtype=int, default=-42) == -42 assert u.get('test', 'str', 'foo', dtype=int, default='-42') == -42
def test_get_list(tmpdir): 'Test get with list' tmpdir.join('test').write(""" config testing 'testing' option option '0' list list '0' list list '1' """) u = euci.EUci(confdir=tmpdir.strpath) assert u.get('test', 'testing', 'option', dtype=bool, list=True) == (False, ) assert not u.get('test', 'testing', 'option', dtype=bool, list=False) assert u.get('test', 'testing', 'list', dtype=bool, list=True) == (False, True) assert not u.get('test', 'testing', 'list', dtype=bool, list=False)
def test_get_integer(tmpdir): 'Test get for dtype int' tmpdir.join('test').write(""" config integer 'integer' option plus '42' option minus '-42' list primes '2' list primes '3' list primes '5' list primes '7' """) u = euci.EUci(confdir=tmpdir.strpath) assert u.get('test', 'integer', 'plus', dtype=int) == 42 assert u.get('test', 'integer', 'minus', dtype=int) == -42 assert u.get('test', 'integer', 'primes', dtype=int) == (2, 3, 5, 7)
def test_get_boolean(tmpdir): 'Test get for dtype boolean' tmpdir.join('test').write(""" config integer 'integer' option true '1' option false '0' config word 'word' option true 'yes' option false 'no' config state 'state' option true 'on' option false 'off' config bool 'bool' option true 'true' option false 'false' config bled 'bled' option true 'enabled' option false 'disabled' config list 'list' list true '1' list true 'yes' list false '0' list false 'no' """) u = euci.EUci(confdir=tmpdir.strpath) assert u.get('test', 'integer', 'true', dtype=bool) assert u.get('test', 'word', 'true', dtype=bool) assert u.get('test', 'state', 'true', dtype=bool) assert u.get('test', 'bool', 'true', dtype=bool) assert u.get('test', 'bled', 'true', dtype=bool) assert u.get('test', 'list', 'true', dtype=bool) == (True, True) assert not u.get('test', 'integer', 'false', dtype=bool) assert not u.get('test', 'word', 'false', dtype=bool) assert not u.get('test', 'state', 'false', dtype=bool) assert not u.get('test', 'bool', 'false', dtype=bool) assert not u.get('test', 'bled', 'false', dtype=bool) assert u.get('test', 'list', 'false', dtype=bool) == (False, False)
#!/usr/bin/python3 import time import json import re import subprocess import euci from foris_controller.buses.mqtt import MqttNotificationSender TIME = 3000 STEP = 200 uci = euci.EUci() controller_id = None host = uci.get("foris-controller", "mqtt", "host", default="localhost") port = uci.get("foris-controller", "mqtt", "port", dtype=int, default=11883) passwd_path = uci.get("foris-controller", "mqtt", "credentials_file", default="/etc/fosquitto/credentials.plain") with open(passwd_path, "r") as f: credentials = re.match(r"^([^:]+):(.*)$", f.readlines()[0][:-1]).groups() try: controller_id = subprocess.check_output( ["crypto-wrapper", "serial-number"]).decode().strip() except subprocess.CalledProcessError: controller_id = None sender = MqttNotificationSender(host, port, credentials)
#!/usr/bin/python3 import time import json import re import subprocess import euci TIME = 3000 STEP = 200 with euci.EUci() as uci: bus = uci.get("foris-controller", "main", "bus", default="ubus") controller_id = None if bus == "ubus": path = uci.get("foris-controller", "ubus", "notification_path", default="/var/run/ubus.sock") from foris_controller.buses.ubus import UbusNotificationSender sender = UbusNotificationSender(path) elif bus == "unix": path = uci.get("foris-controller", "unix", "notification_path", default="/var/run/foris-controller-notifications.sock") from foris_controller.buses.unix_socket import UnixSocketNotificationSender sender = UnixSocketNotificationSender(path) elif bus == "mqtt":