basics.py
Go to the documentation of this file.
00001 import string
00002 import yaml
00003 import collections
00004 from lxml import etree
00005 
00006 # Different implementations mix well it seems
00007 # @todo Do not use this?
00008 from xml.etree.ElementTree import ElementTree
00009 
00010 
00011 def xml_string(rootXml, addHeader=True):
00012     # Meh
00013     xmlString = etree.tostring(rootXml, pretty_print=True)
00014     if addHeader:
00015         xmlString = '<?xml version="1.0"?>\n' + xmlString
00016     return xmlString
00017 
00018 
00019 def dict_sub(obj, keys):
00020     return dict((key, obj[key]) for key in keys)
00021 
00022 
00023 def node_add(doc, sub):
00024     if sub is None:
00025         return None
00026     if type(sub) == str:
00027         return etree.SubElement(doc, sub)
00028     elif isinstance(sub, etree._Element):
00029         doc.append(sub)  # This screws up the rest of the tree for prettyprint
00030         return sub
00031     else:
00032         raise Exception('Invalid sub value')
00033 
00034 
00035 def pfloat(x):
00036     return str(x).rstrip('.')
00037 
00038 
00039 def xml_children(node):
00040     children = node.getchildren()
00041 
00042     def predicate(node):
00043         return not isinstance(node, etree._Comment)
00044     return list(filter(predicate, children))
00045 
00046 
00047 def isstring(obj):
00048     try:
00049         return isinstance(obj, basestring)
00050     except NameError:
00051         return isinstance(obj, str)
00052 
00053 
00054 def to_yaml(obj):
00055     """ Simplify yaml representation for pretty printing """
00056     # Is there a better way to do this by adding a representation with
00057     # yaml.Dumper?
00058     # Ordered dict: http://pyyaml.org/ticket/29#comment:11
00059     if obj is None or isstring(obj):
00060         out = str(obj)
00061     elif type(obj) in [int, float, bool]:
00062         return obj
00063     elif hasattr(obj, 'to_yaml'):
00064         out = obj.to_yaml()
00065     elif isinstance(obj, etree._Element):
00066         out = etree.tostring(obj, pretty_print=True)
00067     elif type(obj) == dict:
00068         out = {}
00069         for (var, value) in obj.items():
00070             out[str(var)] = to_yaml(value)
00071     elif hasattr(obj, 'tolist'):
00072         # For numpy objects
00073         out = to_yaml(obj.tolist())
00074     elif isinstance(obj, collections.Iterable):
00075         out = [to_yaml(item) for item in obj]
00076     else:
00077         out = str(obj)
00078     return out
00079 
00080 
00081 class SelectiveReflection(object):
00082     def get_refl_vars(self):
00083         return list(vars(self).keys())
00084 
00085 
00086 class YamlReflection(SelectiveReflection):
00087     def to_yaml(self):
00088         raw = dict((var, getattr(self, var)) for var in self.get_refl_vars())
00089         return to_yaml(raw)
00090 
00091     def __str__(self):
00092         # Good idea? Will it remove other important things?
00093         return yaml.dump(self.to_yaml()).rstrip()


urdfdom_py
Author(s): Thomas Moulard, David Lu, Kelsey Hawkins, Antonio El Khoury, Eric Cousineau, Ioan Sucan , Jackie Kay
autogenerated on Wed Aug 2 2017 02:23:35