summaryrefslogtreecommitdiff
path: root/cmd2
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2020-09-10 09:15:05 -0400
committeranselor <anselor@gmail.com>2020-09-11 13:50:45 -0400
commit872da20feba57f42dde204da01dc48c4c87e1b54 (patch)
tree37f7812aae5eddac9d8d37ead8b22828378e7497 /cmd2
parent6093e5e9c1b6366c67323f090d21696e867b6625 (diff)
downloadcmd2-git-872da20feba57f42dde204da01dc48c4c87e1b54.tar.gz
Changes default category to be heritable by default - meaning that subclasses will inherit the parent class's default category.
Adds optional flag to disable heritability.
Diffstat (limited to 'cmd2')
-rw-r--r--cmd2/cmd2.py7
-rw-r--r--cmd2/command_definition.py24
-rw-r--r--cmd2/constants.py1
-rw-r--r--cmd2/utils.py5
4 files changed, 32 insertions, 5 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 3e9ea9bc..97378bef 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -46,7 +46,7 @@ from . import ansi, constants, plugin, utils
from .argparse_custom import DEFAULT_ARGUMENT_PARSER, CompletionItem
from .clipboard import can_clip, get_paste_buffer, write_to_paste_buffer
from .command_definition import CommandSet
-from .constants import COMMAND_FUNC_PREFIX, COMPLETER_FUNC_PREFIX, HELP_FUNC_PREFIX
+from .constants import CLASS_ATTR_DEFAULT_HELP_CATEGORY, COMMAND_FUNC_PREFIX, COMPLETER_FUNC_PREFIX, HELP_FUNC_PREFIX
from .decorators import with_argparser, as_subcommand_to
from .exceptions import (
CommandSetRegistrationError,
@@ -483,6 +483,8 @@ class Cmd(cmd.Cmd):
predicate=lambda meth: isinstance(meth, Callable)
and hasattr(meth, '__name__') and meth.__name__.startswith(COMMAND_FUNC_PREFIX))
+ default_category = getattr(cmdset, CLASS_ATTR_DEFAULT_HELP_CATEGORY, None)
+
installed_attributes = []
try:
for method_name, method in methods:
@@ -505,6 +507,9 @@ class Cmd(cmd.Cmd):
self._cmd_to_command_sets[command] = cmdset
+ if default_category and not hasattr(method, constants.CMD_ATTR_HELP_CATEGORY):
+ utils.categorize(method, default_category)
+
self._installed_command_sets.append(cmdset)
self._register_subcommands(cmdset)
diff --git a/cmd2/command_definition.py b/cmd2/command_definition.py
index 64adaada..3f05792c 100644
--- a/cmd2/command_definition.py
+++ b/cmd2/command_definition.py
@@ -4,7 +4,7 @@ Supports the definition of commands in separate classes to be composed into cmd2
"""
from typing import Optional, Type
-from .constants import COMMAND_FUNC_PREFIX
+from .constants import CLASS_ATTR_DEFAULT_HELP_CATEGORY, COMMAND_FUNC_PREFIX
from .exceptions import CommandSetRegistrationError
# Allows IDEs to resolve types without impacting imports at runtime, breaking circular dependency issues
@@ -17,22 +17,40 @@ except ImportError: # pragma: no cover
pass
-def with_default_category(category: str):
+def with_default_category(category: str, *, heritable: bool = True):
"""
Decorator that applies a category to all ``do_*`` command methods in a class that do not already
have a category specified.
+ CommandSets that are decorated by this with `heritable` set to True (default) will set a class attribute that is
+ inherited by all subclasses unless overridden. All commands of this CommandSet and all subclasses of this CommandSet
+ that do not declare an explicit category will be placed in this category. Subclasses may use this decorator to
+ override the default category.
+
+ If `heritable` is set to False, then only the commands declared locally to this CommandSet will be placed in the
+ specified category. Dynamically created commands, and commands declared in sub-classes will not receive this
+ category.
+
:param category: category to put all uncategorized commands in
+ :param heritable: Flag whether this default category should apply to sub-classes. Defaults to True
:return: decorator function
"""
def decorate_class(cls: Type[CommandSet]):
+ if heritable:
+ setattr(cls, CLASS_ATTR_DEFAULT_HELP_CATEGORY, category)
+
from .constants import CMD_ATTR_HELP_CATEGORY
import inspect
from .decorators import with_category
+ # get members of the class that meet the following criteria:
+ # 1. Must be a function
+ # 2. Must start with COMMAND_FUNC_PREFIX (do_)
+ # 3. Must be a member of the class being decorated and not one inherited from a parent declaration
methods = inspect.getmembers(
cls,
- predicate=lambda meth: inspect.isfunction(meth) and meth.__name__.startswith(COMMAND_FUNC_PREFIX))
+ predicate=lambda meth: inspect.isfunction(meth) and meth.__name__.startswith(COMMAND_FUNC_PREFIX)
+ and meth in inspect.getmro(cls)[0].__dict__.values())
category_decorator = with_category(category)
for method in methods:
if not hasattr(method[1], CMD_ATTR_HELP_CATEGORY):
diff --git a/cmd2/constants.py b/cmd2/constants.py
index 037a7cab..552c1a74 100644
--- a/cmd2/constants.py
+++ b/cmd2/constants.py
@@ -39,6 +39,7 @@ COMPLETER_FUNC_PREFIX = 'complete_'
# The custom help category a command belongs to
CMD_ATTR_HELP_CATEGORY = 'help_category'
+CLASS_ATTR_DEFAULT_HELP_CATEGORY = 'cmd2_default_help_category'
# The argparse parser for the command
CMD_ATTR_ARGPARSER = 'argparser'
diff --git a/cmd2/utils.py b/cmd2/utils.py
index a2b1c854..d396fb6a 100644
--- a/cmd2/utils.py
+++ b/cmd2/utils.py
@@ -1038,7 +1038,10 @@ def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None
for item in func:
setattr(item, constants.CMD_ATTR_HELP_CATEGORY, category)
else:
- setattr(func, constants.CMD_ATTR_HELP_CATEGORY, category)
+ if inspect.ismethod(func):
+ setattr(func.__func__, constants.CMD_ATTR_HELP_CATEGORY, category)
+ else:
+ setattr(func, constants.CMD_ATTR_HELP_CATEGORY, category)
def get_defining_class(meth):