plugin_xml.py
Go to the documentation of this file.
00001 from xml.dom.minidom import parse
00002 from collections import OrderedDict
00003 import os.path
00004 
00005 NS_PATTERN = '%s::%s'
00006 
00007 
00008 class PluginXML:
00009     def __init__(self, rel_fn, file_path):
00010         self.rel_fn = rel_fn
00011         self.file_path = file_path
00012         self.has_class_libraries_tag = False
00013         self.libraries = OrderedDict()
00014         self.parent_pkgs = set()
00015         self.changed = False
00016 
00017         if os.path.exists(self.file_path):
00018             self.read()
00019 
00020     def read(self):
00021         tree = parse(self.file_path)
00022 
00023         self.has_class_libraries_tag = len(tree.getElementsByTagName('class_libraries')) > 0
00024 
00025         for el in tree.getElementsByTagName('library'):
00026             path = el.getAttribute('path').replace('lib/lib', '')
00027             cls = OrderedDict()
00028             self.libraries[path] = cls
00029 
00030             for clstag in el.getElementsByTagName('class'):
00031                 d = {}
00032                 d['base_class_type'] = clstag.getAttribute('base_class_type')
00033                 self.parent_pkgs.add(d['base_class_type'].split('::')[0])
00034                 d['type'] = clstag.getAttribute('type')
00035                 d['name'] = clstag.getAttribute('name')
00036 
00037                 desc = ''
00038                 for tag in clstag.getElementsByTagName('description'):
00039                     if len(tag.childNodes) == 0:
00040                         continue
00041                     desc += str(tag.childNodes[0].nodeValue)
00042                 d['description'] = desc
00043 
00044                 cls[d['type']] = d
00045 
00046     def contains_library(self, library_name, pkg, name):
00047         if library_name not in self.libraries:
00048             return False
00049         full_name = NS_PATTERN % (pkg, name)
00050         return full_name in self.libraries[library_name]
00051 
00052     def insert_new_class(self, library_name, pkg, name, base_pkg, base_name, description=''):
00053         if library_name not in self.libraries:
00054             self.libraries[library_name] = OrderedDict()
00055         library = self.libraries[library_name]
00056         full_name = NS_PATTERN % (pkg, name)
00057         library[full_name] = {'base_class_type': NS_PATTERN % (base_pkg, base_name),
00058                               'type': full_name,
00059                               'description': description}
00060         self.changed = True
00061 
00062     def write(self):
00063         if not self.changed:
00064             return
00065         with open(self.file_path, 'w') as f:
00066             f.write(str(self))
00067 
00068     def __repr__(self):
00069         s = ''
00070         indent = 0
00071         need_class_libraries_tag = len(self.libraries) > 1 or self.has_class_libraries_tag
00072         if need_class_libraries_tag:
00073             s += '<class_libraries>\n'
00074             indent += 2
00075 
00076         for name, lib in self.libraries.iteritems():
00077             s += ' ' * indent + '<library path="lib/lib%s">\n' % name
00078             for t, clib in lib.iteritems():
00079                 s += self.class_str(clib, indent + 2)
00080             s += ' ' * indent + '</library>\n'
00081 
00082         if need_class_libraries_tag:
00083             s += '</class_libraries>\n'
00084         return s
00085 
00086     def class_str(self, lib, indent):
00087         s = ' ' * indent + '<class'
00088         for at in ['name', 'type', 'base_class_type']:
00089             if at in lib and len(lib[at]) > 0:
00090                 s += ' %s="%s"' % (at, lib[at])
00091         s += '>\n' + (' ' * (indent + 2))
00092         s += '<description>%s</description>' % lib.get('description', '')
00093 
00094         s += '\n' + (' ' * indent) + '</class>\n'
00095         return s


ros_introspection
Author(s):
autogenerated on Wed Jun 19 2019 19:21:34