import importlib


def from_name(name):
    """
    Return a command by it's class name.

    name is either some.module.path:ClassName if you need to specify it explicitly,
    or some.module.path if .path contains a variable COMMAND, which is that class
    :param name: name to load
    :return: Command class
    :raise NameError: not found
    """
    try:
        if ':' in name:
            module, clsname = name.split(':', 2)

            return importlib.import_module(module).__dict__[clsname]
        else:
            return importlib.import_module(name).COMMAND
    except (KeyError, ImportError):
        raise NameError(name)