################################################################################
#
# builds system files according to the configuration specified in 'config.xml'
#
################################################################################

import os, sys
import amara
import shutil
import pyutils

from mako.lookup import TemplateLookup
from mako import exceptions
from pyutils import tokenize

################################################################################

lookup = TemplateLookup(directories = ['.'], module_directory = '../temp/config')
env = Environment(MAKO_LOOKUP = lookup)

################################################################################

def to_lines(lineiter):
	lines = [s.strip(' \n') for s in lineiter]
	return [l for l in lines if l and not l.startswith(';')]

def load_file(fname):
	lines = []
	if os.path.isfile(fname):
		f = open(fname, 'r'); lines = to_lines(f); f.close()
	return lines

def save_file(fname, lines):
	dir = os.path.dirname(fname)
	if not os.path.isdir(dir):
		os.makedirs(dir)
	f = open(fname, 'w')
	for line in lines:
		f.write(line + '\n')
	f.close()

def update_hist(target, source, env):
	fname_hist, key = env['CONFIG_FNAME_HIST'], env['CONFIG_DATAKEY']
	data, hist = load_file(target[0].path), load_file(fname_hist)
	if len(data) <= 2:
		return
	if len(hist) < 2:
		hist[:] = data[:2]
	key_ind = tokenize(data[1]).index(key)
	hist_keys = [tokenize(hist[i])[key_ind] for i in range(2, len(hist))]
	for i in range(2, len(data)):
		keyval = tokenize(data[i])[key_ind]
		try:
			hist_ind = hist_keys.index(keyval)
			hist[hist_ind + 2] = data[i]
		except:
			hist.append(data[i])
	save_file(fname_hist, hist)

def update_data(target, source, env):
	fname_hist, key = env['CONFIG_FNAME_HIST'], env['CONFIG_DATAKEY']
	data, hist = load_file(target[0].path), load_file(fname_hist)
	key_ind = tokenize(data[1]).index(key)
	hist_keys = [tokenize(hist[i])[key_ind] for i in range(2, len(hist))]
	for i in range(2, len(data)):
		keyval = tokenize(data[i])[key_ind]
		try:
			hist_ind = hist_keys.index(keyval)
			data[i] = hist[hist_ind + 2]
		except: pass
	save_file(target[0].path, data)

def mako_render(target, source, env):
	import makoass
	f_tgt = open(target[0].path, 'wb')
	for src in source:
		ksrc = str(src).replace(os.sep, '/')
		if ksrc not in makoass.ass_targets:
			print "   info: key '%s' not found" % ksrc
		param = makoass.ass_targets.get(ksrc, {})
		try:
			tmpl = env['MAKO_LOOKUP'].get_template(str(src))
		except:
			print exceptions.text_error_template().render()
		f_tgt.write(tmpl.render(**param))
	f_tgt.close()

################################################################################

action_render = Action(mako_render, lambda t,s,e: "building '%s'" % t[0].path)

def split_sources(target, app, cond):
	source = []
	for file, xpath in target:
		if xpath not in cond:
			cond[xpath] = len(app.xml_select(xpath)) > 0
		if cond[xpath]:
			source.append(file)
	if len(source) > 0:
		src_mako = filter(lambda x: str(x).endswith('.mako'), source)
		src_other = filter(lambda x: not str(x).endswith('.mako'), source)
		return (src_mako, src_other)
	else:
		return ([], [])

def build_config_std(env, targets, config, deps = []):
	cond = {}
	app = config.xml_select('config/instance/application/*[@node]')[0]
	render = Action(mako_render, lambda t,s,e: "building '%s'" % t[0].path)
	for tname in targets:
		src_mako, src_other = split_sources(targets[tname], app, cond)
		if len(src_mako) + len(src_other) > 0:
			env.Depends(tname, src_other + deps)
			env.Command(tname, src_mako, action_render)

def build_config_hist(env, targets, config, deps = []):
	cond = {}
	app = config.xml_select('config/instance/application/*[@node]')[0]
	for tname, dkey in targets:
		src_mako, src_other = split_sources(targets[(tname, dkey)], app, cond)
		if len(src_mako) + len(src_other) > 0:
			tdir, tbase = os.path.split(tname)
			fname_hist = os.path.join(tdir, '.config', tbase) + '.hist'
			param = {'CONFIG_FNAME_HIST' : fname_hist, 'CONFIG_DATAKEY' : dkey}
			t = env.Command(tname, src_mako, action_render, **param)
			env.AddPreAction(t, Action(update_hist, None))
			env.AddPostAction(t, Action(update_data, None))
			env.Precious(t)
			env.Clean(t, fname_hist)
			env.Depends(t, src_other + deps)


def build_config_reg(env, targets_ci, targets_aft, config):
	app = config.xml_select('config/instance/application/*[@node]')[0]
	rtyp = ['8Color', '10Color', '12Color']
	aityp = ['std_8', 'std_10', 'std_12']
	pmtabs = ['PmArray', 'PmMark', 'PmPattern', 'PmType', 'PmUsage']
	xp_ci = 'config/instance/module/ci'
	xp_aft = 'config/instance/module/aft'
	xp_markreg = 'config/instance/module/ci/option[@name="markpttn"]/@value'
	xp_markaft = 'config/instance/module/aft/option[@name="markpttn"]/@value'
	xp_ai = 'config/instance/module/ci/option[@name="ai"]/@value'
	pumax_path = 'config/instance/machine/option[@name="punumb"]/@value'
	pu_sel = config.xml_select(pumax_path)
	pumax = pu_sel[0].xml_value

	aft_sel = config.xml_select(xp_aft)
	aft_on = (len(aft_sel) > 0)
	ci_sel = config.xml_select(xp_ci)
	ci_on = (len(ci_sel) > 0)

	__dir = os.getcwd()
	__name=__dir.replace('config', '')
	__name=os.path.join(__name, 'data')
	__dir_dest  = os.path.join(__name, 'select')

	if ci_on:
		ai_on = 1 if pyutils.bool_from_str(config.xml_select(xp_ai)[0].xml_value) else 0
		markci = config.xml_select(xp_markreg)[0].xml_value
		if targets_ci.has_key(markci):
			cityp_dir = targets_ci[markci]
			if ai_on and (markci in aityp):
				cityp_dir = cityp_dir +'-fast'
			if int(pumax) < 9:
				__dir_src = os.path.join(__dir_dest, rtyp[0])
			elif int(pumax) < 11:
				__dir_src = os.path.join(__dir_dest, rtyp[1])
			else:
				__dir_src = os.path.join(__dir_dest, rtyp[2])
			__dir_src = os.path.join(__dir_src, cityp_dir)
			for elm in pmtabs:
				_dest  = os.path.join(__dir_dest, elm)
				_src  = os.path.join(__dir_src, elm)
				shutil.copy2(_src, _dest)

	if aft_on:
		markaft = config.xml_select(xp_markaft)[0].xml_value
		if targets_aft.has_key(markaft):
			afttyp_dir = targets_aft[markaft]
			__dir_src = os.path.join(__dir_dest, rtyp[2])
			__dir_src = os.path.join(__dir_src, afttyp_dir)
			for elm in pmtabs:
				_dest  = os.path.join(__dir_dest, elm)
				_src  = os.path.join(__dir_src, elm)
				shutil.copy2(_src, _dest)

env.AddMethod(build_config_std, "ConfigStd")
env.AddMethod(build_config_hist, "ConfigHist")
env.AddMethod(build_config_reg, "ConfigReg")

################################################################################

config = amara.parse('config.xml')

cam_count = 0
cam_index = 0
info_mount_cam1 = 'top'
info_mount_cam2 = 'bot'
info_mount_cam3 = 'top'
info_index_cam1 = '0'
info_index_cam2 = '0'
info_index_cam3 = '0'
info_ipm_id = ''
for cam in config.xml_select('config/instance/camera/*'):
	cam_count = cam_count + 1
	cam_index = str(cam.xml_select('option[@name="index"]/@value')[0].xml_value)
	if cam_count == 1:
		info_mount_cam1 = str(cam.xml_select('option[@name="mount"]/@value')[0].xml_value)
		info_index_cam1 = str(cam_index)
	elif cam_count == 2:
		info_mount_cam2 = str(cam.xml_select('option[@name="mount"]/@value')[0].xml_value)
		info_index_cam2 = str(cam_index)
	elif cam_count == 3:
		info_mount_cam3 = str(cam.xml_select('option[@name="mount"]/@value')[0].xml_value)
		info_index_cam3 = str(cam_index)
	if cam_index == '7':
		info_ipm_id = str(cam.xml_name[1])
info_cam_count = str(cam_count)

pumax_path = 'config/instance/machine/option[@name="punumb"]/@value'
pu_sel = config.xml_select(pumax_path)
pu_count = pu_sel[0].xml_value

info_pu_count = str(pu_count)

trav_count = 0
info_trav_index1 = '0'
info_trav_index2 = '0'
info_trav_index3 = '0'
info_trav_index4 = '0'
for trav in config.xml_select('config/instance/traverse/*'):
	trav_count = trav_count + 1
	if trav_count == 1:
		info_trav_index1 = str(trav.xml_select('option[@name="index"]/@value')[0].xml_value)
	elif trav_count == 2:
		info_trav_index2 = str(trav.xml_select('option[@name="index"]/@value')[0].xml_value)
	elif trav_count == 3:
		info_trav_index3 = str(trav.xml_select('option[@name="index"]/@value')[0].xml_value)
	elif trav_count == 4:
		info_trav_index4 = str(trav.xml_select('option[@name="index"]/@value')[0].xml_value)
info_trav_count = str(trav_count)

################################################################################

targets_config = {
	'../iodev.cfg' :  [
				('template/func/wv/wv_can.mako',            'core/func/wv | setup/func/wv'),
				('template/func/wv/wv_cam.mako',            'core/func/wv | setup/func/wv'),
				('template/func/wv/wv_travberg.mako',       'core/func/wv | setup/func/wv'),
				('template/func/wv/wv_travnano.mako',       'core/func/wv | setup/func/wv'),
				('template/func/wv/wv_travgear.mako',       'core/func/wv | setup/func/wv'),
				('template/func/wv/wv_trig.mako',           'core/func/wv | setup/func/wv'),
				('template/func/wv/wv_irf.mako',            'core/func/wv'),
				('template/func/wv/wv_joy.mako',            'core/func/wv'),
				('template/func/wv/wv_pps.mako',            'core/func/wv | setup/func/wv'),
				('template/func/mmx/mmx_io.mako',           'core/func/mmx'),
				('template/func/reg/regmot_fk.mako',        'core/func/ci'),
				('template/func/reg/regmot_off.mako',       'core/func/ci'),
				('template/func/reg/regmot_pb.mako',        'core/func/ci'),
				('template/func/reg/regmot_schiavi.mako',   'core/func/ci'),
				('template/func/reg/regmot_wh.mako',        'core/func/ci | core/func/aft'),
				('template/func/reg/scmot_off.mako',        'core/func/sc'),
				('template/func/reg/scmot_pstp.mako',       'core/func/sc'),
				('template/func/reg/scmot_wh.mako',         'core/func/sc'),
				('template/func/reg/aft_eltro.mako',        'core/func/aft'),
				('template/func/reg/aft_wh.mako',           'core/func/aft'),
				('template/func/reg/aicom_can.mako',        'core/func/ai'),
				('template/func/reg/aicom_pb.mako',         'core/func/ai'),
			],

	'../application.cfg' :  [
				('template/func/glob/glob_core.mako',       'core/func'),
				('template/func/glob/glob_setup.mako',      'setup/func'),
				('template/func/glob/glob_job.mako',        'job/func'),
				('template/func/wv/wv_setup.mako',          'setup/func/wv'),
				('template/func/wv/wv_job.mako',            'job/func/wv'),
				('template/func/wv/wv_core.mako',           'core/func/wv'),
				('template/func/wv/wv_pos.mako',            'core/func/wv'),
				('template/func/wv/wv_travsim.mako',        'core/func/wv | setup/func/wv'),
				('template/func/wv/wv_img.mako',            'core/func/wv | setup/func/wv'),
				('template/func/wv/wv_mon.mako',            'core/func/wv'),
				('template/func/wv/wv_sens.mako',           'core/func/wv | setup/func/wv'),
				('template/func/wv/wv_dig.mako',            'core/func/wv'),
				('template/func/wv/wv_gear.mako',           'core/func/wv'),
				('template/func/mmx/mmx_core.mako',         'core/func/mmx'),
				('template/func/mmx/mmx_pos.mako',          'core/func/mmx'),
				('template/func/mmx/mmx_job.mako',          'job/func/mmx'),
				('template/func/mmx/mmx_setup.mako',        'setup/func/mmx'),
				('template/func/sis/sis_core.mako',         'core/func/sis'),
				('template/func/sis/sis_pos.mako',          'core/func/sis'),
				('template/func/sis/sis_job.mako',          'job/func/sis'),
				('template/func/sis/sis_setup.mako',        'setup/func/sis'),
				('template/func/bc/bc_core.mako',           'core/func/bc'),
				('template/func/bc/bc_pos.mako',            'core/func/bc'),
				('template/func/bc/bc_job.mako',            'job/func/bc'),
				('template/func/bc/bc_setup.mako',          'setup/func/bc'),
				('template/func/icm/icm_core.mako',         'core/func/icm'),
				('template/func/icm/icm_pos.mako',          'core/func/icm'),
				('template/func/icm/icm_job.mako',          'job/func/icm'),
				('template/func/icm/icm_setup.mako',        'setup/func/icm'),
				('template/func/reg/reg_core.mako',         'core/func/aft | core/func/ci'),
				('template/func/reg/reg_job.mako',          'job/func/aft | job/func/ci'),
				('template/func/reg/reg_setup.mako',        'setup/func/aft | setup/func/ci'),
				('template/func/reg/regmot_sim.mako',       'core/func/ci'),
				('template/func/reg/sc_core.mako',          'core/func/sc'),
				('template/func/reg/scmot_sim.mako',        'core/func/sc'),
				('template/func/reg/aft_core.mako',         'core/func/aft'),
				('template/func/reg/ai_core.mako',          'core/func/ai'),
				('template/func/app/app_setup.mako',        'setup/func'),
				('template/func/app/app_job.mako',          'job/func'),
				('template/func/app/app_core.mako',         'core/func'),
				('template/func/app/app_data.mako',         '*/data'),
				('template/func/app/app_hmi.mako',          '*/hmi')
			],

   '../hmi/dialog/pf.stg' : [
            ('template/hmi/glob/pf_core.mako',          '*/hmi'),
            ('template/hmi/help/pf_core.mako',          '*/hmi'),             # (leer)
            ('template/hmi/widgets/pf_core.mako',       '*/hmi'),             # (leer)
            ('template/hmi/glob/pf_setup.mako',         'setup/hmi'),
            ('template/hmi/glob/pf_job.mako',           'job/hmi'),
            ('template/hmi/wv/pf_setup.mako',           'setup/hmi/wv'),
            ('template/hmi/wv/pf_core.mako',            'core/hmi/wv'),
            ('template/hmi/wv/pf_job.mako',             'job/hmi/wv'),
            ('template/hmi/mmx/pf_setup.mako',          'setup/hmi/mmx'),
            ('template/hmi/mmx/pf_core.mako',           'core/hmi/mmx'),
            ('template/hmi/mmx/pf_job.mako',            'job/hmi/mmx'),
            ('template/hmi/sis/pf_setup.mako',          'setup/hmi/sis'),
            ('template/hmi/sis/pf_core.mako',           'core/hmi/sis'),
            ('template/hmi/sis/pf_job.mako',            'job/hmi/sis'),
            ('template/hmi/bc/pf_setup.mako',           'setup/hmi/bc'),
            ('template/hmi/bc/pf_core.mako',            'core/hmi/bc'),
            ('template/hmi/bc/pf_job.mako',             'job/hmi/bc'),
            ('template/hmi/reg/pf_reg_setup.mako',      'setup/hmi/ci | setup/hmi/aft'),
            ('template/hmi/reg/pf_aft_setup.mako',      'setup/hmi/aft'),
            ('template/hmi/reg/pf_aft_core.mako',       'core/hmi/aft'),
            ('template/hmi/reg/pf_ai_core.mako',        'core/hmi/ai'),
            ('template/hmi/reg/pf_ci_core.mako',        'core/hmi/ci'),
            ('template/hmi/reg/pf_reg_job.mako',        'job/hmi/ci | job/hmi/aft'),
            ('template/hmi/reg/pf_ci_job.mako',         'job/hmi/ci'),
            ('template/hmi/reg/pf_aft_job.mako',        'job/hmi/aft'),
            ('template/hmi/icm/pf_setup.mako',          'setup/hmi/icm'),
            ('template/hmi/icm/pf_core.mako',           'core/hmi/icm'),
            ('template/hmi/icm/pf_job.mako',            'job/hmi/icm'),
            ('template/hmi/mis/pf_core.mako',           'core/hmi/mis'),
         ],

	'../hmi/dialog/dmu.stg' : [
				('template/hmi/glob/dmu_dlg.mako',          '*/hmi'),
				('template/hmi/wv/dmu_dlg.mako',            '*/hmi/wv'),
				('template/hmi/mmx/dmu_dlg.mako',           '*/hmi/mmx'),
				('template/hmi/sis/dmu_dlg.mako',           '*/hmi/sis'),
				('template/hmi/bc/dmu_dlg.mako',            '*/hmi/bc'),
				('template/hmi/icm/dmu_dlg.mako',           '*/hmi/icm'),
				('template/hmi/reg/dmu_dlg_aft.mako',  	  '*/hmi/aft'),
				('template/hmi/reg/dmu_dlg_ci.mako',        '*/hmi/ci'),
				('template/hmi/mis/dmu_dlg.mako',           '*/hmi/mis'),
				('template/hmi/glob/dmu_hw.mako',           '*/hmi'),
				('template/hmi/glob/dmu_ctrl.mako',         '*/hmi'),
				('template/hmi/glob/dmu_app.mako',          '*/hmi'),
				('template/hmi/glob/dmu_trans_core.mako',   '*/hmi'),
				('template/hmi/glob/dmu_trans_setup.mako',  '*/hmi'),
				('template/hmi/wv/dmu_trans_core.mako',     '*/hmi/wv'),
				('template/hmi/wv/dmu_trans_job.mako',      '*/hmi/wv'),
				('template/hmi/wv/dmu_trans_setup.mako',    '*/hmi/wv'),
				('template/hmi/mmx/dmu_trans_core.mako',    '*/hmi/mmx'),
				('template/hmi/mmx/dmu_trans_job.mako',     '*/hmi/mmx'),
				('template/hmi/mmx/dmu_trans_setup.mako',   '*/hmi/mmx'),
				('template/hmi/sis/dmu_trans_core.mako',    '*/hmi/sis'),
				('template/hmi/sis/dmu_trans_job.mako',     '*/hmi/sis'),
				('template/hmi/sis/dmu_trans_setup.mako',   '*/hmi/sis'),
				('template/hmi/bc/dmu_trans_core.mako',     '*/hmi/bc'),
				('template/hmi/bc/dmu_trans_job.mako',      '*/hmi/bc'),
				('template/hmi/bc/dmu_trans_setup.mako',    '*/hmi/bc'),
				('template/hmi/reg/dmu_trans_setup_ci.mako',   '*/hmi/ci'),
				('template/hmi/reg/dmu_trans_setup_aft.mako',  '*/hmi/aft'),
				('template/hmi/reg/dmu_trans_core_aft.mako',   '*/hmi/aft'),
				('template/hmi/reg/dmu_trans_core_ci.mako',    '*/hmi/ci'),
				('template/hmi/reg/dmu_trans_job_aft.mako',    '*/hmi/aft'),
				('template/hmi/reg/dmu_trans_job_ci.mako',     '*/hmi/ci'),
				('template/hmi/icm/dmu_trans_core.mako',    '*/hmi/icm'),
				('template/hmi/icm/dmu_trans_job.mako',     '*/hmi/icm'),
				('template/hmi/icm/dmu_trans_setup.mako',   '*/hmi/icm'),
				('template/hmi/mis/dmu_trans_core.mako',    '*/hmi/mis'),
				('template/hmi/glob/dmu_trans_main.mako',   '*/hmi')
			],

	'../hmi/ctxt_ass.txt' : [
				('template/hmi/glob/ctxt_ass.mako',        '*/hmi'),
				('template/hmi/wv/ctxt_ass.mako',          '*/hmi/wv'),
				('template/hmi/mmx/ctxt_ass.mako',         '*/hmi/mmx'),
				('template/hmi/sis/ctxt_ass.mako',         '*/hmi/sis'),
				('template/hmi/bc/ctxt_ass.mako',          '*/hmi/bc'),
				('template/hmi/icm/ctxt_ass.mako',         '*/hmi/icm'),
				('template/hmi/reg/ctxt_ass_aft.mako',     '*/hmi/aft'),
				('template/hmi/reg/ctxt_ass_ci.mako',      '*/hmi/ci'),
				('template/hmi/mis/ctxt_ass.mako',         '*/hmi/mis'),
			],

	'../hmi/icon.txt' : [
				('template/hmi/glob/icons.mako',           '*/hmi'),
				('template/hmi/widgets/icons.mako',        '*/hmi'),
				('template/hmi/wv/icons.mako',             '*/hmi/wv'),
				('template/hmi/mmx/icons.mako',            '*/hmi/mmx'),
				('template/hmi/sis/icons.mako',            '*/hmi/sis'),
				('template/hmi/bc/icons.mako',             '*/hmi/bc'),
				('template/hmi/icm/icons.mako',            '*/hmi/icm'),
				('template/hmi/reg/icons_reg.mako',        '*/hmi/ci | */hmi/aft'),
				('template/hmi/reg/icons_ci.mako',         '*/hmi/ci'),
				('template/hmi/reg/icons_aft.mako',        '*/hmi/aft'),
				('template/hmi/mis/icons.mako',            '*/hmi/mis'),
			],

	'../hmi/dialog/ascii_kbd.dlg' : [
				('template/hmi/glob/ascii_kbd.dlg.mako',   '*/hmi')
			],

	'../hmi/dialog/app.stg' : [
				('template/hmi/glob/stg_core.mako',        '*/hmi/wv'),
				('template/hmi/glob/stg_help.mako',        '*/hmi'),
				('template/hmi/glob/stg_job.mako',         'job/hmi'),
				('template/hmi/glob/stg_setup.mako',       'setup/hmi'),
				('template/hmi/wv/stg_core.mako',          '*/hmi/wv'),
				('template/hmi/mmx/stg_core.mako',         '*/hmi/mmx'),
				('template/hmi/sis/stg_core.mako',         '*/hmi/sis'),
				('template/hmi/bc/stg_core.mako',          '*/hmi/bc'),
				('template/hmi/icm/stg_core.mako',         '*/hmi/icm'),
				('template/hmi/reg/stg_aft_core.mako',     '*/hmi/aft'),
				('template/hmi/reg/stg_ci_core.mako',      '*/hmi/ci'),
				('template/hmi/mis/stg_core.mako',         '*/hmi/mis'),
			],

	'../data/cfgman/CfgQuery' : [
				('template/data/cfgman/query_header.mako', 'core/data/wv'),
				('template/data/cfgman/query_wv.mako',     'core/data/wv'),
				('template/data/cfgman/query_mmx.mako',    'core/data/mmx'),
				('template/data/cfgman/query_bc.mako',     'core/data/bc'),
				('template/data/cfgman/query_sis.mako',    'core/data/sis'),
				('template/data/cfgman/query_reg.mako',    'core/data/ci | core/data/aft'),
				('template/data/cfgman/query_icm.mako',    'core/data/icm'),
				('template/data/cfgman/query_mis.mako',    'core/data/mis')
			],

	'../data/cfgman/CfgAssign' : [
				('template/data/cfgman/assign_header.mako','core/func/wv | setup/func/wv'),
				('template/data/cfgman/assign_cam.mako',   'core/func/wv | setup/func/wv'),
				('template/data/cfgman/assign_hmi.mako',   '*/hmi'),
				('template/data/cfgman/assign_img.mako',   'core/func/wv | setup/func/wv'),
				('template/data/cfgman/assign_mach.mako',  'core/func/wv'),
				('template/data/cfgman/assign_trav.mako',  'core/func/wv | setup/func/wv'),
				('template/data/cfgman/assign_trig.mako',  'core/func/wv | setup/func/wv'),
				('template/data/cfgman/assign_wv.mako',    'core/func/wv'),
				('template/data/cfgman/assign_gear.mako',  'core/func/wv'),
				('template/data/cfgman/assign_sens.mako',  'core/func/wv'),
				('template/data/cfgman/assign_mmx.mako',   'core/func/mmx'),
				('template/data/cfgman/assign_sis.mako',   'core/func/sis'),
				('template/data/cfgman/assign_bc.mako',    'core/func/bc'),
				('template/data/cfgman/assign_reg.mako',   'core/func/ci | core/func/aft'),
				('template/data/cfgman/assign_aft.mako',   'core/func/aft'),
				('template/data/cfgman/assign_ci.mako',    'core/func/ci'),
				('template/data/cfgman/assign_icm.mako',   'core/func/icm'),
				('template/data/cfgman/assign_mis.mako',   'core/func/mis')
			],

	'../data/copman/CopDepend' : [
				('template/data/copman/copdepend_header.mako',    'core/data/wv'),
				('template/data/copman/copdepend_wv.mako',        'core/data/wv'),
				('template/data/copman/copdepend_mmx.mako',       'core/data/mmx'),
				('template/data/copman/copdepend_sis.mako',       'core/data/sis'),
				('template/data/copman/copdepend_bc.mako',        'core/data/bc'),
				('template/data/copman/copdepend_reg.mako',       'core/data/aft | core/data/ci'),
				('template/data/copman/copdepend_icm.mako',       'core/data/icm'),
			],

	'../data/select/dbscheme' : [
				('template/data/select/wv/dbscheme_wv.mako',         '*/data/wv'),
				('template/data/select/mmx/dbscheme_mmx.mako',       '*/data/mmx'),
				('template/data/select/sis/dbscheme_sis.mako',       '*/data/sis'),
				('template/data/select/bc/dbscheme_bc.mako',         '*/data/bc'),
				('template/data/select/reg/dbscheme_reg.mako',       '*/data/aft | */data/ci'),
				('template/data/select/icm/dbscheme_icm.mako',       '*/data/icm')
			],

	'../data/select/FlashAutoScan' : [
				('template/data/select/wv/FlashAutoScan.mako', '*/data/wv')
			],

	'../data/select/SisResolution' : [
				('template/data/select/wv/SisResolution.mako', '*/data/wv')
			],

	'../data/select/SisSets' : [
				('template/data/select/sis/SisSets.mako', '*/data/sis')
			],

	'../data/select/SisSetsFilter' : [
				('template/data/select/sis/SisSetsFilter.mako', '*/data/sis')
			],

	#'../data/job/dbscheme' : [
	#			('template/data/job/wv/dbscheme_wv.mako',      '*/data/wv'),
	#			('template/data/job/mmx/dbscheme_mmx.mako',     '*/data/mmx')
	#		],

	'../data/system/dbscheme' : [
				('template/data/system/wv/dbscheme_wv.mako',         '*/data/wv'),
				('template/data/system/glob/dbscheme_glob.mako',     '*/data/wv'),
				('template/data/system/mmx/dbscheme_mmx.mako',       '*/data/mmx'),
				('template/data/system/cfg/dbscheme_cfg.mako',       '*/data/wv'),
				('template/data/system/sis/dbscheme_sis.mako',       '*/data/sis'),
				('template/data/system/bc/dbscheme_bc.mako',         '*/data/bc'),
				('template/data/system/reg/dbscheme_reg.mako',       '*/data/aft | */data/ci'),
				('template/data/system/icm/dbscheme_icm.mako',       '*/data/icm')
			],

	'../data/system/CameraTyp' : [
				('template/data/system/wv/CameraTyp.mako',     '*/data/wv')
			],

	'../data/system/ImgSvrCfg' : [
				('template/data/system/cfg/ImgSvrCfg.mako',    '*/data/wv')
			],

	'../data/system/SplitMode' : [
				('template/data/system/wv/SplitMode.mako',     '*/data/wv')
			],

	'../data/system/TriggerSync' : [
				('template/data/system/glob/TriggerSync.mako',   '*/data/wv')
			],

	'../data/system/UnitDefine' : [
				('template/data/system/cfg/UnitDefine.mako',   'core/data | setup/data')
			],

	'../data/system/WvFlashCfg' : [
				('template/data/system/cfg/WvFlashCfg.mako',   '*/data/wv')
			],

	'../data/system/WebLengthCam' : [
				('template/data/system/reg/WebLengthCam.mako',   '*/data/aft | */data/ci')
			],
}

targets_value = {
	'../data/system/MmxIpmMap' : [
				('template/data/system/mmx/MmxIpmMap.mako',    '*/data/mmx'),
				(env.Value(info_ipm_id),                       '*')
			],

	'../data/system/PrintUnitDscr' : [
				('template/data/system/reg/PrintUnitDscr.mako', '*/data/aft | */data/ci'),
				(env.Value(info_pu_count),                    '*'),
			],

	'../data/system/RegMotor' : [
				('template/data/system/reg/RegMotor.mako', '*/data/aft | */data/ci'),
				(env.Value(info_pu_count),                    '*'),
			],

	'../data/system/RegUnit' : [
				('template/data/system/reg/RegUnit.mako', '*/data/aft | */data/ci'),
				(env.Value(info_pu_count),                    '*'),
			],

	'../data/system/SCParam' : [
				('template/data/system/reg/SCParam.mako', '*/data/aft | */data/ci'),
				(env.Value(info_pu_count),                    '*'),
			],

	'../data/system/SCParam' : [
				('template/data/system/reg/SCParam.mako', '*/data/aft | */data/ci'),
				(env.Value(info_pu_count),                    '*'),
			],

	'../data/system/WebLengthPu' : [
				('template/data/system/reg/WebLengthPu.mako', '*/data/aft | */data/ci'),
				(env.Value(info_pu_count),                    '*'),
			],
}


targets_hist = {
	('../data/system/Camera', 'CamId') : [
				('template/data/system/wv/Camera.mako',        '*/data/wv'),
				(env.Value(info_mount_cam1),                   '*'),
				(env.Value(info_mount_cam2),                   '*'),
				(env.Value(info_mount_cam3),                   '*'),
				(env.Value(info_index_cam1),                   '*'),
				(env.Value(info_index_cam2),                   '*'),
				(env.Value(info_index_cam3),                   '*'),
				(env.Value(info_cam_count),                    '*'),
			],

	('../data/system/CoTransf', 'TrfId') : [
				('template/data/system/wv/CoTransf.mako',      '*/data/wv'),
				(env.Value(info_mount_cam1),                   '*'),
				(env.Value(info_mount_cam2),                   '*'),
				(env.Value(info_index_cam1),                   '*'),
				(env.Value(info_index_cam2),                   '*'),
				(env.Value(info_index_cam3),                   '*'),
				(env.Value(info_cam_count),                    '*')
			],

	('../data/system/Traverse', 'TravId') : [
				('template/data/system/wv/Traverse.mako',      '*/data/wv'),
				(env.Value(info_trav_index1),                  '*'),
				(env.Value(info_trav_index2),                  '*'),
				(env.Value(info_trav_index3),                  '*'),
				(env.Value(info_trav_index4),                  '*'),
				(env.Value(info_trav_count),                   '*')
			],

	('../data/system/TriggerCtrl', 'CamId') : [
				('template/data/system/glob/TriggerCtrl.mako', '*/data/wv'),
				(env.Value(info_cam_count),                    '*'),
				(env.Value(info_index_cam1),                   '*'),
				(env.Value(info_index_cam2),                   '*'),
				(env.Value(info_index_cam3),                   '*'),
			],

	#('../data/system/WebLengthCam', 'CamId') : [
	#			('template/data/system/reg/WebLengthCam.mako', '*/data/wv'),
	#			(env.Value(info_cam_count),                    '*'),
	#			(env.Value(info_index_cam1),                   '*'),
	#			(env.Value(info_index_cam2),                   '*'),
	#			(env.Value(info_index_cam3),                   '*'),
	#		],
}

cifield_targets = {'std_8' : 'xx0084D-xx0086D',
						 'std_8_0556' : 'xx0556-x0559',
						 'std_8_04262' : 'xx04262',
						 'std_8_wa15970' : 'xx04262-WA15970',
						 'std_8_wa16978' : 'xx0084D-WA16978',
						 'std_8_wa17181' : '1007189-WA17181',
						 'std_8_wa17266' : '1007575-WA17266',
						 'std_10' : 'xx0098-xx0096',
						 'std_10_03064' : '03064-03066',
						 'std_10_wa16488' : '1003359-WA16488',
						 'std_10_wa17102' : '5097-5104-WA17102',
						 'std_10_wa17106' : 'xx0098-WA17106',
						 'std_12' : '1005459-1005461'
						 }

aftfield_targets = {'std_aft' : '1005621-aft',
						 'std_wa17233' : '1005621-aft-WA17233',
						 'std_wa17319' : '02879-aft-WA17319'
						 }


################################################################################

env.ConfigStd(targets_config, config, ['config.xml'])
env.ConfigStd(targets_value, config)
env.ConfigHist(targets_hist, config)
env.ConfigReg(cifield_targets, aftfield_targets, config)

################################################################################

