コード例 #1
0
def test_magic_method():
    c = CatConfig(data={'test': 'val'})

    assert str(c) == "{'test': 'val'}"

    c = CatConfig()
    assert str(c) == 'None'
コード例 #2
0
def test_validate():
    data = {'foo': {'key': 'value'}, 'id': 114514}
    schema = {
        'foo': {
            'type': 'dict',
            'schema': {
                'key': {
                    'type': 'integer'
                }
            }
        },
        'id': {
            'type': 'string'
        }
    }

    c = CatConfig(validator_schema=schema)

    with pytest.raises(ValidationError):
        c.load(data)
コード例 #3
0
ファイル: test_rpc_aria2c.py プロジェクト: dev-techmoe/megumi
async def test_aria2c_rpc(mock_server):
    port = mock_server['port']

    client = Aria2cConnector(
        CatConfig(data={
            'address': f'http://localhost:{port}',
            'secret': '1234'
        }))
    resp = await client.download_by_url('https://example.com')
    assert resp != None
    resp = await client.download_by_torrent(
        open('./tests/assests/ubuntu_iso_just_for_test.torrent', 'rb').read())
    assert resp != None
コード例 #4
0
def test_update():
    c = CatConfig(data={'foo': 'bar', 'obj': {'key': 'val'}, 'boolitem': True})

    c.update({'test': 'val'})
    c.set('key', 'val')
    c.obj.set('key', 'new_val')

    assert c.test == 'val'
    assert c.key == 'val'
    assert c.obj.key == 'new_val'

    assert c.boolitem == True
    assert bool(c.obj) == True
    assert bool(c.some.item.does.nt.exist) == False

    assert c.get('test') == 'val'
    assert c.get('some_key_does_not_exists') == None
コード例 #5
0
def test_get():
    c = CatConfig(
        data={
            'foo': {
                'bar': 'test'
            },
            'cat': [{
                'name': 'tom',
                'age': 114514
            }, {
                'name': 'Jerry',
                'age': 1919810
            }]
        })

    assert c.foo.bar == 'test'
    assert c.foo == {'bar': 'test'}
    assert c.a.b == None
    assert c.cat[0].name == 'tom'

    assert c['foo']['bar'] == 'test'
    assert c['foo'] == {'bar': 'test'}
    assert c['a']['b'] == None
    assert c['cat'][0]['name'] == 'tom'
コード例 #6
0
ファイル: config.py プロジェクト: dev-techmoe/megumi
import toml
from typing import Optional
import uuid
from catconfig import CatConfig

default_config = {
    'api': {
        'host': '127.0.0.1',
        'port': 8091,
        'auth_token': None
    },
    'runtime': {
        'data_path': 'runtime'
    },
    'downloader': {
        'type': 'aria2',
        'aria2': {
            'address': 'http://localhost:6800',
            'secret': None
        }
    },
    'logging': {
        'no_ansi': False,
        'level': 'INFO'
    }
}

config = CatConfig(data=default_config)
コード例 #7
0
from catconfig import CatConfig, ValidationError

# Load
# Load config from string
c = CatConfig()
c.load_from_string("""
{
    "foo": "bar"
}
""")
# Load config when initalizing CatConfig object
c = CatConfig(data={
    'foo': 'bar'
})
# load config from file
c = CatConfig()
c.load_from_file('./tests/assests/test.json')
# Specify config type when initalizing CatConfig object
c = CatConfig(format='json')
c.load_from_file('./tests/assests/test.json')
# Specify config type when loading config file
c = CatConfig()
c.load_from_file('./tests/assests/test.json', format='json')

# Get item
print(c.foo)
# Print: bar
print(bool(c.some.value.does.nt.exists))
# Print: False
print(c['foo'])
# Print: bar
コード例 #8
0
def test_load_file(type, path):
    c = CatConfig(format=type)
    c.load_from_file(path)
    assert c.foo == 'bar'

    c = CatConfig()
    c.load_from_file(path, format=type)
    assert c.foo == 'bar'

    c = CatConfig(data={'previous_key': 'val'})
    c.load_from_file(path, format=type)
    assert c.previous_key == 'val'