summaryrefslogtreecommitdiff
path: root/plugins/template/cmd2_myplugin
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2020-07-13 15:28:40 -0400
committeranselor <anselor@gmail.com>2020-07-14 19:26:30 -0400
commite38684d219847f636562ab2720b82aae4a6fd408 (patch)
treeec20d7a46b8cba1a662d46e79d5e004590bee93b /plugins/template/cmd2_myplugin
parent683d049299c0cf7a2821b639a95ad0911bab1bc7 (diff)
downloadcmd2-git-e38684d219847f636562ab2720b82aae4a6fd408.tar.gz
Brought in cmd2 plugin template as a first-class member of cmd2 proper
Diffstat (limited to 'plugins/template/cmd2_myplugin')
-rw-r--r--plugins/template/cmd2_myplugin/__init__.py15
-rw-r--r--plugins/template/cmd2_myplugin/myplugin.py69
-rw-r--r--plugins/template/cmd2_myplugin/pylintrc10
3 files changed, 94 insertions, 0 deletions
diff --git a/plugins/template/cmd2_myplugin/__init__.py b/plugins/template/cmd2_myplugin/__init__.py
new file mode 100644
index 00000000..41f0b9cc
--- /dev/null
+++ b/plugins/template/cmd2_myplugin/__init__.py
@@ -0,0 +1,15 @@
+#
+# coding=utf-8
+"""Description of myplugin
+
+An overview of what myplugin does.
+"""
+
+from pkg_resources import get_distribution, DistributionNotFound
+
+from .myplugin import empty_decorator, MyPluginMixin # noqa: F401
+
+try:
+ __version__ = get_distribution(__name__).version
+except DistributionNotFound:
+ __version__ = 'unknown'
diff --git a/plugins/template/cmd2_myplugin/myplugin.py b/plugins/template/cmd2_myplugin/myplugin.py
new file mode 100644
index 00000000..5fa12caf
--- /dev/null
+++ b/plugins/template/cmd2_myplugin/myplugin.py
@@ -0,0 +1,69 @@
+#
+# coding=utf-8
+"""An example cmd2 plugin"""
+
+import functools
+from typing import Callable, TYPE_CHECKING
+
+import cmd2
+
+if TYPE_CHECKING:
+ _Base = cmd2.Cmd
+else:
+ _Base = object
+
+
+def empty_decorator(func: Callable) -> Callable:
+ """An empty decorator for myplugin"""
+
+ @functools.wraps(func)
+ def _empty_decorator(self, *args, **kwargs):
+ self.poutput("in the empty decorator")
+ func(self, *args, **kwargs)
+
+ _empty_decorator.__doc__ = func.__doc__
+ return _empty_decorator
+
+
+class MyPluginMixin(_Base):
+ """A mixin class which adds a 'say' command to a cmd2 subclass
+
+ The order in which you add the mixin matters. Say you want to
+ use this mixin in a class called MyApp.
+
+ class MyApp(cmd2_myplugin.MyPlugin, cmd2.Cmd):
+ def __init__(self, *args, **kwargs):
+ # gotta have this or neither the plugin or cmd2 will initialize
+ super().__init__(*args, **kwargs)
+ """
+
+ def __init__(self, *args, **kwargs):
+ # code placed here runs before cmd2 initializes
+ super().__init__(*args, **kwargs)
+ # code placed here runs after cmd2 initializes
+ # this is where you register any hook functions
+ self.register_preloop_hook(self.cmd2_myplugin_preloop_hook)
+ self.register_postloop_hook(self.cmd2_myplugin_postloop_hook)
+ self.register_postparsing_hook(self.cmd2_myplugin_postparsing_hook)
+
+ def do_say(self, statement):
+ """Simple say command"""
+ self.poutput(statement)
+
+ #
+ # define hooks as functions, not methods
+ def cmd2_myplugin_preloop_hook(self) -> None:
+ """Method to be called before the command loop begins"""
+ self.poutput("preloop hook")
+
+ def cmd2_myplugin_postloop_hook(self) -> None:
+ """Method to be called after the command loop finishes"""
+ self.poutput("postloop hook")
+
+ def cmd2_myplugin_postparsing_hook(
+ self,
+ data: cmd2.plugin.PostparsingData
+ ) -> cmd2.plugin.PostparsingData:
+ """Method to be called after parsing user input, but before running the command"""
+ self.poutput('in postparsing hook')
+ return data
diff --git a/plugins/template/cmd2_myplugin/pylintrc b/plugins/template/cmd2_myplugin/pylintrc
new file mode 100644
index 00000000..2f6d3de2
--- /dev/null
+++ b/plugins/template/cmd2_myplugin/pylintrc
@@ -0,0 +1,10 @@
+#
+# pylint configuration
+#
+# $ pylint --rcfile=cmd2_myplugin/pylintrc cmd2_myplugin
+#
+
+[messages control]
+# too-few-public-methods pylint expects a class to have at
+# least two public methods
+disable=too-few-public-methods