Beispiel #1
0
async def test_set_settings(settings, conf_path, ui_path, addr,
                            patch_autoflush):
    conf = {'settings': settings, 'devices': []}
    srv = await hat.manager.server.create_server(conf, conf_path, ui_path)
    conn = juggler.RpcConnection(await juggler.connect(addr))
    data_settings_queue = create_remote_data_change_queue(conn, 'settings')

    data_settings = await data_settings_queue.get()
    assert data_settings == settings

    await conn.call('save')
    new_conf = json.decode_file(conf_path)
    assert new_conf['settings'] == settings

    await conn.call('set_settings', ['ui', 'address'], 'abc')
    new_settings = json.set_(settings, ['ui', 'address'], 'abc')
    data_settings = await data_settings_queue.get()
    assert data_settings == new_settings

    await conn.call('save')
    new_conf = json.decode_file(conf_path)
    assert new_conf['settings'] == new_settings

    await conn.async_close()
    await srv.async_close()
Beispiel #2
0
def _ext_get_view(name, view_path, conf_path):
    data = {}
    for i in view_path.rglob('*'):
        if i.is_dir():
            continue

        if i.suffix in {'.js', '.css', '.txt'}:
            with open(i, encoding='utf-8') as f:
                content = f.read()

        elif i.suffix in {'.json', '.yaml', '.yml'}:
            content = json.decode_file(i)

        elif i.suffix in {'.xml', '.svg'}:
            with open(i, encoding='utf-8') as f:
                content = vt.parse(f)

        else:
            with open(i, 'rb') as f:
                content = f.read()
            content = base64.b64encode(content).decode('utf-8')

        file_name = i.relative_to(view_path).as_posix()
        data[file_name] = content

    conf = json.decode_file(conf_path) if conf_path else None
    schema = util.first(v for k, v in data.items()
                        if k in {'schema.json', 'schema.yaml', 'schema.yml'})
    if schema:
        repo = json.SchemaRepository(schema)
        repo.validate(schema['id'], conf)

    return View(name=name,
                conf=conf,
                data=data)
Beispiel #3
0
def test_encode_decode_file(tmp_path, format, indent, data):
    path = tmp_path / 'data'
    json.encode_file(data, path, format, indent)
    decoded = json.decode_file(path, format)
    assert data == decoded

    if format == json.Format.JSON:
        path = path.with_suffix('.json')
    elif format == json.Format.YAML:
        path = path.with_suffix('.yaml')
    else:
        raise NotImplementedError()
    json.encode_file(data, path, None, indent)
    decoded = json.decode_file(path, None)
    assert data == decoded
Beispiel #4
0
async def test_add_remove_device(settings, conf_path, ui_path, addr,
                                 patch_autoflush, patch_device_queue):
    conf = {'settings': settings, 'devices': []}
    srv = await hat.manager.server.create_server(conf, conf_path, ui_path)
    conn = juggler.RpcConnection(await juggler.connect(addr))
    data_devices_queue = create_remote_data_change_queue(conn, 'devices')

    data_devices = await data_devices_queue.get()
    assert data_devices == {}

    device_id = await conn.call('add', 'device_type')
    data_devices = await data_devices_queue.get()
    assert device_id in data_devices
    assert data_devices[device_id]['type'] == 'device_type'

    await conn.call('set_name', device_id, 'abc')
    data_devices = await data_devices_queue.get()
    assert data_devices[device_id]['name'] == 'abc'

    await conn.call('save')
    new_conf = json.decode_file(conf_path)
    assert len(new_conf['devices']) == 1
    assert new_conf['devices'][0]['type'] == 'device_type'
    assert new_conf['devices'][0]['name'] == 'abc'

    await conn.call('remove', device_id)
    data_devices = await data_devices_queue.get()
    assert data_devices == {}

    await conn.async_close()
    await srv.async_close()
Beispiel #5
0
 def from_json(
         data: typing.Union[pathlib.PurePath, json.Data]) -> 'Repository':
     """Create repository from JSON data representation"""
     if isinstance(data, pathlib.PurePath):
         data = json.decode_file(data)
     repo = Repository()
     repo._refs = {
         common.type_from_json(k): common.type_from_json(v)
         for k, v in data
     }
     return repo
Beispiel #6
0
def main(conf: typing.Optional[Path], ui_path: Path):
    """Orchestrator"""
    aio.init_asyncio()

    if not conf:
        for suffix in ('.yaml', '.yml', '.json'):
            conf = (user_conf_dir / 'orchestrator').with_suffix(suffix)
            if conf.exists():
                break
    conf = json.decode_file(conf)
    json_schema_repo.validate('hat://orchestrator.yaml#', conf)

    logging.config.dictConfig(conf['log'])

    with contextlib.suppress(asyncio.CancelledError):
        aio.run_asyncio(async_main(conf, ui_path))
Beispiel #7
0
async def test_save(settings, conf_path, ui_path, addr):
    conf = {'settings': settings, 'devices': []}
    srv = await hat.manager.server.create_server(conf, conf_path, ui_path)
    conn = juggler.RpcConnection(await juggler.connect(addr))

    assert conf_path.exists() is False

    await conn.call('save')

    assert conf_path.exists() is True

    new_conf = json.decode_file(conf_path)

    assert new_conf['settings'] == conf['settings']
    assert new_conf['devices'] == conf['devices']

    await conn.async_close()
    await srv.async_close()
Beispiel #8
0
def main(conf: typing.Optional[Path], ui_path: Path):
    """Main entry point"""
    aio.init_asyncio()

    conf, conf_path = None, conf
    if not conf_path:
        for suffix in ('.yaml', '.yml', '.json'):
            conf_path = (user_conf_dir / 'manager').with_suffix(suffix)
            if conf_path.exists():
                break
    if conf_path.exists():
        conf = json.decode_file(conf_path)
    else:
        conf = common.default_conf
    common.json_schema_repo.validate('hat://manager/main.yaml#', conf)

    logging.config.dictConfig(conf['log'])

    with contextlib.suppress(asyncio.CancelledError):
        aio.run_asyncio(async_main(conf, conf_path, ui_path))
Beispiel #9
0
def main(conf: typing.Optional[Path]):
    """Gateway"""
    aio.init_asyncio()

    if not conf:
        for suffix in ('.yaml', '.yml', '.json'):
            conf = (user_conf_dir / 'gateway').with_suffix(suffix)
            if conf.exists():
                break
    conf = json.decode_file(conf)
    common.json_schema_repo.validate('hat://gateway/main.yaml#', conf)

    for device_conf in conf['devices']:
        module = importlib.import_module(device_conf['module'])
        if module.json_schema_repo and module.json_schema_id:
            module.json_schema_repo.validate(module.json_schema_id,
                                             device_conf)

    logging.config.dictConfig(conf['log'])

    with contextlib.suppress(asyncio.CancelledError):
        aio.run_asyncio(async_main(conf))
Beispiel #10
0
def main(conf: typing.Optional[Path]):
    """Event Server"""
    aio.init_asyncio()

    if not conf:
        for suffix in ('.yaml', '.yml', '.json'):
            conf = (user_conf_dir / 'event').with_suffix(suffix)
            if conf.exists():
                break
    conf = json.decode_file(conf)
    common.json_schema_repo.validate('hat://event/main.yaml#', conf)

    sub_confs = ([conf['backend_engine']['backend']] +
                 conf['module_engine']['modules'])
    for sub_conf in sub_confs:
        module = importlib.import_module(sub_conf['module'])
        if module.json_schema_repo and module.json_schema_id:
            module.json_schema_repo.validate(module.json_schema_id, sub_conf)

    logging.config.dictConfig(conf['log'])

    with contextlib.suppress(asyncio.CancelledError):
        aio.run_asyncio(async_main(conf))
Beispiel #11
0
def main(conf: typing.Optional[Path],
         ui_path: Path):
    """GUI Server"""
    aio.init_asyncio()

    if not conf:
        for suffix in ('.yaml', '.yml', '.json'):
            conf = (user_conf_dir / 'gui').with_suffix(suffix)
            if conf.exists():
                break
    conf = json.decode_file(conf)
    hat.gui.common.json_schema_repo.validate('hat://gui/main.yaml#', conf)

    for adapter_conf in conf['adapters']:
        module = importlib.import_module(adapter_conf['module'])
        if module.json_schema_repo and module.json_schema_id:
            module.json_schema_repo.validate(module.json_schema_id,
                                             adapter_conf)

    logging.config.dictConfig(conf['log'])

    with contextlib.suppress(asyncio.CancelledError):
        aio.run_asyncio(async_main(conf, ui_path))
Beispiel #12
0
def main(conf: typing.Optional[Path], log: typing.Optional[str],
         syslog_addr: typing.Optional[str], syslog_pem: typing.Optional[Path],
         ui_addr: typing.Optional[str], ui_pem: typing.Optional[Path],
         ui_path: Path, db_path: typing.Optional[Path],
         db_low_size: typing.Optional[int], db_high_size: typing.Optional[int],
         db_enable_archive: bool, db_disable_journal: bool):
    """Syslog Server"""
    aio.init_asyncio()

    if not conf:
        for suffix in ('.yaml', '.yml', '.json'):
            conf = (user_conf_dir / 'syslog').with_suffix(suffix)
            if conf.exists():
                break
        else:
            conf = None
    if conf:
        conf = json.decode_file(conf)
        common.json_schema_repo.validate('hat://syslog.yaml#', conf)

    if log:
        log_conf = _get_console_log_conf(log)
    elif conf:
        log_conf = conf['log']
    else:
        log_conf = {'version': 1}

    if not syslog_addr:
        syslog_addr = conf['syslog_addr'] if conf else default_syslog_addr

    if not syslog_pem and conf and 'syslog_pem' in conf:
        syslog_pem = Path(conf['syslog_pem'])

    if not ui_addr:
        ui_addr = conf['ui_addr'] if conf else default_ui_addr

    if not ui_pem and conf and 'ui_pem' in conf:
        ui_pem = Path(conf['ui_pem'])

    if not db_path:
        db_path = Path(conf['db_path']) if conf else default_db_path

    if db_low_size is None:
        db_low_size = conf['db_low_size'] if conf else default_db_low_size

    if db_high_size is None:
        db_high_size = conf['db_high_size'] if conf else default_db_high_size

    if not db_enable_archive and conf:
        db_enable_archive = conf['db_enable_archive']

    if not db_disable_journal and conf:
        db_disable_journal = conf['db_disable_journal']

    logging.config.dictConfig(log_conf)
    with contextlib.suppress(asyncio.CancelledError):
        aio.run_asyncio(
            async_main(syslog_addr=syslog_addr,
                       syslog_pem=syslog_pem,
                       ui_addr=ui_addr,
                       ui_pem=ui_pem,
                       ui_path=ui_path,
                       db_path=db_path,
                       db_low_size=db_low_size,
                       db_high_size=db_high_size,
                       db_enable_archive=db_enable_archive,
                       db_disable_journal=db_disable_journal))
Beispiel #13
0
 async def get_settings(self):
     if not settings_path.exists():
         return
     return json.decode_file(settings_path)