def main(): """Main""" args = _create_parser().parse_args() json_schema_repo = json.SchemaRepository( json.json_schema_repo, *args.additional_json_schemas_paths) translators = [] for module in itertools.chain(builtin_translators, args.module_names): translators += importlib.import_module(module).translators format = {'yaml': json.Format.YAML, 'json': json.Format.JSON}[args.format] if args.action == 'list': output = [_translator_to_json(trans) for trans in translators] elif args.action == 'translate': trans = util.first( translators[::-1], lambda i: i.input_type == args.input_type and i. output_type == args.output_type) if not trans: raise Exception('translator not found') input_conf = json.decode(sys.stdin.read(), format=format) if trans.input_schema: json_schema_repo.validate(trans.input_schema, input_conf) output = trans.translate(input_conf) if trans.output_schema: json_schema_repo.validate(trans.output_schema, output) else: raise NotImplementedError() print(json.encode(output, format=format, indent=4))
def _format_payload(payload, default='-'): if not payload: return (default, default) type = { common.EventPayloadType.BINARY: 'BINARY', common.EventPayloadType.JSON: 'JSON', common.EventPayloadType.SBS: 'SBS' }[payload.type] data = { common.EventPayloadType.BINARY: lambda: payload.data.hex(), common.EventPayloadType.JSON: lambda: json.encode(payload.data), common.EventPayloadType.SBS: lambda: json.encode({ 'module': payload.data.module, 'type': payload.data.type, 'data': payload.data.data.hex() }) }[payload.type]() return (type, data)
async def test_invalid_client_message(server_address, tmpdir): server = await create_server(server_address, tmpdir, []) async with aiohttp.ClientSession() as session: async with session.ws_connect(server_address + '/ws') as ws: await ws.send_bytes(b'123') while not ws.closed: await ws.receive() async with aiohttp.ClientSession() as session: async with session.ws_connect(server_address + '/ws') as ws: await ws.send_str(json.encode({'type': 'invalid'})) while not ws.closed: await ws.receive() async with aiohttp.ClientSession() as session: async with session.ws_connect(server_address + '/ws') as ws: await ws.send_str(json.encode({'type': 'start', 'id': 'invalid'})) while not ws.closed: await ws.receive() await server.async_close()
def event_payload_to_sbs(payload): """Convert EventPayload to SBS data Args: payload (EventPayload): event payload Returns: hat.sbs.Data """ return { EventPayloadType.BINARY: lambda: ('binary', payload.data), EventPayloadType.JSON: lambda: ('json', json.encode(payload.data)), EventPayloadType.SBS: lambda: ('sbs', _sbs_data_to_sbs(payload.data)) }[payload.type]()
def run_translator(action, args, data=None, decode=True): data_str = (json.encode(data, format=format) if data is not None else None) p = subprocess.run([ 'python', '-m', 'hat.translator', action, '--format', format.name.lower(), '--module', 'test_sys.test_translator.test_main', '--additional-json-schemas-path', str(Path(__file__).with_suffix('.yaml')) ] + args, input=data_str, stdout=subprocess.PIPE, check=True, stderr=subprocess.DEVNULL, universal_newlines=True) return json.decode(p.stdout, format=format) if decode else p.stdout
async def _query(args, client, printer): data = common.QueryData( event_ids=args.event_ids, event_types=args.event_types, t_from=args.t_from, t_to=args.t_to, source_t_from=args.source_t_from, source_t_to=args.source_t_to, payload=(common.EventPayload(type=common.EventPayloadType.JSON, data=json.encode(args.payload)) if args.payload is not None else None), order=args.order, order_by=args.order_by, unique_type=args.unique_type, max_results=args.max_results) events = await client.query(data) printer.print(events)