rosconfig.py
Go to the documentation of this file.
1 '''
2  Copyright (C) 1997-2017 JDERobot Developers Team
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, see <http://www.gnu.org/licenses/>.
16 
17  Authors : Okan Asik (asik.okan@gmail.com)
18 
19  '''
20 
21 from visualstates.generators.pythonrosgenerator import PythonRosGenerator
22 
23 class RosConfig(object):
24 
25  PUBLISH = 'Publish'
26  SUBSCRIBE = 'Subscribe'
27 
28  def __init__(self):
29  self.topics = []
31  self.runDependencies = []
32 
33  def updateROSConfig(self, newConfig):
34  self.updateTopics(newConfig.topics)
35  self.updateBuildDependencies(newConfig.buildDependencies)
36  self.updateRunDependencies(newConfig.runDependencies)
37 
38  def getTopics(self):
39  return self.topics
40 
41  def isTopicByName(self, topicName):
42  topicNames = map(lambda x: x['name'], self.topics)
43  if topicName in topicNames:
44  return False
45  else:
46  return True
47 
48  def addTopic(self, id, topic):
49  newTopic = {}
50  newTopic['id'] = id
51  if topic['opType'] == RosConfig.PUBLISH:
52  newTopic['methodname'] = topic['methodname']
53  elif topic['opType'] == RosConfig.SUBSCRIBE:
54  newTopic['variablename'] = topic['variablename']
55  newTopic['name'] = topic['name']
56  newTopic['type'] = topic['type']
57  newTopic['opType'] = topic['opType']
58  self.topics.append(newTopic)
59 
60  def updateTopics(self, topics):
61  for topic in topics:
62  if topic not in self.topics:
63  self.addTopic(self.getTopicID(), topic)
64 
65  def getTopicID(self):
66  if self.topics:
67  return max(map(lambda x: x['id'], self.topics)) + 1
68  else:
69  return 0
70 
71  def setBuildDependencies(self, dependencies):
72  """Dependencies coming as strings"""
73  self.buildDependencies = []
74  dependStrs = dependencies.split('\n')
75  for dStr in dependStrs:
76  if len(dStr.strip()) > 0:
77  self.buildDependencies.append(dStr.strip())
78 
80  return self.buildDependencies
81 
82  def updateBuildDependencies(self, dependencies):
83  """Dependencies coming as List"""
84  for dep in dependencies:
85  if dep not in self.buildDependencies:
86  self.buildDependencies.append(dep)
87 
88  def getRunDependencies(self):
89  return self.runDependencies
90 
91  def updateRunDependencies(self, dependencies):
92  """Dependencies coming as List"""
93  for dep in dependencies:
94  if dep not in self.runDependencies:
95  self.runDependencies.append(dep)
96 
97  def createNode(self, doc):
98  cfgElement = doc.createElement('config')
99 
100  bDependencies = doc.createElement('buildDependencies')
101  for bDepend in self.buildDependencies:
102  dependElement = doc.createElement('dependency')
103  dependElement.appendChild(doc.createTextNode(bDepend))
104  bDependencies.appendChild(dependElement)
105  cfgElement.appendChild(bDependencies)
106 
107  rDependencies = doc.createElement('runDependencies')
108  for rDepend in self.runDependencies:
109  dElement = doc.createElement('dependency')
110  dElement.appendChild(doc.createTextNode(rDepend))
111  rDependencies.appendChild(dElement)
112  cfgElement.appendChild(rDependencies)
113 
114  tElements = doc.createElement('topics')
115  for t in self.topics:
116  tElement = doc.createElement('topic')
117  tElement.setAttribute('id', str(t['id']))
118  if t['opType'] == RosConfig.PUBLISH:
119  methodElement = doc.createElement('methodname')
120  methodElement.appendChild(doc.createTextNode(t['methodname']))
121  tElement.appendChild(methodElement)
122  elif t['opType'] == RosConfig.SUBSCRIBE:
123  varElement = doc.createElement('variablename')
124  varElement.appendChild(doc.createTextNode(t['variablename']))
125  tElement.appendChild(varElement)
126  nameElement = doc.createElement('name')
127  nameElement.appendChild(doc.createTextNode(t['name']))
128  tElement.appendChild(nameElement)
129  typeElement = doc.createElement('type')
130  typeElement.appendChild(doc.createTextNode(t['type']))
131  tElement.appendChild(typeElement)
132  opElement = doc.createElement('opType')
133  opElement.appendChild(doc.createTextNode(t['opType']))
134  tElement.appendChild(opElement)
135 
136  tElements.appendChild(tElement)
137 
138  cfgElement.appendChild(tElements)
139 
140  return cfgElement
141 
142  def loadNode(self, node):
143  self.buildDependencies = []
144  bDependencies = node.getElementsByTagName('buildDependencies')[0]
145  for bDependency in bDependencies.getElementsByTagName('dependency'):
146  if len(bDependency.childNodes) > 0:
147  self.buildDependencies.append(bDependency.childNodes[0].nodeValue)
148  # print('bdepend:' + bDependency.childNodes[0].nodeValue)
149 
150  self.runDependencies = []
151  rDependencies = node.getElementsByTagName('runDependencies')[0]
152  for rDependency in rDependencies.getElementsByTagName('dependency'):
153  if len(rDependency.childNodes) > 0:
154  self.runDependencies.append(rDependency.childNodes[0].nodeValue)
155  # print('rdepend:' + rDependency.childNodes[0].nodeValue)
156 
157  self.topics = []
158  tElements = node.getElementsByTagName('topics')[0]
159  for t in tElements.getElementsByTagName('topic'):
160  topic = {}
161  topic['id'] = int(t.getAttribute('id'))
162  topic['name'] = t.getElementsByTagName('name')[0].childNodes[0].nodeValue
163  topic['type'] = t.getElementsByTagName('type')[0].childNodes[0].nodeValue
164  topic['opType'] = t.getElementsByTagName('opType')[0].childNodes[0].nodeValue
165  if topic['opType'] == RosConfig.PUBLISH:
166  methodnames = t.getElementsByTagName('methodname')
167  if len(methodnames) > 0:
168  topic['methodname'] = methodnames[0].childNodes[0].nodeValue
169  else:
170  topic['methodname'] = self.getVarName(topic['name'])
171  elif topic['opType'] == RosConfig.SUBSCRIBE:
172  varnames = t.getElementsByTagName('variablename')
173  if len(varnames) > 0:
174  topic['variablename'] = varnames[0].childNodes[0].nodeValue
175  else:
176  topic['variablename'] = self.getVarName(topic['name'])
177 
178  self.topics.append(topic)
179 
180  def getVarName(self, varName):
181  varName = varName.replace('/', '_')
182  if varName[0] == '_':
183  varName = varName[1:]
184  return varName
def updateRunDependencies(self, dependencies)
Definition: rosconfig.py:91
def setBuildDependencies(self, dependencies)
Definition: rosconfig.py:71
def updateBuildDependencies(self, dependencies)
Definition: rosconfig.py:82


visualstates
Author(s):
autogenerated on Thu Apr 1 2021 02:42:20