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


ros_introspection
Author(s):
autogenerated on Wed Jun 22 2022 02:45:33