File: clamrefactor.py

package info (click to toggle)
clam 1.4.0-5.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,780 kB
  • sloc: cpp: 92,499; python: 9,721; ansic: 1,602; xml: 444; sh: 239; makefile: 153; perl: 54; asm: 15
file content (375 lines) | stat: -rwxr-xr-x 13,429 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/python

# This script can be used, as a command line tool or as a python module,
# to performs a set of high level transformations on CLAM networks.
#
# The script is useful to upgrade the network to changes on C++
# code and also to perform automatized parameter changes from the
# commandline (not using the NetworkEditor).
#

# DONE: Basic operaions
# DONE: Making it easy to use on command line, not just as library
# DONE: Reading 'migration scripts' with a set of those commands
# DONE: Add CLAM version information into the networks and check it.
# TODO: More operators: reorderConfig, renameProcessingInstance...
# TODO: Being able to update also ui files


from xml.etree import ElementTree, ElementPath
import xml.dom
import sys
import re
import copy

class NotACommand(Exception) :
	def __init__(self, command, available) :
		self.command = command
		self.available = available
	def __str__(self) : 
		return "'%s' is not an available command. Try with: %s"%(
			self.command, ", ".join(self.available))

class ClamNetwork() :

	def __init__ (self, networkfile) :
		self.document = ElementTree.parse(networkfile)
		self.processings = self.document.findall("processing")
		self.connections = dict(
			incontrol = self.document.findall("control_connection/in"),
			outcontrol =  self.document.findall("control_connection/out"),
			inport = self.document.findall("port_connection/in"),
			outport = self.document.findall("port_connection/out"),
			)
		self.version = self.document.find("/").attrib.get("clamVersion","1.3.0")
		self.log = []
		self.verbose = False
		self.ensuredVersion = ""
		self.modified = False

	def _featuredVersion(self, fullVersion) :
		feature = ".".join(fullVersion.split(".")[:2])
	def runCommand(self, command) :
		import shlex
		tokens = shlex.split(command)
		try: method=getattr(self, tokens[0])
		except AttributeError:
			raise NotACommand(tokens[0], self._availableCommands())
		method(*tokens[1:])

	def _log(self, message) :
		if self.verbose : print >> sys.stderr,message
		self.modified = True
		self.log.append(message)
	def _versionNotApplies(self) :
		if not self.ensuredVersion : return False
		featuredVersion = self._featuredVersion(self.version)
		if featuredVersion == self._featuredVersion(self.ensuredVersion) :
			return False
		return True
	def _namesForType(self, type) :
		return [ processing.get("id")
			for processing in self.processings
			if processing.get('type') == type ]

	def _processingsOfType(self, type) :
		for processing in self.processings :
			if processing.get('type') == type :
				yield processing

	def _processingOfId(self, id) :
		for processing in self.processings :
			if processing.get('id') == id :
				return processing
		return None
	def _availableCommands(self) :
		return [ method for method in dir(self)
			if method[0] != '_' and callable(getattr(self,method)) ]

	def beVerbose(self, shouldBe=True) :
		self.verbose = shouldBe
	def dump(self, file=sys.stdout) :
		self.document.write(file)
	def dumpLog(self, file=sys.stdout) :
		print >> file, "\n".join(self.log)

	def ensureVersion(self, versionString) :
		'''Makes the next commands apply just if the network version
		versionString'. Use 'any' to remove the restriction'''
		if versionString.lower() is "any" : versionString = ''
		self.ensuredVersion = versionString
		
	def upgrade(self, version) :
		if self._versionNotApplies() : return
		self.document.find('/').attrib['clamVersion'] = version
		self.version = version

	def renameClass(self, oldType, newType) :
		if self._versionNotApplies() : return
		for processing in self._processingsOfType(oldType) : 
			processing.set('type', newType)
			self._log("Retyping processing %s: %s -> %s" %(
				processing.get("id"), oldType, newType))

	def renameConnector(self, processingType, connectorKind, oldName, newName) :
		if self._versionNotApplies() : return
		names = self._namesForType(processingType)
		for connection in self.connections[connectorKind] :
			processing, connector = connection.text.split(".")
			if processing not in names: continue
			if connector != oldName: continue
			connection.text = ".".join([processing, newName])
			self._log("Updating %s: %s.%s -> %s.%s" %(
				connectorKind, processing, oldName, processing, newName))

	def renameConnectorRegExp(self, processingType, connectorKind, oldName, newNameExpression) :
		if self._versionNotApplies() : return
		names = self._namesForType(processingType)
		regexpr = re.compile(oldName)
		for connection in self.connections[connectorKind] :
			processing, connector = connection.text.split(".")
			if processing not in names: continue
			match = regexpr.match(connector)
			if not match : continue
			newName= regexpr.sub(newNameExpression, connector)
			connection.text = ".".join([processing, newName])
			self._log("Updating %s: %s.%s -> %s.%s" %(
				connectorKind, processing, connector, processing, newName))
	
	def _removeConnections(self, processingName):
		connections = dict(outport=[],)
		for connection in self.connections['outport']:
			if connection.text.split('.')[0] != processingName:
				connections['outport'].append(connection)
		self.connections['outport'] = connections['outport']
	
	def _addConnections(self, fromProcessing, fromNr):
		out = ElementTree.Element('out')
		out.text = "%s.%s" % (fromProcessing, fromNr)
		self.connections['outport'] .append(out)

	def _findLastTag(self, tag):
		order = [
			'description',
			'processing',
			'port_connection',
			'control_connection',
			]
		children = self.document.getroot().getchildren()
		tagOrder = order.index(tag)
		for i, child in enumerate(children):
			if order.index(child.tag) > tagOrder: return i
		return len(children)

	def removeOutConnections(self, connectionType, processingName):
		self._removeConnections(processingName) # keep internal data structure intact
		network = self.document.getroot()
		connections = network.findall(connectionType)
		for connection in connections:
			m = re.search(processingName, "%s" % connection.find('out').text)
			if m:
				network.remove(connection)
				self._log("Removed %s from %s" %(m.group(0), connectionType))	
			
	def  addConnection(self, connectionType, fromProcessing, fromNr, toProcessing, toNr):
		self._addConnections(fromProcessing, fromNr) # keep internal data structure intact
		
		index = self._findLastTag(connectionType)
				
		network = self.document.getroot()
		connection = ElementTree.Element(connectionType)
		out = ElementTree.SubElement(connection, "out")
		out.text = "%s.%s" % (fromProcessing, fromNr)
		in_ = ElementTree.SubElement(connection, "in")
		in_.text = "%s.%s" % (toProcessing, toNr)
		
		network.insert(index, connection)
		self._log("Added %s.%s -> %s.%s in %s" %(fromProcessing, fromNr, \
			toProcessing, toNr, connectionType))	
	
	def duplicateProcessing(self, processingId, processingIdnew, xOffset, yOffset):
		processing = self._processingOfId(processingId) 	
		newProcessing = processing.makeelement(processing.tag,processing.attrib)
		newProcessing.attrib['id']=processingIdnew
		x, y = eval(newProcessing.attrib['position'])
		newProcessing.attrib['position'] = '%s, %s' % (x+xOffset, y+yOffset)
		
		for element in processing.getchildren():
			newProcessing.append(copy.copy(element))
		
		index = self._findLastTag("processing") 
		network = self.document.getroot()
		network.insert(index, newProcessing)
		
		# keep internal data structure intact
		self.processings.append(newProcessing)
		self._log("Duplicated %s as %s" %(processingId, processingIdnew))
		
	def renameConfig(self, processingType, oldName, newName) :
		"""Change the name of a config parameter"""
		if self._versionNotApplies() : return
		for processing in self._processingsOfType(processingType) : 
			configs = processing.findall(oldName)
			for config in configs :
				config.tag = newName
				self._log("Renaming configuration parameter for processing %s: %s -> %s" %(
				processing.get("id"), oldName, newName))

	def setConfigByType(self, processingType, name, value) :
		"""Change the value for a config parameter"""
		if self._versionNotApplies() : return
		for processing in self._processingsOfType(processingType) : 
			configs = processing.findall(name)
			for config in configs :
				config.text = value
				self._log("Setting configuration parameter %s.%s = %s" %(
					processing.get("id"), name, value))

	def transformConfigByType(self, processingType, name, expression) :
		"""Transform the value for a config parameter"""
		if self._versionNotApplies() : return
		for processing in self._processingsOfType(processingType) : 
			configs = processing.findall(name)
			for config in configs :
				value=config.text
				config.text = eval(expression)
				self._log("Setting configuration parameter %s.%s = %s" %(
					processing.get("id"), name, config.text))

	def setConfig(self, processingId, param, value) :
		"""Change the value for a config parameter"""
		if self._versionNotApplies() : return
		processing = self._processingOfId(processingId)
		if not processing : raise "Processing ID '%s' not found" % processingId
		configs = processing.findall(param)
		for config in configs :
			config.text = value
			self._log("Setting configuration parameter %s.%s = %s" %(
				processing.get("id"), param, value))

	def addConfig(self, processingType, name, default) :
		"""Change the name of a config parameter"""
		if self._versionNotApplies() : return
		for processing in self._processingsOfType(processingType) :
			if processing.findall(name) : continue # Already present, don't add
			parameterElement = ElementTree.Element(name) 
			parameterElement.text = unicode(default)
			processing.append(parameterElement)
			self._log("Adding configuration parameter %s.%s = %s" %(
				processing.get("id"), name, default))

	def removeConfig(self, processingType, name) :
		if self._versionNotApplies() : return
		for processing in self._processingsOfType(processingType) :
			for param in processing.findall(name) :
				if param.tag != name : continue
				processing.remove(param) 
				self._log("Removing configuration parameter %s.%s" %(
					processing.get("id"), name))

	def moveConfig(self, processingType, name, beforeTag=None) :
		raise "Not implemented!!!!"
		if self._versionNotApplies() : return
		for processing in self._processingsOfType(processingType) :
			for param in processing.findall(name) :
				processing.remove(param) 
				self._log("Removing configuration parameter %s.%s" %(
					processing.get("id"), name))

	def noOp(self) :
		if self.verbose : print "Dummy command"
		pass

	def help(self, command) :
		help(getattr(self,command))

	def commands(self) :
		print "Available commands:", ", ".join(self._availableCommands())


def test() :
	testInput = "../../NetworkEditor/example-data/FilePlayer.clamnetwork"
	network = ClamNetwork(file(testInput))
	commandFileContent =  [
		'beVerbose',
		'ensureVersion 1.3',
		'renameClass OutControlSender Foo',
		'renameConnector AudioMixer inport "Input 0" lala',
		'renameConfig AudioMixer NumberOfInPorts NInputs',
		'setConfigByType AudioMixer NInputs 14',
		'upgrade 1.3.2',
		# not executed because we are still ensuring 1.3
		'renameConnector AudioMixer inport NInputs NumberOfInPorts',
		'ensureVersion 1.3.2',
		'setConfig "Gain 0" Max 2',
		'addConfig AudioMixer Ramping 1',
		'addConfig AudioMixer NInputs 96',
		'removeConfig AudioMixer FrameSize',
		'upgrade 1.4',
				
		#'removeOutConnections Vbap3D'
		#'addConnection Vbap3D 01 AudioSink 1'
		#'duplicateProcessing AudioMixer AudioMixerCopy_1 0 100',
	]
	

	for command in commandFileContent :
		network.runCommand(command)
	try:
		network.setConfig('NonExistingId', "Max", "2")
	except: print "Exception caugth"
	network.dump()
	network.commands()
	sys.exit(0)

if __name__ == "__main__" :
	from optparse import OptionParser
	parser = OptionParser(
		usage="usage: %prog [-c COMMAND|-f SCRIPTFILE] network1 network2...",
		version="%prog 0.9"
		)
	parser.add_option("-v", "--verbose", dest='verbose', action='store_true')
	parser.add_option("-q", "--quiet", dest='verbose', action='store_false')
	parser.add_option("-f", "--file", dest='scriptFile',
			help="Specifies a file with commands", metavar="SCRIPTFILE")
	parser.add_option("-c", "--command", dest='commands', action='append',
			help="Specifies a command to be executed on the networks", metavar="COMMAND")
	parser.add_option("-t", "--test", dest='test', action="store_true",
			help="Runs the library test")
	parser.add_option("-x", "--apply", dest='apply', action="store_true",
			help="Actually modify the files (by default they are just dumped to the screen).")
	options, args = parser.parse_args()

	if options.test :
		test()

	commands = []
	if options.commands :
		commands = options.commands
	elif options.scriptFile :
		commands = file(options.scriptFile).readlines()

	if not commands :
		print >> sys.stderr, "No command specified, use either -c or -f options"
	if not args :
		print >> sys.stderr, "No file to be processed"
		

	for filename in args :
		print >> sys.stderr, "Processing", filename
		network = ClamNetwork(file(filename))
		for command in commands :
			if not command.strip() or command.strip()[0]=='#' :
				continue
			network.runCommand(command)

		output = sys.stdout
		if network.modified :
			if options.apply :
				print "Updating", filename
				output = open(filename,"w")
			network.dump(output)
		print >> sys.stderr, "No changes applied."