Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
commit 350432469b21e2ea258a6dfebf512956aa175d5b Author: Peter Hatina phatina@redhat.com Date: Wed Jan 30 13:04:11 2013 +0100
introduce _LmiInstance::tomof()
cli-tools/lmi/lmi_client_shell.py | 94 +++++++++++++++++++++++-------------- 1 files changed, 59 insertions(+), 35 deletions(-)
diff --git a/cli-tools/lmi/lmi_client_shell.py b/cli-tools/lmi/lmi_client_shell.py index 38bbc82..03bd667 100644 --- a/cli-tools/lmi/lmi_client_shell.py +++ b/cli-tools/lmi/lmi_client_shell.py @@ -14,6 +14,7 @@ # along with this program; if not, see http://www.gnu.org/licenses/.
import os +import abc import new import sys import pywbem @@ -50,13 +51,57 @@ class _LmiTextFormatter(object): if "separator" in kwargs and kwargs["separator"]: f.write("\n")
-class _LmiXmlFormatter(object): - def __init__(self, xml): - self._xml = xml +class _LmiFormatter(object): + __metaclass__ = abc.ABCMeta + + def __init__(self): self._width = int(os.popen('stty size', 'r').read().split()[1]) if not self._width: self._width = 80
+ @abc.abstractmethod + def format(self, indent = 0, sub_indent = 0, width = 80, f = sys.stdout): + pass + + @staticmethod + def __which(program): + is_exe = lambda fpath: os.path.isfile(fpath) and os.access(fpath, os.X_OK) + fpath, fname = os.path.split(program) + if fpath: + if is_exe(program): + return program + else: + for path in os.environ["PATH"].split(os.pathsep): + exe_file = os.path.join(path, program) + if is_exe(exe_file): + return exe_file + return None + + @staticmethod + def __get_pager(): + if "PAGER" in os.environ: + return os.environ["PAGER"] + for p in ["less", "more"]: + path = _LmiFormatter.__which(p) + if path: + return path + + def fancy_format(self, interactive): + if interactive: + tmpfile = tempfile.mkstemp() + f = os.fdopen(tmpfile[0], "w") + self.format(0, 0, self._width, f) + f.close() + subprocess.call([_LmiFormatter.__get_pager(), tmpfile[1]]) + os.remove(tmpfile[1]) + else: + self.format(0, 0, self._width) + +class _LmiXmlFormatter(_LmiFormatter): + def __init__(self, xml): + super(self.__class__, self).__init__() + self._xml = xml + def __format_class_property_content(self, node, indent = 0, sub_indent = 0, width = 80, f = sys.stdout): children = node.childNodes children_len = len(children) @@ -166,44 +211,19 @@ class _LmiXmlFormatter(object): elif isinstance(n, pywbem.cim_xml.PROPERTY_REFERENCE): self.__format_instance_property_reference(n, indent + 4, sub_indent, width, f)
- @staticmethod - def __which(program): - is_exe = lambda fpath: os.path.isfile(fpath) and os.access(fpath, os.X_OK) - fpath, fname = os.path.split(program) - if fpath: - if is_exe(program): - return program - else: - for path in os.environ["PATH"].split(os.pathsep): - exe_file = os.path.join(path, program) - if is_exe(exe_file): - return exe_file - return None - - def __get_pager(self): - if "PAGER" in os.environ: - return os.environ["PAGER"] - for p in ["less", "more"]: - path = self.__which(p) - if path: - return path - def format(self, indent = 0, sub_indent = 0, width = 80, f = sys.stdout): if isinstance(self._xml, pywbem.cim_xml.CLASS): self.__format_class(self._xml, indent, sub_indent, width, f) elif isinstance(self._xml, pywbem.cim_xml.VALUE_NAMEDINSTANCE): self.__format_instance(self._xml, indent, sub_indent, width, f)
- def fancy_format(self, interactive): - if interactive: - tmpfile = tempfile.mkstemp() - f = os.fdopen(tmpfile[0], "w") - self.format(0, 0, self._width, f) - f.close() - subprocess.call([self.__get_pager(), tmpfile[1]]) - os.remove(tmpfile[1]) - else: - self.format(0, 0, self._width) +class _LmiMofFormatter(_LmiFormatter): + def __init__(self, mof): + super(self.__class__, self).__init__() + self._mof = mof + + def format(self, indent = 0, sub_indent = 0, width = 80, f = sys.stdout): + f.write(self._mof)
# Base class for classes, that are used for tab-completion. # Used due to deprecated methods since python 2.2. @@ -554,6 +574,10 @@ class _LmiInstance(BaseObject): _LmiXmlFormatter(self._cim_instance.tocimxml()).fancy_format(self._client.interactive)
@possibly_deleted + def tomof(self): + _LmiMofFormatter(self._cim_instance.tomof()).fancy_format(self._client.interactive) + + @possibly_deleted def associators(self, AssocClass = None, ResultClass = None, Role = None,\ ResultRole = None, Props = None): result = []
cura-tools-devel@lists.fedorahosted.org