You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
50 lines
1.2 KiB
# Imports
|
|
|
|
import logging
|
|
# from ..scripts import Function
|
|
from ...constants import LOGGER_NAME
|
|
from .base import ItemizedCommand
|
|
from .mappings import MAPPING
|
|
|
|
log = logging.getLogger(LOGGER_NAME)
|
|
|
|
# Functions
|
|
|
|
|
|
def command_exists(name):
|
|
"""Indicates whether the named command exists.
|
|
|
|
:param name: The name of the command to be checked.
|
|
:type name: str
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
|
return name in MAPPING
|
|
|
|
|
|
def command_factory(name, comment, overlay, *args, **kwargs):
|
|
# if name in ("func", "function"):
|
|
# kwargs['comment'] = comment
|
|
# return Function(*args, **kwargs)
|
|
|
|
if not command_exists(name):
|
|
log.warning("No mapping for command: %s" % name)
|
|
return None
|
|
|
|
_args = list(args)
|
|
kwargs['comment'] = comment
|
|
kwargs['overlay'] = overlay
|
|
|
|
log.debug("%s: %s" % (comment, kwargs))
|
|
|
|
command_class = MAPPING[name]
|
|
try:
|
|
items = kwargs.pop("items", None)
|
|
if items is not None:
|
|
return ItemizedCommand(command_class, items, *_args, **kwargs)
|
|
|
|
return command_class(*_args, **kwargs)
|
|
except (KeyError, TypeError, ValueError) as e:
|
|
log.critical("Failed to load %s command: %s" % (name, e))
|
|
return None
|
|
|