rviz_config.py
Go to the documentation of this file.
1 import sys
2 import yaml
3 import ruamel.yaml # For custom yaml dumping
4 
5 
6 my_yaml_writer = ruamel.yaml.YAML()
7 my_yaml_writer.indent(mapping=2, sequence=4, offset=2)
8 my_yaml_writer.representer.add_representer(type(None),
9  lambda self, data:
10  self.represent_scalar('tag:yaml.org,2002:null', '~')
11  )
12 
13 if sys.version_info[0] == 2:
14  # If this is running Python2, we need to manually sort the dictionaries
15  def sorted_dict_representer(self, data):
16  value = []
17  node = ruamel.yaml.nodes.MappingNode('tag:yaml.org,2002:map', value)
18  for item_key, item_value in sorted(data.items()):
19  node_key = self.represent_key(item_key)
20  node_value = self.represent_data(item_value)
21  value.append((node_key, node_value))
22  return node
23 
24  my_yaml_writer.representer.add_representer(dict, sorted_dict_representer)
25 
26 
27 def get_class_dicts(entry):
28  classes = []
29  if isinstance(entry, list):
30  for sub in entry:
31  classes += get_class_dicts(sub)
32  elif isinstance(entry, dict):
33  if entry.get('Class'):
34  classes.append(entry)
35  for k, v in entry.items():
36  classes += get_class_dicts(v)
37  return classes
38 
39 
40 def dictionary_subtract(alpha, beta):
41  changed = False
42  for k in beta.keys():
43  if k not in alpha:
44  continue
45  v = alpha[k]
46  if isinstance(v, dict):
47  changed |= dictionary_subtract(v, beta[k])
48  if not v:
49  del alpha[k]
50  changed = True
51  elif v == beta[k]:
52  del alpha[k]
53  changed = True
54  return changed
55 
56 
57 class RVizConfig:
58  def __init__(self, rel_fn, path):
59  self.rel_fn = rel_fn
60  self.path = path
61  self.contents = yaml.safe_load(open(path))
62  self.changed = False
63 
64  def get_class_dicts(self):
65  return get_class_dicts(self.contents)
66 
67  def get_dependencies(self):
68  packages = set()
69  for config in self.get_class_dicts():
70  value = config['Class'].split('/')[0]
71  packages.add(value)
72  return packages
73 
74  def write(self):
75  if not self.changed:
76  return
77  with open(self.path, 'w') as f:
78  my_yaml_writer.dump(self.contents, f, transform=lambda s: s.replace(": ''\n", ': ""\n'))
def sorted_dict_representer(self, data)
Definition: rviz_config.py:15
def __init__(self, rel_fn, path)
Definition: rviz_config.py:58
def dictionary_subtract(alpha, beta)
Definition: rviz_config.py:40


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