def test_schema_repository_init_paths(tmp_path): dir_path = tmp_path / 'repo' json_path = dir_path / 'schema.json' yaml_path = dir_path / 'schema.yaml' dir_path.mkdir() with open(json_path, 'w', encoding='utf-8') as f: f.write('{"id": "xyz1://abc1"}') with open(yaml_path, 'w', encoding='utf-8') as f: f.write("id: 'xyz2://abc2'") repo1 = json.SchemaRepository(dir_path) repo2 = json.SchemaRepository(json_path, yaml_path) assert repo1.to_json() == repo2.to_json()
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)
from pathlib import Path import asyncio import contextlib from hat import aio from hat import chatter from hat import json from hat import sbs import hat.event.server.common package_path = Path(__file__).parent json_schema_id = "test://modules/remote.yaml#" json_schema_repo = json.SchemaRepository(package_path / 'remote.yaml') sbs_repo = sbs.Repository(chatter.sbs_repo, package_path / 'remote.sbs') async def create(conf, engine): module = RemoteModule() module._subscription = hat.event.server.common.Subscription( conf['subscriptions']) module._async_group = aio.Group() module._conn = await chatter.connect(sbs_repo, conf['address']) module._async_group.spawn(aio.call_on_cancel, module._on_close) module._send('ModuleCreate', None) return module class RemoteModule(hat.event.server.common.Module): @property
def generate(): repo = json.SchemaRepository(*src_paths) data = repo.to_json() for dst_path in dst_paths: json.encode_file(data, dst_path, indent=None)
import collections import datetime import enum import struct import typing from hat import chatter from hat import json from hat import sbs import hat.monitor.common package_path: Path = Path(__file__).parent """Python package path""" json_schema_repo: json.SchemaRepository = json.SchemaRepository( json.json_schema_repo, hat.monitor.common.json_schema_repo, json.SchemaRepository.from_json(package_path / 'json_schema_repo.json')) """JSON schema repository""" sbs_repo = sbs.Repository( chatter.sbs_repo, sbs.Repository.from_json(package_path / 'sbs_repo.json')) """SBS schema repository""" EventType: typing.Type = typing.Tuple[str, ...] """Event type""" Order = enum.Enum('Order', ['DESCENDING', 'ASCENDING']) OrderBy = enum.Enum('OrderBy', ['TIMESTAMP', 'SOURCE_TIMESTAMP']) EventPayloadType = enum.Enum('EventPayloadType', ['BINARY', 'JSON', 'SBS'])
"""Common gateway interfaces""" from pathlib import Path import abc import typing from hat import aio from hat import json import hat.event.common import hat.monitor.common json_schema_repo: json.SchemaRepository = json.SchemaRepository( json.json_schema_repo, hat.monitor.common.json_schema_repo, json.SchemaRepository.from_json( Path(__file__).parent / 'json_schema_repo.json')) """JSON schema repository""" DeviceConf = json.Data """Device configuration""" EventTypePrefix = hat.event.common.EventType """Event type prefix""" CreateDevice = aio.AsyncCallable[ [DeviceConf, 'DeviceEventClient', EventTypePrefix], 'Device'] """Create device callable""" class Device(aio.Resource): """Device interface
def test_json_schema_repository_validate_invalid(schemas, schema_id, data): repo = json.SchemaRepository(*[json.decode(i, format=json.Format.YAML) for i in schemas]) with pytest.raises(Exception): repo.validate(schema_id, data)
def test_json_schema_repository_validate(schemas, schema_id, data): repo = json.SchemaRepository(*[json.decode(i, format=json.Format.YAML) for i in schemas]) repo.validate(schema_id, data)
def test_schema_repository_init_duplicate_id(): schema = json.decode("id: 'xyz://abc'", format=json.Format.YAML) repo = json.SchemaRepository(schema) assert repo.to_json() == json.SchemaRepository(repo, repo).to_json() with pytest.raises(Exception): json.SchemaRepository(schema, schema)
def test_schema_repository_init(): schema = json.decode("id: 'xyz://abc'", format=json.Format.YAML) repo = json.SchemaRepository(schema) assert repo.to_json()
def test_schema_repository_init_empty(): repo = json.SchemaRepository() assert not repo.to_json()