def route_cli_commands_map(cls): mapping = {} for route_method in cls.iter_route_methods(): method_name = names.to_variable_name(route_method.__name__) command_name = route_method.cli_command_name if command_name is False: mapping[method_name] = False elif route_method.client_methods: for name in route_method.client_methods: mapping[name] = names.to_command(name) else: mapping[method_name] = command_name or names.to_command(route_method.__name__) return mapping
def iter_modules(package, parents=None): """ Iterates recursively the package's modules :param package: a package to iterate over :param parents: patents of package :return: a list of tuples: ( module: module object, parents: list of packages names the the module exists in. ) """ modules = [] parents = parents or [] for _, name, is_package in pkgutil.iter_modules(package.__path__): if is_package: # name is package name package_parents = parents[:] son_package = importlib.import_module(".".join([package.__name__, name]), package.__name__) path = getattr(son_package, "PATH", name) class_name = names.to_class_name(path) path_name = getattr(son_package, "PATH", names.to_path(path)) cli_command_name = getattr(son_package, "CLI_COMMAND_NAME", names.to_command(path)) package_parents.append(Package(name=name, path=path_name, class_name=class_name, cli_command_name=cli_command_name)) modules.extend(iter_modules(son_package, package_parents)) else: # name is module name module = importlib.import_module(".".join([package.__name__, name]), package.__name__) modules.append((module, parents)) return modules
def _load_class(self, instance, commands=None): resource_command_name = getattr(instance, 'CLI_COMMAND_NAME', '') def cli_command_map(func_name): return getattr(instance, 'ROUTE_CLI_COMMAND_MAP', {func_name: func_name})[func_name] if resource_command_name is False and self.remove_commands_with_name_false: return commands = (commands or []) + [ resource_command_name if isinstance(resource_command_name, six.string_types) else names.to_command(instance.__class__.__name__) ] for name in dir(instance): # Get all clients' resources: attribute = getattr(instance, name) if isinstance(attribute, type) or name.startswith('_') or name in IGNORES: continue if callable(attribute): self._add_command(attribute, commands, cli_command_map(name)) else: self._load_class(attribute, commands)
def _add_command(self, method, commands, command_name): if command_name is False and self.remove_commands_with_name_false: return commands = commands + [command_name or names.to_command(method.__name__)] command_type = command.factory(method, self.column_order, self.column_colors) command_name = ' '.join([part for part in commands if part]) LOG.debug('Adding command: %s', command_name) self.add_command(command_name, command_type)
def _join_hierarchy(parents, resource_class, route_method, sep): if (route_method is False or resource_class.cli_command_name() is False or any((parent.cli_command_name is False for parent in parents))): return None command = [parent.cli_command_name for parent in parents] + [ resource_class.cli_command_name(), route_method.cli_command_name or names.to_command(route_method.__name__), ] return sep.join(command)
def _build_cli_command(parents, resource_class, route_method): if (route_method is False or resource_class.cli_command_name() is False or any((parent.cli_command_name is False for parent in parents))): return None command = [parent.cli_command_name for parent in parents] + [ resource_class.cli_command_name(), route_method.cli_command_name or names.to_command(route_method.__name__), ] return ' '.join(command)
def _build_cli_command(parents, resource_class, route_method): if ( route_method is False or resource_class.cli_command_name() is False or any((parent.cli_command_name is False for parent in parents)) ): return None command = [parent.cli_command_name for parent in parents] + [ resource_class.cli_command_name(), route_method.cli_command_name or names.to_command(route_method.__name__), ] return ' '.join(command)
def policy_group_name(cls): if cls.POLICY_GROUP_NAME is False: return False return cls.POLICY_GROUP_NAME or names.to_command(cls.name().lower())
def cli_command_name(cls): return cls.CLI_COMMAND_NAME if cls.CLI_COMMAND_NAME is not None else names.to_command(cls.name())
def _parser_name(self): return '--{}'.format(names.to_command(self.name).strip('_'))