def test_context_commit(tmpdir):
    """Test that when we leave context that we commit. This depends on working
    test_set.
    """
    tmpdir.join('test').write("")
    with uci.Uci(confdir=tmpdir.strpath) as u:
        u.set('test', 'testing', 'testing')
        u.set('test', 'testing', 'one', '0')
        u.set('test', 'testing', 'two', '1')
    with uci.Uci(confdir=tmpdir.strpath) as u:
        assert u.get('test', 'testing', 'one') == '0'
        assert u.get('test', 'testing', 'two') == '1'
def test_set(tmpdir):
    'Test set method. This depends on working test_get.'
    tmpdir.join('test').write("")
    u = uci.Uci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
    u.set('test', 'testing', 'testing')
    u.set('test', 'testing', 'variable', 'value')
    assert u.get('test', 'testing') == 'testing'
    assert u.get('test', 'testing', 'variable') == 'value'
def test_commit(tmpdir):
    'Test commit method. This depends on working test_set.'
    cnf = tmpdir.join('test')
    cnf.write("")
    u = uci.Uci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
    u.set('test', 'testing', 'testing')
    u.set('test', 'testing', 'variable', 'value')
    u.commit('test')
    assert cnf.read() == """
def test_context(tmpdir):
    'Test context with Uci. This depends on working test_get.'
    tmpdir.join('test').write("""
config testing 'testing'
    option one '0'
    option two '1'
""")
    with uci.Uci(confdir=tmpdir.strpath) as u:
        assert u.get('test', 'testing', 'one') == '0'
        assert u.get('test', 'testing', 'two') == '1'
def test_revert(tmpdir):
    'Test revert method. This depends on working test_set.'
    tmpdir.join('test').write("")
    u = uci.Uci(confdir=tmpdir.strpath)
    u.set('test', 'testing', 'testing')
    u.set('test', 'testing', 'variable', 'value')
    assert u.get('test', 'testing', 'variable') == 'value'
    u.revert('test')
    with pytest.raises(uci.UciExceptionNotFound):
        u.get('test', 'testing', 'variable')
def test_save(tmpdir):
    'Test save method. This depends on working test_set.'
    sf = tmpdir.mkdir('save')
    cnf = tmpdir.mkdir('conf')
    cnf.join('test').write("")
    u = uci.Uci(savedir=sf.strpath, confdir=cnf.strpath)
    u.set('test', 'testing', 'testing')
    u.set('test', 'testing', 'variable', 'value')
    u.save('test')
    assert sf.join('test').read() == """test.testing='testing'
def no_test_reorder(tmpdir):
    'Test delete method. This depends on working test_commit.'
    cnf = tmpdir.join('test')
    cnf.write("""
config testing 'testing'
    option one '0'

config deploy 'deploy'
    option two '1'

""")
    u = uci.Uci(confdir=tmpdir.strpath)
    u.reorder('test', 'testing', 1)
    u.commit('test')
    assert cnf.read() == """
def test_delete(tmpdir):
    'Test delete method. This depends on working test_get.'
    tmpdir.join('test').write("""
config testing 'testing'
    option one '0'
    option two '1'
""")
    u = uci.Uci(confdir=tmpdir.strpath)
    u.delete('test', 'testing', 'one')
    assert u.get('test', 'testing', 'two') == '1'
    with pytest.raises(uci.UciExceptionNotFound):
        u.get('test', 'testing', 'one')
    u.delete('test', 'testing')
    with pytest.raises(uci.UciExceptionNotFound):
        u.get('test', 'testing', 'two')
    with pytest.raises(uci.UciExceptionNotFound):
        u.get('test', 'testing')
def test_get(tmpdir):
    'Test get method. This depends on working test_init.'
    tmpdir.join('test').write("""
config testing 'testing'
    option one '0'
    option two '1'

config testlist 'testlist'
    list list1 '42'
    list list2 'once'
    list list2 'twice'
    list list2 'thrice'
""")
    u = uci.Uci(confdir=tmpdir.strpath)
    assert u.get('test', 'testing') == 'testing'
    assert u.get('test', 'testing', 'one') == '0'
    assert u.get('test', 'testing', 'two') == '1'
    assert u.get('test', 'testlist', 'list1') == ('42', )
    assert u.get('test', 'testlist', 'list2') == ('once', 'twice', 'thrice')
def test_init():
    'Test class __init__ and __del__'
    assert uci.Uci() is not None
def test_list_configs(tmpdir):
    'Test list_configs method.'
    tmpdir.join('test').write("")
    tmpdir.join('deploy').write("")
    u = uci.Uci(confdir=tmpdir.strpath)
    assert u.list_configs() == ['deploy', 'test']
Beispiel #12
0
#!/usr/bin/python3

import argparse
import json
import re
import time
import uci
import uuid

import typing

from paho.mqtt import client as mqtt

u = uci.Uci()


def uci_get(config: str, section: str, option: str, default):
    try:
        val = u.get(config, section, option)
    except uci.UciExceptionNotFound:
        return default
    return val


def read_passwd_file(path: str) -> typing.Tuple[str]:
    """ Returns username and password from passwd file
    """
    with open(path, "r") as f:
        return re.match(r"^([^:]+):(.*)$", f.readlines()[0][:-1]).groups()