Repository :
http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
---------------------------------------------------------------
commit 5fceaa081505f2b043515c5974750434c84348bc
Author: Peter Hatina <phatina(a)redhat.com>
Date: Thu Nov 29 11:14:42 2012 +0100
switch from file descriptors to files in _LmiTextFormater
---------------------------------------------------------------
cli-tools/lmi/lmi_client_shell.py | 101 +++++++++++++++++++------------------
1 files changed, 51 insertions(+), 50 deletions(-)
diff --git a/cli-tools/lmi/lmi_client_shell.py b/cli-tools/lmi/lmi_client_shell.py
index b92bc25..4c78030 100644
--- a/cli-tools/lmi/lmi_client_shell.py
+++ b/cli-tools/lmi/lmi_client_shell.py
@@ -49,16 +49,16 @@ class _LmiTextFormatter(object):
def __init__(self, text):
self._text = text
- def format(self, indent = 0, sub_indent = 0, width = 80, fd = sys.stdout.fileno(),
**kwargs):
+ def format(self, indent = 0, sub_indent = 0, width = 80, f = sys.stdout, **kwargs):
if indent > width:
return # NOTE: this is wrong!
wrapper = textwrap.TextWrapper()
wrapper.width = width - indent
wrapper.subsequent_indent = " " * sub_indent
for l in wrapper.wrap(self._text):
- os.write(fd, "%s%s\n" % (" " * indent, l))
+ f.write("%s%s\n" % (" " * indent, l))
if "separator" in kwargs and kwargs["separator"]:
- os.write(fd, "\n")
+ f.write("\n")
class _LmiXmlFormatter(object):
def __init__(self, xml):
@@ -67,23 +67,23 @@ class _LmiXmlFormatter(object):
if not self._width:
self._width = 80
- def __format_class_property_content(self, node, indent = 0, sub_indent = 0, width =
80, fd = sys.stdout.fileno()):
+ def __format_class_property_content(self, node, indent = 0, sub_indent = 0, width =
80, f = sys.stdout):
children = node.childNodes
children_len = len(children)
for (i, n) in enumerate(children):
if isinstance(n, pywbem.cim_xml.QUALIFIER):
- self.__format_qualifier(n, indent, sub_indent, width, fd)
+ self.__format_qualifier(n, indent, sub_indent, width, f)
elif isinstance(n, pywbem.cim_xml.PROPERTY):
- self.__format_class_property(n, indent, sub_indent, width, fd)
+ self.__format_class_property(n, indent, sub_indent, width, f)
elif isinstance(n, pywbem.cim_xml.PROPERTY_ARRAY):
- self.__format_class_property_array(n, indent, sub_indent, width, fd)
+ self.__format_class_property_array(n, indent, sub_indent, width, f)
elif isinstance(n, pywbem.cim_xml.METHOD):
- self.__format_method(n, indent, sub_indent, width, fd)
+ self.__format_method(n, indent, sub_indent, width, f)
elif isinstance(n, pywbem.cim_xml.VALUE):
# NOTE: we skip default value for the class
pass
- def __format_qualifier(self, node, indent = 0, sub_indent = 0, width = 80, fd =
sys.stdout.fileno()):
+ def __format_qualifier(self, node, indent = 0, sub_indent = 0, width = 80, f =
sys.stdout):
is_array = isinstance(node.firstChild, pywbem.cim_xml.VALUE_ARRAY)
val = "[qualifier] %s%s %s" % (node.getAttribute("TYPE"),
" []" if is_array else "",
node.getAttribute("NAME"))
@@ -92,41 +92,41 @@ class _LmiXmlFormatter(object):
val += ": { " + ", ".join(["'" +
v.firstChild.nodeValue + "'" for v in values[0].childNodes]) + "
}\n"
else:
val += ": '%s'\n" % values[0].firstChild.nodeValue
- _LmiTextFormatter(val).format(indent, sub_indent + 4, width, fd, separator =
True)
+ _LmiTextFormatter(val).format(indent, sub_indent + 4, width, f, separator =
True)
- def __format_class_property(self, node, indent = 0, sub_indent = 0, width = 80, fd =
sys.stdout.fileno()):
+ def __format_class_property(self, node, indent = 0, sub_indent = 0, width = 80, f =
sys.stdout):
val = "[property] %s %s" % (node.getAttribute("TYPE"),
node.getAttribute("NAME"))
- _LmiTextFormatter(val).format(indent, sub_indent + 4, width, fd, separator =
True)
- self.__format_class_property_content(node, indent + 4, sub_indent, width, fd)
+ _LmiTextFormatter(val).format(indent, sub_indent + 4, width, f, separator =
True)
+ self.__format_class_property_content(node, indent + 4, sub_indent, width, f)
- def __format_class_property_array(self, node, indent = 0, sub_indent = 0, width = 80,
fd = sys.stdout.fileno()):
+ def __format_class_property_array(self, node, indent = 0, sub_indent = 0, width = 80,
f = sys.stdout):
val = "[property array] %s [] %s" %
(node.getAttribute("TYPE"), node.getAttribute("TYPE"))
- _LmiTextFormatter(val).format(indent, sub_indent + 4, width, fd, separator =
True)
- self.__format_class_property_content(node, indent + 4, sub_indent, width, fd)
+ _LmiTextFormatter(val).format(indent, sub_indent + 4, width, f, separator =
True)
+ self.__format_class_property_content(node, indent + 4, sub_indent, width, f)
- def __format_class_property_reference(self, node, indent = 0, sub_indent = 0, width =
80, fd = sys.stdout.fileno()):
+ def __format_class_property_reference(self, node, indent = 0, sub_indent = 0, width =
80, f = sys.stdout):
val = "[property ref] %s %s" %
(node.getAttribute("REFERENCECLASS"), node.getAttribute("NAME"))
- _LmiTextFormatter(val).format(indent, sub_indent + 4, width, fd, separator =
True)
- self.__format_class_property_content(node, indent + 4, sub_indent, width, fd)
+ _LmiTextFormatter(val).format(indent, sub_indent + 4, width, f, separator =
True)
+ self.__format_class_property_content(node, indent + 4, sub_indent, width, f)
- def __format_instance_property(self, node, indent = 0, sub_indent = 0, width = 80, fd
= sys.stdout.fileno()):
+ def __format_instance_property(self, node, indent = 0, sub_indent = 0, width = 80, f
= sys.stdout):
val = node.firstChild
prop = "[property] %s %s%s" % (node.getAttribute("TYPE"),
node.getAttribute("NAME"),
" = '%s'" % val.firstChild.nodeValue if val else
"")
- _LmiTextFormatter(prop).format(indent, sub_indent + 4, width, fd, separator =
True)
+ _LmiTextFormatter(prop).format(indent, sub_indent + 4, width, f, separator =
True)
- def __format_instance_property_array(self, node, indent = 0, sub_indent = 0, width =
80, fd = sys.stdout.fileno()):
+ def __format_instance_property_array(self, node, indent = 0, sub_indent = 0, width =
80, f = sys.stdout):
prop = "[property array] %s [] %s" %
(node.getAttribute("TYPE"), node.getAttribute("NAME"))
val = node.firstChild
if val:
prop += " = { " + ", ".join(["'" +
v.firstChild.nodeValue + "'" for v in val.childNodes]) + " }"
- _LmiTextFormatter(prop).format(indent, sub_indent + 4, width, fd, separator =
True)
+ _LmiTextFormatter(prop).format(indent, sub_indent + 4, width, f, separator =
True)
- def __format_instance_property_reference(self, node, indent = 0, sub_indent = 0,
width = 80, fd = sys.stdout.fileno()):
+ def __format_instance_property_reference(self, node, indent = 0, sub_indent = 0,
width = 80, f = sys.stdout):
val = "[property ref] %s %s" %
(node.getAttribute("REFERENCECLASS"), node.getAttribute("NAME"))
- _LmiTextFormatter(val).format(indent, sub_indent + 4, width, fd, separator =
True)
+ _LmiTextFormatter(val).format(indent, sub_indent + 4, width, f, separator =
True)
- def __format_method(self, node, indent = 0, sub_indent = 0, width = 80, fd =
sys.stdout.fileno()):
+ def __format_method(self, node, indent = 0, sub_indent = 0, width = 80, f =
sys.stdout):
qualifiers = [x for x in node.childNodes if isinstance(x,
pywbem.cim_xml.QUALIFIER)]
parameters = [x for x in node.childNodes if isinstance(x,
pywbem.cim_xml.PARAMETER)]
parameters_arr = [x for x in node.childNodes if isinstance(x,
pywbem.cim_xml.PARAMETER_ARRAY)]
@@ -134,47 +134,47 @@ class _LmiXmlFormatter(object):
parameters_ref_arr = [x for x in node.childNodes if isinstance(x,
pywbem.cim_xml.PARAMETER_REFARRAY)]
has_args = parameters != [] or parameters_arr != [] or parameters_ref != [] or
parameters_ref_arr != []
val = "[method] %s %s%s" % (node.getAttribute("TYPE"),
node.getAttribute("NAME"), "(...)" if has_args else "()")
- _LmiTextFormatter(val).format(indent, sub_indent, width, fd, separator = True)
+ _LmiTextFormatter(val).format(indent, sub_indent, width, f, separator = True)
for q in qualifiers:
- self.__format_qualifier(q, indent + 4, sub_indent + 4, width, fd)
+ self.__format_qualifier(q, indent + 4, sub_indent + 4, width, f)
for p in parameters:
val = "[param] %s %s" % (p.getAttribute("TYPE"),
p.getAttribute("NAME"))
- _LmiTextFormatter(val).format(indent + 4, sub_indent + 4, width, fd,
separator = True)
+ _LmiTextFormatter(val).format(indent + 4, sub_indent + 4, width, f, separator
= True)
for p in parameters_arr:
val = "[param array] %s (ref) %s" %
(p.getAttribute("TYPE"), p.getAttribute("NAME"))
- _LmiTextFormatter(val).format(indent + 4, sub_indent + 4, width, fd,
separator = True)
+ _LmiTextFormatter(val).format(indent + 4, sub_indent + 4, width, f, separator
= True)
for p in parameters_ref:
val = "[param ref] %s (ref) %s" %
(p.getAttribute("REFERENCECLASS"), p.getAttribute("NAME"))
- _LmiTextFormatter(val).format(indent + 4, sub_indent + 4, width, fd,
separator = True)
+ _LmiTextFormatter(val).format(indent + 4, sub_indent + 4, width, f, separator
= True)
for p in parameters_ref_arr:
val = "[param ref array] %s (ref) %s" %
(p.getAttribute("REFERENCECLASS"), p.getAttribute("NAME"))
- _LmiTextFormatter(val).format(indent + 4, sub_indent + 4, width, fd,
separator = True)
+ _LmiTextFormatter(val).format(indent + 4, sub_indent + 4, width, f, separator
= True)
- def __format_class(self, node, indent = 0, sub_indent = 0, width = 80, fd =
sys.stdout.fileno()):
- _LmiTextFormatter("Class: %s" %
node.getAttribute("NAME")).format(indent, sub_indent, width, fd)
- _LmiTextFormatter("SuperClass: %s" %
node.getAttribute("SUPERCLASS")).format(indent + 4, sub_indent, width, fd)
+ def __format_class(self, node, indent = 0, sub_indent = 0, width = 80, f =
sys.stdout):
+ _LmiTextFormatter("Class: %s" %
node.getAttribute("NAME")).format(indent, sub_indent, width, f)
+ _LmiTextFormatter("SuperClass: %s" %
node.getAttribute("SUPERCLASS")).format(indent + 4, sub_indent, width, f)
for n in node.childNodes:
if isinstance(n, pywbem.cim_xml.QUALIFIER):
- self.__format_qualifier(n, indent + 4, sub_indent, width, fd)
+ self.__format_qualifier(n, indent + 4, sub_indent, width, f)
elif isinstance(n, pywbem.cim_xml.PROPERTY):
- self.__format_class_property(n, indent + 4, sub_indent, width, fd)
+ self.__format_class_property(n, indent + 4, sub_indent, width, f)
elif isinstance(n, pywbem.cim_xml.PROPERTY_ARRAY):
- self.__format_class_property_array(n, indent + 4, sub_indent, width, fd)
+ self.__format_class_property_array(n, indent + 4, sub_indent, width, f)
elif isinstance(n, pywbem.cim_xml.PROPERTY_REFERENCE):
- self.__format_class_property_reference(n, indent + 4, sub_indent, width,
fd)
+ self.__format_class_property_reference(n, indent + 4, sub_indent, width,
f)
elif isinstance(n, pywbem.cim_xml.METHOD):
- self.__format_method(n, indent + 4, sub_indent, width, fd)
+ self.__format_method(n, indent + 4, sub_indent, width, f)
- def __format_instance(self, node, indent = 0, sub_indent = 0, width = 80, fd =
sys.stdout.fileno()):
+ def __format_instance(self, node, indent = 0, sub_indent = 0, width = 80, f =
sys.stdout):
instance = node.firstChild.nextSibling
- _LmiTextFormatter("Instance of %s" %
instance.getAttribute("CLASSNAME")).format(indent, sub_indent, width, fd)
+ _LmiTextFormatter("Instance of %s" %
instance.getAttribute("CLASSNAME")).format(indent, sub_indent, width, f)
for n in instance.childNodes:
if isinstance(n, pywbem.cim_xml.PROPERTY):
- self.__format_instance_property(n, indent + 4, sub_indent, width, fd)
+ self.__format_instance_property(n, indent + 4, sub_indent, width, f)
elif isinstance(n, pywbem.cim_xml.PROPERTY_ARRAY):
- self.__format_instance_property_array(n, indent + 4, sub_indent, width,
fd)
+ self.__format_instance_property_array(n, indent + 4, sub_indent, width,
f)
elif isinstance(n, pywbem.cim_xml.PROPERTY_REFERENCE):
- self.__format_instance_property_reference(n, indent + 4, sub_indent,
width, fd)
+ self.__format_instance_property_reference(n, indent + 4, sub_indent,
width, f)
@staticmethod
def __which(program):
@@ -198,17 +198,18 @@ class _LmiXmlFormatter(object):
if path:
return path
- def format(self, indent = 0, sub_indent = 0, width = 80, fd = sys.stdout.fileno()):
+ 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, fd)
+ 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, fd)
+ self.__format_instance(self._xml, indent, sub_indent, width, f)
def fancy_format(self, interactive):
if interactive:
tmpfile = tempfile.mkstemp()
- self.format(0, 0, self._width, tmpfile[0])
- os.close(tmpfile[0])
+ 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: