ros_generator.py
Go to the documentation of this file.
1 import os.path
2 import re
3 
4 AT_LEAST_THREE_DASHES = re.compile('^\-{3,}\r?$')
5 FIELD_LINE = re.compile('([\w_/]+)(\[\d*\])?\s+([\w_]+)\s*(=.*)?(\s*\#.*)?$', re.DOTALL)
6 
7 
9  def __init__(self, type, is_array, name, value):
10  self.type = type
11  self.is_array = is_array
12  self.name = name
13  self.value = value
14 
15  def __repr__(self):
16  s = self.type
17  if self.is_array:
18  s += '[]'
19  s += ' '
20  s += self.name
21  if self.value:
22  s += '='
23  s += self.value
24  return s
25 
26 
28  def __init__(self):
29  self.contents = []
30  self.fields = []
31 
32  def add_line(self, line):
33  stripped = line.strip()
34  if not stripped or stripped[0] == '#':
35  self.contents.append(line)
36  return
37  m = FIELD_LINE.match(line)
38  if m:
39  type, is_array, name, value, comment = m.groups()
40  field = GeneratorField(type, is_array, name, value)
41  self.contents.append(field)
42  self.fields.append(field)
43  if comment:
44  self.contents.append(comment)
45  else:
46  self.contents.append('\n')
47  else:
48  raise Exception('Unable to parse generator line: ' + repr(line))
49 
50  def __repr__(self):
51  return ''.join(map(str, self.contents))
52 
53 
55  def __init__(self, rel_fn, file_path):
56  self.file_path = file_path
57  parts = os.path.splitext(rel_fn)
58  self.base_name = os.path.split(parts[0])[-1]
59  self.type = parts[-1][1:] # Just the extension, no dot
60  self.name = os.path.basename(rel_fn)
61  self.changed = False
63 
64  self.dependencies = set()
65 
66  with open(file_path) as f:
67  for line in f:
68  if AT_LEAST_THREE_DASHES.match(line):
69  self.sections.append(GeneratorSection())
70  continue
71  else:
72  self.sections[-1].add_line(line)
73 
74  for section in self.sections:
75  for field in section.fields:
76  if '/' not in field.type:
77  continue
78  package, part = field.type.split('/')
79  if package != self.name:
80  self.dependencies.add(package)
81 
82  if self.type == 'action':
83  self.dependencies.add('actionlib_msgs')
84 
85  def output(self):
86  return '---\n'.join(map(str, self.sections))
87 
88  def write(self):
89  if not self.changed:
90  return
91  with open(self.file_path, 'w') as f:
92  f.write(self.output())
93 
94  def __repr__(self):
95  return self.name
def __init__(self, type, is_array, name, value)
Definition: ros_generator.py:9
def __init__(self, rel_fn, file_path)


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