diff options
author | Michal Minar <miminar@redhat.com> | 2014-03-17 10:52:53 +0100 |
---|---|---|
committer | Michal Minar <miminar@redhat.com> | 2014-03-17 16:26:36 +0100 |
commit | 3c44652bf567632d3ec2c5ef400f21b01ff66a6e (patch) | |
tree | 253123554605811e620f012247969473535575b8 | |
parent | eab5df929f59827a7493651b305661a611bc25fa (diff) | |
download | openlmi-scripts-3c44652bf567632d3ec2c5ef400f21b01ff66a6e.tar.gz openlmi-scripts-3c44652bf567632d3ec2c5ef400f21b01ff66a6e.tar.xz openlmi-scripts-3c44652bf567632d3ec2c5ef400f21b01ff66a6e.zip |
fixed creation of command with helpers
Module name of commands created with helper functions
(register_subcommands, make_list_command) was taken from one of base
classes which is wrong. It should have the name of module where it's
defined.
-rw-r--r-- | lmi/scripts/common/command/helper.py | 6 | ||||
-rw-r--r-- | lmi/scripts/common/command/util.py | 28 |
2 files changed, 33 insertions, 1 deletions
diff --git a/lmi/scripts/common/command/helper.py b/lmi/scripts/common/command/helper.py index 319442b..3a2bfde 100644 --- a/lmi/scripts/common/command/helper.py +++ b/lmi/scripts/common/command/helper.py @@ -33,6 +33,7 @@ Module with convenient function for defining user commands. from lmi.scripts.common.command import LmiLister from lmi.scripts.common.command import LmiCommandMultiplexer +from lmi.scripts.common.command import util def make_list_command(func, name=None, @@ -62,7 +63,9 @@ def make_list_command(func, name = func.__name__ if not name.startswith('_'): name = '_' + name.capitalize() - props = { 'COLUMNS' : columns, 'CALLABLE' : func } + props = { 'COLUMNS' : columns + , 'CALLABLE' : func + , '__module__' : util.get_module_name() } if verify_func: props['verify_options'] = verify_func if transform_func: @@ -89,6 +92,7 @@ def register_subcommands(command_name, usage, command_map, props = { 'COMMANDS' : command_map , 'OWN_USAGE' : True , '__doc__' : usage + , '__module__' : util.get_module_name() , 'FALLBACK_COMMAND' : fallback_command } return LmiCommandMultiplexer.__metaclass__(command_name, (LmiCommandMultiplexer, ), props) diff --git a/lmi/scripts/common/command/util.py b/lmi/scripts/common/command/util.py index eb2c9af..914850a 100644 --- a/lmi/scripts/common/command/util.py +++ b/lmi/scripts/common/command/util.py @@ -31,6 +31,8 @@ Utility functions used in command sub-package. """ +import inspect +import os import re #: Regular expression matching bracket argument such as ``<arg_name>``. @@ -79,3 +81,29 @@ def is_abstract_method(clss, method, missing_is_abstract=False): return False return missing_is_abstract +def get_module_name(frame_level=2): + """ + Get a module name of caller from particular outer frame. + + :param integer frame_level: Number of nested frames to skip when searching + for called function scope by inspecting stack upwards. When the result + of this function is applied directly on the definition of function, + it's value should be 1. When used from inside of some other factory, it + must be increased by 1. + + Level 0 returns name of this module. Level 1 returns module name of + caller. Level 2 returns module name of caller's caller. + :returns: Module name. + :rtype: string + """ + frame = inspect.currentframe() + while frame_level > 0 and frame.f_back: + frame = frame.f_back + frame_level -= 1 + module = getattr(frame, 'f_globals', {}).get('__name__', None) + if module is None: + if hasattr(frame, 'f_code'): + module = os.path.basename(frame.f_code.co_filename.splitext())[0] + else: + module = '_unknown_' + return module |