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.file_path = file_path
59  parts = os.path.splitext(rel_fn)
60  self.base_name = os.path.split(parts[0])[-1]
61  self.type = parts[-1][1:] # Just the extension, no dot
62  self.name = os.path.basename(rel_fn)
63  self.changed = False
65 
66  self.dependencies = set()
67 
68  with open(file_path) as f:
69  for line in f:
70  if AT_LEAST_THREE_DASHES.match(line):
71  self.sections.append(GeneratorSection())
72  continue
73  else:
74  self.sections[-1].add_line(line)
75 
76  for section in self.sections:
77  for field in section.fields:
78  if '/' not in field.type:
79  continue
80  package, part = field.type.split('/')
81  if package != self.name:
82  self.dependencies.add(package)
83 
84  if self.type == 'action':
85  self.dependencies.add('actionlib_msgs')
86 
87  def output(self):
88  return '---\n'.join(map(str, self.sections))
89 
90  def write(self):
91  if not self.changed:
92  return
93  with open(self.file_path, 'w') as f:
94  f.write(self.output())
95 
96  def __repr__(self):
97  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 Mar 3 2021 03:56:00