Skip to content

Commit

Permalink
change run method to sync
Browse files Browse the repository at this point in the history
remove all stuff related to coroutines and `AioCmd`
  • Loading branch information
HugeBrain16 committed Jan 3, 2022
1 parent 3b174fa commit 6865df7
Showing 1 changed file with 9 additions and 42 deletions.
51 changes: 9 additions & 42 deletions cmdtools/ext/command.py
Expand Up @@ -88,18 +88,6 @@ def _checkfunc(cls, func) -> bool:

return False

def is_coroutine(self) -> bool:
"""check whether command is coroutine or not, based by the callbacks"""
if self.has_callback():
return inspect.iscoroutinefunction(self.callback)
if self.has_callback() and self.has_error_callback():
return inspect.iscoroutinefunction(
self.callback
) and inspect.iscoroutinefunction(self.error_callback)

# not a coroutine or object does not have command callback
return False

def has_callback(self) -> bool:
"""check whether callback is exist or not"""
return self._checkfunc(self.callback)
Expand Down Expand Up @@ -148,18 +136,6 @@ def _checkfunc(cls, func: Callable) -> bool:

return False

def is_coroutine(self) -> bool:
"""check whether command is coroutine or not, based by the callbacks"""
if self.has_callback():
return inspect.iscoroutinefunction(self.callback)
if self.has_callback() and self.has_error_callback():
return inspect.iscoroutinefunction(
self.callback
) and inspect.iscoroutinefunction(self.error_callback)

# not a coroutine or object does not have command callback
return False

def has_callback(self) -> bool:
"""check whether callback is exist or not"""
return self._checkfunc(self.callback)
Expand Down Expand Up @@ -204,13 +180,13 @@ def func_wrapper(*args, **kwargs):
raise TypeError("Cannot assign command for object:", type(obj))
return decorator

async def run(self, cmd: Union[cmdtools.Cmd, cmdtools.AioCmd], attrs: dict = None) -> Any:
def run(self, cmd: cmdtools.Cmd, attrs: dict = None) -> Any:
"""run command instance"""
if attrs is None:
attrs = {}

if not isinstance(cmd, (cmdtools.Cmd, cmdtools.AioCmd)):
raise TypeError(f"object {type(cmd)} is not a Cmd or AioCmd instance")
if not isinstance(cmd, cmdtools.Cmd):
raise TypeError(f"object {type(cmd)} is not a Cmd instance")

for command in self.commands:
if command.name == cmd.name or cmd.name in command.aliases:
Expand All @@ -222,9 +198,6 @@ async def run(self, cmd: Union[cmdtools.Cmd, cmdtools.AioCmd], attrs: dict = Non
if command.has_error_callback():
args.append(command.error_callback)

if command.is_coroutine():
return await cmd.process_cmd(*args, attrs=attrs)

return cmd.process_cmd(*args, attrs=attrs)

raise RunnerError(f"Couln't find command '{cmd.name}'")
Expand All @@ -236,11 +209,11 @@ class CommandRunner:
def __init__(self, command: CommandObject):
self.command = command

async def run(self, cmd: Union[cmdtools.Cmd, cmdtools.AioCmd], attrs: dict = None) -> Any:
def run(self, cmd: cmdtools.Cmd, attrs: dict = None) -> Any:
"""run command from parsed command object"""

if not isinstance(cmd, (cmdtools.Cmd, cmdtools.AioCmd)):
raise TypeError(f"object {type(cmd)} is not a Cmd or AioCmd instance")
if not isinstance(cmd, cmdtools.Cmd):
raise TypeError(f"object {type(cmd)} is not a Cmd instance")

if attrs is None:
attrs = {}
Expand All @@ -254,9 +227,6 @@ async def run(self, cmd: Union[cmdtools.Cmd, cmdtools.AioCmd], attrs: dict = Non
if self.command.has_error_callback():
args.append(self.command.error_callback)

if self.command.is_coroutine():
return await cmd.process_cmd(*args, attrs=attrs)

return cmd.process_cmd(*args, attrs=attrs)

raise RunnerError("Command name is invalid")
Expand All @@ -268,11 +238,11 @@ class CommandRunnerContainer:
def __init__(self, commands: List[CommandObject]):
self.commands = commands

async def run(self, cmd: Union[cmdtools.Cmd, cmdtools.AioCmd], attrs: dict = None) -> Any:
def run(self, cmd: cmdtools.Cmd, attrs: dict = None) -> Any:
"""run command from parsed command object"""

if not isinstance(cmd, (cmdtools.Cmd, cmdtools.AioCmd)):
raise TypeError(f"object {type(cmd)} is not a Cmd or AioCmd instance")
if not isinstance(cmd, cmdtools.Cmd):
raise TypeError(f"object {type(cmd)} is not a Cmd instance")

if attrs is None:
attrs = {}
Expand All @@ -287,9 +257,6 @@ async def run(self, cmd: Union[cmdtools.Cmd, cmdtools.AioCmd], attrs: dict = Non
if command.has_error_callback():
args.append(command.error_callback)

if command.is_coroutine():
return await cmd.process_cmd(*args, attrs=attrs)

return cmd.process_cmd(*args, attrs=attrs)

raise RunnerError(f"Couln't find command '{cmd.name}'")
Expand Down

0 comments on commit 6865df7

Please sign in to comment.