diff --git a/site_ansto/instrument/util/gen_sct.py b/site_ansto/instrument/util/gen_sct.py index e895627b..0d40d60c 100755 --- a/site_ansto/instrument/util/gen_sct.py +++ b/site_ansto/instrument/util/gen_sct.py @@ -529,25 +529,28 @@ def build_variable(MyDriver, p): def build_group(MyDriver, p): if Verbose: - print 'Group:', p + print 'Group:', p[0]['name'], p push_context() MyGroup = {} MyGroup['Groups'] = {} MyGroup['Vars'] = {} - for item in [i for i in p if 'Variable' not in i]: - if Verbose: - print "Group Item:", item - for key in item.keys(): - MyGroup[key] = item[key] - if key in ContextStack[ContextIndex]: - ContextStack[ContextIndex][key] = item[key] - if MyGroup['name']: + # the sequence of both variables and non-variables is significant + # Therefore, they have to be processed in a single sequence + if p[0]['name']: if len(ContextStack[ContextIndex]['path']) > 0: ContextStack[ContextIndex]['path'] += '/' - ContextStack[ContextIndex]['path'] += MyGroup['name'] - for item in [i for i in p if 'Variable' in i]: - MyVar = build_variable(MyDriver, item['Variable']) - MyGroup['Vars'][MyVar['name']] = MyVar + ContextStack[ContextIndex]['path'] += p[0]['name'] + for item in p: + if 'Variable' in item: + MyVar = build_variable(MyDriver, item['Variable']) + MyGroup['Vars'][MyVar['name']] = MyVar + else: + if Verbose: + print "Group Item:", item + for key in item: + MyGroup[key] = item[key] + if key in ContextStack[ContextIndex]: + ContextStack[ContextIndex][key] = item[key] pop_context() return MyGroup @@ -649,14 +652,14 @@ def put_preamble(MyDriver): def put_write_function(MyDriver, func): txt = [''] - txt += ['proc %s::%s {tc_root nextState cmd} {' % (MyDriver['namespace'], func)] - txt += [' debug_log 1 "%s tc_root=${tc_root} sct=[sct] cmd=${cmd}"' % func] + txt += ['proc %s::%s {tc_root nextState cmd_str} {' % (MyDriver['namespace'], func)] + txt += [' debug_log 1 "%s tc_root=${tc_root} sct=[sct] cmd=${cmd_str}"' % func] txt += [' if { [hpropexists [sct] geterror] } {'] txt += [' hdelprop [sct] geterror'] txt += [' }'] txt += [' set par [sct target]'] - txt += [' set cmd "${cmd}${par}"'] - if func in MyDriver['Funcs']: + txt += [' set cmd "${cmd_str}${par}"'] + if func in MyDriver['Funcs'] and len(MyDriver['Funcs'][func]['text']) > 0: txt += ['# hook code starts'] txt += MyDriver['Funcs'][func]['text'] txt += ['# hook code ends'] @@ -670,7 +673,7 @@ def put_check_function(MyDriver, func): txt = [''] txt += ['proc %s::%s {tc_root} {' % (MyDriver['namespace'], func)] txt += [' debug_log 1 "%s tc_root=${tc_root} sct=[sct] resp=[sct result]"' % func] - if func in MyDriver['Funcs']: + if func in MyDriver['Funcs'] and len(MyDriver['Funcs'][func]['text']) > 0: txt += ['# hook code starts'] txt += MyDriver['Funcs'][func]['text'] txt += ['# hook code ends'] @@ -680,12 +683,13 @@ def put_check_function(MyDriver, func): def put_fetch_function(MyDriver, func): txt = [''] - txt += ['proc %s::%s {tc_root nextState cmd} {' % (MyDriver['namespace'], func)] - txt += [' debug_log 1 "%s tc_root=${tc_root} sct=[sct] cmd=${cmd}"' % func] + txt += ['proc %s::%s {tc_root nextState cmd_str} {' % (MyDriver['namespace'], func)] + txt += [' debug_log 1 "%s tc_root=${tc_root} sct=[sct] cmd=${cmd_str}"' % func] txt += [' if { [hpropexists [sct] geterror] } {'] txt += [' hdelprop [sct] geterror'] txt += [' }'] - if func in MyDriver['Funcs']: + txt += [' set cmd "${cmd_str}"'] + if func in MyDriver['Funcs'] and len(MyDriver['Funcs'][func]['text']) > 0: txt += ['# hook code starts'] txt += MyDriver['Funcs'][func]['text'] txt += ['# hook code ends'] @@ -709,7 +713,7 @@ def put_read_function(MyDriver, func): txt += [' sct geterror "${data}"'] txt += [' return -code error "[sct geterror]"'] txt += [' }'] - if func in MyDriver['Funcs']: + if func in MyDriver['Funcs'] and len(MyDriver['Funcs'][func]['text']) > 0: txt += ['# hook code starts'] txt += MyDriver['Funcs'][func]['text'] txt += ['# hook code ends'] @@ -745,7 +749,7 @@ def put_check(MyDriver): txt += [' # upperlimit not set, use target'] txt += [' set hilimit [sct target]'] txt += [' }'] - if 'check' in MyDriver['Funcs']: + if 'check' in MyDriver['Funcs'] and len(MyDriver['Funcs']['check']['text']) > 0: txt += ['# hook code starts'] txt += MyDriver['Funcs']['check']['text'] txt += ['# hook code ends'] @@ -774,7 +778,7 @@ def put_checklimits(MyDriver): txt += [' # upperlimit not set, use target'] txt += [' set hilimit [sct target]'] txt += [' }'] - if 'checklimits' in MyDriver['Funcs']: + if 'checklimits' in MyDriver['Funcs'] and len(MyDriver['Funcs']['checklimits']['text']) > 0: txt += ['# hook code starts'] txt += MyDriver['Funcs']['checklimits']['text'] txt += ['# hook code ends'] @@ -790,7 +794,7 @@ def put_checkstatus(MyDriver): txt = [''] txt += ['# checkstatus function for driveable interface'] txt += ['proc %s::checkstatus {tc_root} {' % MyDriver['namespace']] - if 'checkstatus' in MyDriver['Funcs']: + if 'checkstatus' in MyDriver['Funcs'] and len(MyDriver['Funcs']['checkstatus']['text']) > 0: txt += ['# hook code starts'] txt += MyDriver['Funcs']['checkstatus']['text'] txt += ['# hook code ends'] @@ -814,7 +818,7 @@ def put_halt(MyDriver): txt += ['proc %s::halt {tc_root} {' % MyDriver['namespace']] txt += [' debug_log 1 "halt tc_root=${tc_root} sct=[sct] driving=[sct driving]"'] txt += [' ### TODO hset [sct] [hval [sct]]'] - if 'halt' in MyDriver['Funcs']: + if 'halt' in MyDriver['Funcs'] and len(MyDriver['Funcs']['halt']['text']) > 0: txt += ['# hook code starts'] txt += MyDriver['Funcs']['halt']['text'] txt += ['# hook code ends'] @@ -1080,7 +1084,11 @@ def put_group(MyDriver, MyGroup): def put_mk_sct_driver(MyDriver): txt = [''] - txt += ['proc %s::mk_sct_%s { sct_controller name %s } {' % (MyDriver['namespace'], MyDriver['name'], MyDriver['make_args'])] + if 'make_args' in MyDriver: + line = 'proc %s::mk_sct_%s { sct_controller name %s } {' % (MyDriver['namespace'], MyDriver['name'], MyDriver['make_args']) + else: + line = 'proc %s::mk_sct_%s { sct_controller name } {' % (MyDriver['namespace'], MyDriver['name']) + txt += [line] txt += [' debug_log 1 "mk_sct_%s for ${name}"' % MyDriver['name']] txt += [' set ns "[namespace current]"'] txt += [' set catch_status [ catch {'] @@ -1154,7 +1162,11 @@ def put_postamble(MyDriver): txt += [' namespace export mk_sct_%s' % MyDriver['name']] txt += ['}'] txt += [''] - txt += ['proc add_%s {name IP port %s} {' % (MyDriver['name'], MyDriver['add_args'])] + if 'add_args' in MyDriver: + line = 'proc add_%s {name IP port %s} {' % (MyDriver['name'], MyDriver['add_args']) + else: + line = 'proc add_%s {name IP port} {' % MyDriver['name'] + txt += [line] txt += [' set ns "%s"' % MyDriver['namespace']] txt += [' ${ns}::debug_log 1 "add_%s ${name} ${IP} ${port}"' % MyDriver['name']] txt += [' if {[SplitReply [%s]]=="false"} {' % MyDriver['simulation_group']] @@ -1162,17 +1174,26 @@ def put_postamble(MyDriver): txt += [' ${ns}::debug_log 1 "makesctcontroller sct_${name} aqadapter ${port}"'] txt += [' makesctcontroller sct_${name} aqadapter ${port}'] txt += [' } else {'] - txt += [' ${ns}::debug_log 1 "makesctcontroller sct_${name} %s ${IP}:${port} %s"' % (MyDriver['protocol'], MyDriver['protocol_args'].replace('\\', '\\\\').replace('"', '\\"'))] - txt += [' makesctcontroller sct_${name} %s ${IP}:${port} %s' % (MyDriver['protocol'], MyDriver['protocol_args'])] + if 'protocol_args' in MyDriver: + protocol_args = MyDriver['protocol_args'].replace('\\', '\\\\').replace('"', '\\"') + txt += [' ${ns}::debug_log 1 "makesctcontroller sct_${name} %s ${IP}:${port} %s"' % (MyDriver['protocol'], protocol_args)] + txt += [' makesctcontroller sct_${name} %s ${IP}:${port} %s' % (MyDriver['protocol'], MyDriver['protocol_args'])] + else: + txt += [' ${ns}::debug_log 1 "makesctcontroller sct_${name} %s ${IP}:${port}"' % MyDriver['protocol']] + txt += [' makesctcontroller sct_${name} %s ${IP}:${port}' % MyDriver['protocol']] txt += [' }'] txt += [' }'] make_args = '' - for arg in MyDriver['make_args'].split(): - if len(make_args) > 0: - make_args += ' ' - make_args += '${' + arg + '}' - txt += [' ${ns}::debug_log 1 "mk_sct_%s sct_${name} ${name} %s"' % (MyDriver['name'], make_args)] - txt += [' ${ns}::mk_sct_%s sct_${name} ${name} %s' % (MyDriver['name'], make_args)] + if 'make_args' in MyDriver: + for arg in MyDriver['make_args'].split(): + if len(make_args) > 0: + make_args += ' ' + make_args += '${' + arg + '}' + txt += [' ${ns}::debug_log 1 "mk_sct_%s sct_${name} ${name} %s"' % (MyDriver['name'], make_args)] + txt += [' ${ns}::mk_sct_%s sct_${name} ${name} %s' % (MyDriver['name'], make_args)] + else: + txt += [' ${ns}::debug_log 1 "mk_sct_%s sct_${name} ${name}"' % MyDriver['name']] + txt += [' ${ns}::mk_sct_%s sct_${name} ${name}' % MyDriver['name']] # TODO #txt += [' ${ns}::debug_log 1 "makesctemon ${name} /sics/${name}/emon/monmode /sics/${name}/emon/isintol /sics/${name}/emon/errhandler"'] # txt += [' makesctemon ${name} /sics/${name}/emon/monmode /sics/${name}/emon/isintol /sics/${name}/emon/errhandler']