plugin_xml.py
Go to the documentation of this file.
1 from xml.dom.minidom import parse
2 from collections import OrderedDict
3 import os.path
4 
5 NS_PATTERN = '%s::%s'
6 
7 
8 class PluginXML:
9  def __init__(self, rel_fn, file_path):
10  self.rel_fn = rel_fn
11  self.file_path = file_path
13  self.libraries = OrderedDict()
14  self.parent_pkgs = set()
15  self.changed = False
16 
17  if os.path.exists(self.file_path):
18  self.read()
19 
20  def read(self):
21  tree = parse(self.file_path)
22 
23  self.has_class_libraries_tag = len(tree.getElementsByTagName('class_libraries')) > 0
24 
25  for el in tree.getElementsByTagName('library'):
26  path = el.getAttribute('path').replace('lib/lib', '')
27  cls = OrderedDict()
28  self.libraries[path] = cls
29 
30  for clstag in el.getElementsByTagName('class'):
31  d = {}
32  d['base_class_type'] = clstag.getAttribute('base_class_type')
33  self.parent_pkgs.add(d['base_class_type'].split('::')[0])
34  d['type'] = clstag.getAttribute('type')
35  d['name'] = clstag.getAttribute('name')
36 
37  desc = ''
38  for tag in clstag.getElementsByTagName('description'):
39  if len(tag.childNodes) == 0:
40  continue
41  desc += str(tag.childNodes[0].nodeValue)
42  d['description'] = desc
43 
44  cls[d['type']] = d
45 
46  def contains_library(self, library_name, pkg, name):
47  if library_name not in self.libraries:
48  return False
49  full_name = NS_PATTERN % (pkg, name)
50  return full_name in self.libraries[library_name]
51 
52  def insert_new_class(self, library_name, pkg, name, base_pkg, base_name, description=''):
53  if library_name not in self.libraries:
54  self.libraries[library_name] = OrderedDict()
55  library = self.libraries[library_name]
56  full_name = NS_PATTERN % (pkg, name)
57  library[full_name] = {'base_class_type': NS_PATTERN % (base_pkg, base_name),
58  'type': full_name,
59  'description': description}
60  self.changed = True
61 
62  def write(self):
63  if not self.changed:
64  return
65  with open(self.file_path, 'w') as f:
66  f.write(str(self))
67 
68  def __repr__(self):
69  s = ''
70  indent = 0
71  need_class_libraries_tag = len(self.libraries) > 1 or self.has_class_libraries_tag
72  if need_class_libraries_tag:
73  s += '<class_libraries>\n'
74  indent += 2
75 
76  for name, lib in self.libraries.iteritems():
77  s += ' ' * indent + '<library path="lib/lib%s">\n' % name
78  for t, clib in lib.iteritems():
79  s += self.class_str(clib, indent + 2)
80  s += ' ' * indent + '</library>\n'
81 
82  if need_class_libraries_tag:
83  s += '</class_libraries>\n'
84  return s
85 
86  def class_str(self, lib, indent):
87  s = ' ' * indent + '<class'
88  for at in ['name', 'type', 'base_class_type']:
89  if at in lib and len(lib[at]) > 0:
90  s += ' %s="%s"' % (at, lib[at])
91  s += '>\n' + (' ' * (indent + 2))
92  s += '<description>%s</description>' % lib.get('description', '')
93 
94  s += '\n' + (' ' * indent) + '</class>\n'
95  return s
def __init__(self, rel_fn, file_path)
Definition: plugin_xml.py:9
def insert_new_class(self, library_name, pkg, name, base_pkg, base_name, description='')
Definition: plugin_xml.py:52
def contains_library(self, library_name, pkg, name)
Definition: plugin_xml.py:46
def class_str(self, lib, indent)
Definition: plugin_xml.py:86


ros_introspection
Author(s):
autogenerated on Wed Jun 19 2019 19:56:52