File: template.py

package info (click to toggle)
gpick 0.2.6-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,800 kB
  • sloc: cpp: 27,983; python: 738; xml: 70; makefile: 37; sh: 10
file content (33 lines) | stat: -rw-r--r-- 986 bytes parent folder | download | duplicates (3)
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
import re, os
from SCons.Script import Builder
from SCons.Action import Action
from SCons.Node.FS import File
def addTemplateBuilder(env):
	def buildEmitter(target, source, env):
		data = open(source[0].srcnode().get_path()).read()
		dict = env.Dictionary()
		keys = dict.keys()
		for key in keys:
			if isinstance(dict[key], str):
				env.Depends(target, env.Value(dict[key]))
		return target, source
	def buildFile(target, source, env):
		source_dest = str(target[0])
		wfile = open(source_dest, "w")
		data = open(source[0].srcnode().get_path()).read()
		dict = env.Dictionary()
		keys = dict.keys()
		for key in keys:
			if isinstance(dict[key], str):
				data = re.sub("%" + key + "%", dict[key], data)
		wfile.write(data)
		wfile.close()
		return 0
	def buildString(target, source, env):
		return "Preparing file %s" % os.path.basename(str(target[0]))
	env.Append(BUILDERS = {
		'Template': Builder(
			action = Action(buildFile, buildString),
			emitter = buildEmitter,
		),
	})