Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
commit d4f169e7fca81c2b2b85eb4e9dfc19b7b2d36022 Author: Peter Hatina phatina@redhat.com Date: Fri Aug 9 12:06:19 2013 +0200
introduce custom help() function
cli/lmi/shell/LMIConsole.py | 5 +++- cli/lmi/shell/LMIHelper.py | 54 +++++++++++++++++++++++++++++++++++++++++++ cli/lmi/shell/LMIUtil.py | 7 +++++ 3 files changed, 65 insertions(+), 1 deletions(-)
diff --git a/cli/lmi/shell/LMIConsole.py b/cli/lmi/shell/LMIConsole.py index 31cecd6..1a0bd3e 100644 --- a/cli/lmi/shell/LMIConsole.py +++ b/cli/lmi/shell/LMIConsole.py @@ -39,6 +39,8 @@ from LMIUtil import lmi_set_use_exceptions from LMIUtil import lmi_get_use_exceptions from LMIUtil import lmi_isinstance
+from LMIHelper import LMIHelper + class LMIConsole(code.InteractiveConsole): """ Class representing an interactive console. @@ -64,7 +66,8 @@ class LMIConsole(code.InteractiveConsole): "LMIInstanceName" : LMIObjectFactory().LMIInstanceName, "LMIMethod" : LMIObjectFactory().LMIMethod, "use_exceptions" : lmi_set_use_exceptions, - "lmi_isinstance" : lmi_isinstance + "lmi_isinstance" : lmi_isinstance, + "help" : LMIHelper() }
def __init__(self): diff --git a/cli/lmi/shell/LMIHelper.py b/cli/lmi/shell/LMIHelper.py new file mode 100644 index 0000000..5984db9 --- /dev/null +++ b/cli/lmi/shell/LMIHelper.py @@ -0,0 +1,54 @@ +# Copyright (C) 2012-2013 Peter Hatina phatina@redhat.com +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see http://www.gnu.org/licenses/. + +import os +import sys +import subprocess +from site import _Helper + +from LMIUtil import lmi_script_name + +class LMIHelper(_Helper): + """ + LMI Helper class, which overrides python help. + """ + def __repr__(self): + """ + Returns a pretty string for the object. + """ + return "Type help() to see man page for %s, " \ + "or help(object) for help about object." % lmi_script_name() + + def __call__(self, *args, **kwargs): + """ + Executes either man page for LMIShell or prints pydoc help for an object. + """ + if not args and not kwargs: + try: + # Python 3+ + from subprocess import DEVNULL as devnull + devnull_close = lambda: None + except ImportError: + devnull = open(os.devnull, "wb") + devnull_close = lambda: devnull.close() + rcode = subprocess.call(["man", lmi_script_name()], stderr=devnull) + devnull_close() + if rcode > 0: + sys.stderr.write("Man page for %s can not be found, " % lmi_script_name()) + sys.stderr.write("refer to Wiki page instead.\n\n") + sys.stderr.write("Available at: https://fedorahosted.org/openlmi/wiki/shell%5Cn" ) + else: + import pydoc + return pydoc.help(*args, **kwargs) diff --git a/cli/lmi/shell/LMIUtil.py b/cli/lmi/shell/LMIUtil.py index 990ac8d..9f34d21 100644 --- a/cli/lmi/shell/LMIUtil.py +++ b/cli/lmi/shell/LMIUtil.py @@ -13,6 +13,7 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, see http://www.gnu.org/licenses/.
+import os import sys import pywbem
@@ -314,3 +315,9 @@ def lmi_isinstance(lmi_obj, lmi_class): return True classname = client._get_superclass(classname, namespace) return False + +def lmi_script_name(): + """ + Returns a string of executed script. + """ + return os.path.basename(sys.argv[0])
cura-tools-devel@lists.fedorahosted.org