mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 01:52:56 +01:00
Cmds.py: Run all commands in asyncio event loop
This commit makee Cmds run all sub-commands in an asyncio event loop. The event loop is currently passed to the commands, which seems unnecessary and looks like it's using a feature which is bound to be deprecated in the future. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
036a978130
commit
6123a68195
1 changed files with 28 additions and 8 deletions
|
|
@ -1,15 +1,16 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
import jwutils
|
|
||||||
import importlib
|
import importlib
|
||||||
import inspect
|
import inspect
|
||||||
import re
|
import re
|
||||||
import pickle
|
import pickle
|
||||||
|
import asyncio
|
||||||
|
import jwutils
|
||||||
|
|
||||||
class Cmds: # export
|
class Cmds: # export
|
||||||
|
|
||||||
def __init__(self, description = '', filter = '^Cmd.*', modules=None):
|
def __init__(self, description = '', filter = '^Cmd.*', modules=None, eloop=None):
|
||||||
self.__description = description
|
self.__description = description
|
||||||
self.__filter = filter
|
self.__filter = filter
|
||||||
self.__modules = modules
|
self.__modules = modules
|
||||||
|
|
@ -17,6 +18,11 @@ class Cmds: # export
|
||||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, description=self.__description)
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter, description=self.__description)
|
||||||
self.__parser.add_argument('--log-flags', help='Log flags', default='stderr,position,prio,color')
|
self.__parser.add_argument('--log-flags', help='Log flags', default='stderr,position,prio,color')
|
||||||
self.__parser.add_argument('--log-level', help='Log level', default=jwutils.log.NOTICE)
|
self.__parser.add_argument('--log-level', help='Log level', default=jwutils.log.NOTICE)
|
||||||
|
self.eloop = eloop
|
||||||
|
self.__own_eloop = False
|
||||||
|
if eloop is None:
|
||||||
|
self.eloop = asyncio.get_event_loop()
|
||||||
|
self.__own_eloop = True
|
||||||
subparsers = self.__parser.add_subparsers(title='Available commands', metavar='')
|
subparsers = self.__parser.add_subparsers(title='Available commands', metavar='')
|
||||||
if self.__modules == None:
|
if self.__modules == None:
|
||||||
self.__modules = [ '__main__' ]
|
self.__modules = [ '__main__' ]
|
||||||
|
|
@ -28,17 +34,31 @@ class Cmds: # export
|
||||||
continue
|
continue
|
||||||
if inspect.isabstract(c):
|
if inspect.isabstract(c):
|
||||||
continue
|
continue
|
||||||
c().add_parser(subparsers)
|
cmd = c()
|
||||||
|
cmd.cmds = self
|
||||||
|
cmd.add_parser(subparsers)
|
||||||
|
|
||||||
|
async def __run(self):
|
||||||
|
#def __run(self):
|
||||||
|
args = self.__parser.parse_args()
|
||||||
|
jwutils.log.set_level(args.log_level)
|
||||||
|
jwutils.log.set_flags(args.log_flags)
|
||||||
|
return await args.func(args)
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
if self.__own_eloop:
|
||||||
|
if self.eloop is not None:
|
||||||
|
self.eloop.close()
|
||||||
|
self.eloop = None
|
||||||
|
self.__own_eloop = False
|
||||||
|
|
||||||
def parser():
|
def parser():
|
||||||
return self.__parser
|
return self.__parser
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
args = self.__parser.parse_args()
|
#return self.__run()
|
||||||
jwutils.log.set_level(args.log_level)
|
return self.eloop.run_until_complete(self.__run())
|
||||||
jwutils.log.set_flags(args.log_flags)
|
|
||||||
args.func(args)
|
|
||||||
|
|
||||||
def run_sub_commands(description = '', filter = '^Cmd.*', modules=None): # export
|
def run_sub_commands(description = '', filter = '^Cmd.*', modules=None): # export
|
||||||
cmds = Cmds(description, filter, modules)
|
cmds = Cmds(description, filter, modules)
|
||||||
cmds.run()
|
return cmds.run()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue