File: TemplateGenerator.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 (321 lines) | stat: -rwxr-xr-x 9,173 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
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#TODO:
#* Add configuration options
#* Add typed controls support

Usage = """Usage: ./TemplateGenerator TEMPLATEFILE"""

import os, sys, shutil
import sets

def make_include(class_name):
	return "#include <CLAM/" + class_name + ".hxx>\n"
#make_include()

def make_base_processing_hxx_file(definitions_dict):
	""" BaseProcessing.hxx file """

	try:
		filename = "templates/%s/BaseProcessing.hxx"%(definitions_dict["template_name"])
		f = open(filename, "w")
	except:
		print "Output file write error. File: "+filename
		sys.exit(2)

	f.write( "#ifndef _BaseProcessing_\n" )
	f.write( "#define _BaseProcessing_\n\n" )

	inputs = definitions_dict["inputs"]
	outputs = definitions_dict["outputs"]
	incontrols = definitions_dict["incontrols"]
	outcontrols = definitions_dict["outcontrols"]

	#Includes
	f.write( make_include(definitions_dict["base_class_name"]) )
	available_port_types_set = sets.Set( (item[0] for item in inputs+outputs) )
	for class_name in available_port_types_set: f.write( make_include(class_name) )
	if len(incontrols)>0: f.write( make_include("InControl") )
	if len(outcontrols)>0: f.write( make_include("OutControl") )
	
	f.write( """
namespace CLAM {

""" )
	
	#Class
	f.write( """	/**	\\brief Short description
	*
	*	Description
	*/
	class BaseProcessing: public %s
	{	
"""%(definitions_dict["base_class_name"]) )
		
	#GetClassName
	f.write("""		/** This method returns the name of the object
		*	@return Char pointer with the name of object
		*/
		const char *GetClassName() const { return \"BaseProcessing\"; }

""")
	
	member_style = definitions_dict["member_style"]
	#Ports
	f.write( "\t\t/** Ports **/\n" )
	for port_type,port_name in (inputs+outputs):
		if member_style=='_': port_name = port_name[0].lower()+port_name[1:]
		f.write( "\t\t" + port_type + " " + member_style + port_name.replace(' ', '') + ";\n" )
	
	#Controls
	f.write( "\n\t\t/** Controls **/\n" )
	for cmin,cmax,control_name in incontrols:
		f.write( "\t\tFloatInControl " + member_style + control_name.replace(' ', '') + ";\n" )
	for cmin,cmax,control_name in outcontrols:
		f.write( "\t\tFloatOutControl " + member_style + control_name.replace(' ', '') + ";\n" )
	
	f.write( "\n\tpublic:\n" )
	
	#Constructor
	f.write( "\t\tBaseProcessing(const Config & config=Config())\n\t\t\t")
	members_list = []
	for port_type,port_name in (inputs+outputs):
		members_list.append( "\t\t\t" + member_style + port_name.replace(' ', '') + "(\"" + port_name + "\", this)" )
	for cmin,cmax,control_name in (incontrols+outcontrols):
		members_list.append( "\t\t\t" + member_style + control_name.replace(' ', '') + "(\"" + control_name + "\", this)" )
	
	if len(members_list)>0:
		f.write(":\n")
		for i in range(len(members_list)-1):
			f.write( members_list[i] + ",\n" )
		f.write( members_list[len(members_list)-1] )
		
	f.write( """
		{
			Configure( config );

""" )
	
	#InControls SetBounds, SetDefaultValue, DoControl
	for cmin,cmax,control_name in (incontrols):
		control_media = round((float(cmin)+float(cmax))/2.)
		f.write( "\t\t\t" +  member_style + control_name.replace(' ', '') + ".SetBounds(" + cmin + "," + cmax + ");\n" )
		f.write( "\t\t\t" +  member_style + control_name.replace(' ', '') + ".SetDefaultValue(" + str(control_media) + ");\n" )
		f.write( "\t\t\t" +  member_style + control_name.replace(' ', '') + ".DoControl(" + str(control_media) + ");\n" )
	f.write( "\t\t}\n" )
	
	
	#Destructor
	f.write( "\n\t\t~BaseProcessing() {}\n" )
	
	#Do()
	f.write( """
		bool Do()
		{
			return true;
		}""" )

	f.write("""
	
	private:
		/** Child processings **/
	
	};

};//namespace CLAM

#endif // _BaseProcessing_
""")
	f.close()
#make_base_processing_hxx_file()

def make_base_processing_cxx_file(definitions_dict):
	""" BaseProcessing.cxx file """
	try:
		filename = "templates/%s/BaseProcessing.cxx"%(definitions_dict["template_name"])
		f = open(filename, "w")
	except:
		print "Output file write error. File: "+filename
		sys.exit(2)
	
	f.write( """#include "BaseProcessing.hxx"
#include <CLAM/ProcessingFactory.hxx>

namespace CLAM
{

namespace Hidden
{
	static const char * metadata[] = {
		"key", "BaseProcessing",
		"category", "Plugins",
		"description", "BaseProcessing",
		0
	};
	static FactoryRegistrator<ProcessingFactory, BaseProcessing> reg = metadata;
}

}
""")
	f.close()
#make_base_processing_cxx_file()

def make_sconstruct_file(definitions_dict):
	""" Standard SConstruct file """

	try:
		filename = "templates/%s/SConstruct"%(definitions_dict["template_name"])
		f = open(filename,"w")
	except:
		print "Output file write error. File: "+filename
		sys.exit(2)

	f.write("""#! /usr/bin/python

import os, glob, sys

libraryName='baseprocessing'

def scanFiles(pattern, paths) :
	files = []
	for path in paths :
		files+=glob.glob(os.path.join(path,pattern))
	return files

def recursiveDirs(root) :
	return filter( (lambda a : a.rfind( ".svn")==-1 ),  [ a[0] for a in os.walk(root)]  )

options = Variables('options.cache', ARGUMENTS)
options.Add(PathVariable('clam_prefix', 'The prefix where CLAM was installed', ''))
if sys.platform=='linux2' :
	options.Add(BoolVariable('crossmingw', 'Using MinGW crosscompiler mode', 'no') )
env = Environment(ENV=os.environ, options=options)
options.Save('options.cache', env)
Help(options.GenerateHelpText(env))
env.SConsignFile() # Single signature file

CLAMInstallDir = env['clam_prefix']
clam_sconstoolspath = os.path.join(CLAMInstallDir,'share','clam','sconstools')
if env['crossmingw'] :
	env.Tool('crossmingw', toolpath=[clam_sconstoolspath])
env.Tool('clam', toolpath=[clam_sconstoolspath])
env.EnableClamModules([
	'clam_core',
	'clam_audioio',
	'clam_processing',
	] , CLAMInstallDir)

sourcePaths = recursiveDirs(".")
sources = scanFiles('*.cxx', sourcePaths)
sources = dict.fromkeys(sources).keys()
if sys.platform=='linux2' :
	env.Append( CCFLAGS=['-g','-O3','-Wall'] )
libraries = [
	env.SharedLibrary(target=libraryName, source = sources),
	]

install = env.Install(os.path.join(CLAMInstallDir,'lib','clam'), libraries)

env.Alias('install', install)
env.Default(libraries)
""")
	f.close()
#make_sconstruct_file()

def make_readme_file(definitions_dict):
	""" Standard SConstruct file """

	try:
		filename = "templates/%s/README"%(definitions_dict["template_name"])
		f = open(filename,"w")
	except:
		print "Output file write error. File: "+filename
		sys.exit(2)

	f.write("""== Description ==


== Install ==

scons clam_prefix=path/to/installed/clam/prefix
scons install
""")
	f.close()
#make_readme_file()

def main(args): 
	""" Generates a template for clam plugins from a config file
	
	Example of input file (template file):
	
	Name:TestTemplate
	BaseClass:Processing
	i:AudioInPort,Audio Input Name 1
	i:AudioInPort,AudioInput Name 2
	o:AudioOutPort,Audio Output Name
	ic:0,10,Input Control Name
	oc:0,0,Output Control Name
	
	Convention:
	i (input) | o (output): type,Name
	ic (input control) | oc (output control): integer/float, range
	"""

	if len(sys.argv) < 2:
		print "\nBad amount of input arguments.\n", Usage, "\n"
		sys.exit(1)

	try:
		f = open( "./" + sys.argv[1], 'r' )
	except IOError:
		print "Template file read error."
		sys.exit(2)

	definitions_dict = {}
	definitions_dict["inputs"] = list()
	definitions_dict["outputs"] = list()
	definitions_dict["incontrols"] = list()
	definitions_dict["outcontrols"] = list()

	# Style definitions:
#	definitions_dict["member_style"] = "m"
	definitions_dict["member_style"] = "_"

	filedata = f.read(); f.close()
	for elem in filedata.splitlines():
		if elem!='' and elem[0]!='#':
			(item, value) = elem.split(':')
			if   item=="Name": definitions_dict["template_name"] = value 
			elif item=="BaseClass": definitions_dict["base_class_name"] = value
			elif item=='i': definitions_dict["inputs"].append( value.split(',') )
			elif item=='o': definitions_dict["outputs"].append( value.split(',') )
			elif item=='ic': definitions_dict["incontrols"].append( value.split(',') )
			elif item=='oc': definitions_dict["outcontrols"].append( value.split(',') )

	if not os.path.isdir( "templates/" + definitions_dict["template_name"] ): os.mkdir( "templates/" + definitions_dict["template_name"] )

	print "Creating " + definitions_dict["template_name"] + " template"
	make_base_processing_hxx_file(definitions_dict)
	make_base_processing_cxx_file(definitions_dict)
	make_sconstruct_file(definitions_dict)
	make_readme_file(definitions_dict)
#main()


if __name__ == '__main__':
	main(sys.argv)