Use click context

Defer async to click command
This commit is contained in:
up-n-atom
2023-08-17 00:22:55 -04:00
committed by GitHub
parent ad61ab26b8
commit 365b381ae5
+12 -11
View File
@@ -29,17 +29,18 @@ class EnumChoice(click.Choice):
@click.option('-a', '--authentication-method', @click.option('-a', '--authentication-method',
default=EncryptionMethod.SHA512, type=EnumChoice(EncryptionMethod), default=EncryptionMethod.SHA512, type=EnumChoice(EncryptionMethod),
help='Authentication method') help='Authentication method')
async def main(host: str, username: str, password: str, authentication_method: EncryptionMethod) -> None: @click.pass_context
async with SagemcomClient(**locals(), ssl=True, keep_keys=True) as client: async def main(ctx: click.Context, host: str, username: str, password: str, authentication_method: EncryptionMethod) -> None:
try: ctx.obj = client = await ctx.with_async_resource(
await client.login() SagemcomClient(host, username, password, authentication_method, ssl=True, keep_keys=True)
)
wanmode = await client.get_value_by_xpath(XPATH_DEVICE_SERVICES_BELLNETWORKCFG_WANMODE) try:
except Exception as e: await client.login()
print(e) wan_mode = await client.get_value_by_xpath(XPATH_DEVICE_SERVICES_BELLNETWORKCFG_WANMODE)
else: except Exception as e:
print(f"WAN Mode: {wanmode}") raise click.Abort(e)
else:
click.echo(f"WAN Mode: {wan_mode}")
if __name__ == '__main__': if __name__ == '__main__':
main(_anyio_backend='asyncio') main(_anyio_backend='asyncio')