hat.gateway.main
Gateway main
1"""Gateway main""" 2 3from pathlib import Path 4import argparse 5import asyncio 6import contextlib 7import logging.config 8import sys 9 10import appdirs 11 12from hat import aio 13from hat import json 14 15from hat.gateway import common 16from hat.gateway.runner import MainRunner 17 18 19mlog: logging.Logger = logging.getLogger('hat.gateway.main') 20"""Module logger""" 21 22user_conf_dir: Path = Path(appdirs.user_config_dir('hat')) 23"""User configuration directory path""" 24 25 26def create_argument_parser() -> argparse.ArgumentParser: 27 """Create argument parser""" 28 parser = argparse.ArgumentParser() 29 parser.add_argument( 30 '--conf', metavar='PATH', type=Path, default=None, 31 help="configuration defined by hat-gateway://main.yaml " 32 "(default $XDG_CONFIG_HOME/hat/gateway.{yaml|yml|toml|json})") 33 return parser 34 35 36def main(): 37 """Gateway""" 38 parser = create_argument_parser() 39 args = parser.parse_args() 40 conf = json.read_conf(args.conf, user_conf_dir / 'gateway') 41 sync_main(conf) 42 43 44def sync_main(conf: json.Data): 45 """Sync main entry point""" 46 aio.init_asyncio() 47 48 common.json_schema_repo.validate('hat-gateway://main.yaml', conf) 49 50 for device_conf in conf['devices']: 51 info = common.import_device_info(device_conf['module']) 52 if info.json_schema_repo and info.json_schema_id: 53 info.json_schema_repo.validate(info.json_schema_id, device_conf) 54 55 log_conf = conf.get('log') 56 if log_conf: 57 logging.config.dictConfig(log_conf) 58 59 with contextlib.suppress(asyncio.CancelledError): 60 aio.run_asyncio(async_main(conf)) 61 62 63async def async_main(conf: json.Data): 64 """Async main entry point""" 65 main_runner = MainRunner(conf) 66 67 async def cleanup(): 68 await main_runner.async_close() 69 await asyncio.sleep(0.1) 70 71 try: 72 await main_runner.wait_closing() 73 74 finally: 75 await aio.uncancellable(cleanup()) 76 77 78if __name__ == '__main__': 79 sys.argv[0] = 'hat-gateway' 80 sys.exit(main())
Module logger
user_conf_dir: pathlib.Path =
PosixPath('/home/runner/.config/hat')
User configuration directory path
def
create_argument_parser() -> argparse.ArgumentParser:
27def create_argument_parser() -> argparse.ArgumentParser: 28 """Create argument parser""" 29 parser = argparse.ArgumentParser() 30 parser.add_argument( 31 '--conf', metavar='PATH', type=Path, default=None, 32 help="configuration defined by hat-gateway://main.yaml " 33 "(default $XDG_CONFIG_HOME/hat/gateway.{yaml|yml|toml|json})") 34 return parser
Create argument parser
def
main():
37def main(): 38 """Gateway""" 39 parser = create_argument_parser() 40 args = parser.parse_args() 41 conf = json.read_conf(args.conf, user_conf_dir / 'gateway') 42 sync_main(conf)
Gateway
def
sync_main( conf: None | bool | int | float | str | list[ForwardRef('Data')] | dict[str, ForwardRef('Data')]):
45def sync_main(conf: json.Data): 46 """Sync main entry point""" 47 aio.init_asyncio() 48 49 common.json_schema_repo.validate('hat-gateway://main.yaml', conf) 50 51 for device_conf in conf['devices']: 52 info = common.import_device_info(device_conf['module']) 53 if info.json_schema_repo and info.json_schema_id: 54 info.json_schema_repo.validate(info.json_schema_id, device_conf) 55 56 log_conf = conf.get('log') 57 if log_conf: 58 logging.config.dictConfig(log_conf) 59 60 with contextlib.suppress(asyncio.CancelledError): 61 aio.run_asyncio(async_main(conf))
Sync main entry point
async def
async_main( conf: None | bool | int | float | str | list[ForwardRef('Data')] | dict[str, ForwardRef('Data')]):
64async def async_main(conf: json.Data): 65 """Async main entry point""" 66 main_runner = MainRunner(conf) 67 68 async def cleanup(): 69 await main_runner.async_close() 70 await asyncio.sleep(0.1) 71 72 try: 73 await main_runner.wait_closing() 74 75 finally: 76 await aio.uncancellable(cleanup())
Async main entry point