From 899daa8d83708479c77337cf7ad6823bbf7512c8 Mon Sep 17 00:00:00 2001 From: Douglas Clowes Date: Tue, 4 Nov 2014 10:36:38 +1100 Subject: [PATCH] Add defaults to .ini args and refactor add_driver --- site_ansto/instrument/util/gen_sct.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/site_ansto/instrument/util/gen_sct.py b/site_ansto/instrument/util/gen_sct.py index c7b0e01a..93b685d6 100755 --- a/site_ansto/instrument/util/gen_sct.py +++ b/site_ansto/instrument/util/gen_sct.py @@ -1617,7 +1617,7 @@ def put_mkDriver(MyDriver): txt += ['}'] emit(txt) -def put_postamble(MyDriver): +def put_add_driver(MyDriver): txt = [''] if 'add_args' in MyDriver: line = 'proc %s::add_driver {name device_class simulation_flag ip_address tcp_port %s} {' % (MyDriver['namespace'], MyDriver['add_args']) @@ -1664,7 +1664,10 @@ def put_postamble(MyDriver): txt += [' } catch_message ]'] txt += [' handle_exception ${catch_status} ${catch_message}'] txt += ['}'] - txt += [''] + emit(txt) + +def put_postamble(MyDriver): + txt = [''] txt += ['namespace eval %s {' % MyDriver['namespace']] txt += [' namespace export debug_threshold'] txt += [' namespace export debug_log'] @@ -1686,10 +1689,10 @@ def put_postamble(MyDriver): txt += [' set simulation_flag "[string tolower [SplitReply [%s]]]"' % MyDriver['simulation_group']] line = ' %s::add_driver ${name} "%s"' % (MyDriver['namespace'], MyDriver['class']) for arg in ['simulation_flag', 'ip_address', 'tcp_port']: - line += ' "${%s}"' % arg + line += ' ${%s}' % arg if 'add_args_lst' in MyDriver: for arg in MyDriver['add_args_lst']: - line += ' "${%s}"' % arg + line += ' ${%s}' % arg txt += [line] txt += ['}'] txt += [''] @@ -1760,11 +1763,20 @@ def put_read_config(MyDriver): if 'add_args_lst' in MyDriver: txt += [' set arg_list [list]'] txt += [' set missing_list [list]'] + default_list = [] + for arg in [key for key in MyDriver['add_args_lst'] if MyDriver['add_args_map'][key] is not None]: + default_list += [arg, MyDriver['add_args_map'][arg]] + if len(default_list) > 0: + txt += [' array unset default_map'] + txt += [' array set default_map [list %s]' % ' '.join(default_list)] txt += [' foreach arg {' + ' '.join(MyDriver['add_args_lst']) + '} {'] txt += [' if {[dict exists $u $arg]} {'] txt += [' lappend arg_list "[dict get $u $arg]"'] txt += [' } elseif {[dict exists $v $arg]} {'] txt += [' lappend arg_list "[dict get $v $arg]"'] + if len(default_list) > 0: + txt += [' } elseif {[info exists default_map($arg)]} {'] + txt += [' lappend arg_list $default_map($arg)'] txt += [' } else {'] txt += [' ${ns}::sics_log 9 "Missing configuration value $arg"'] txt += [' lappend missing_list $arg'] @@ -1844,6 +1856,7 @@ def generate_driver(MyDriver): put_preamble(MyDriver) put_standard_code(MyDriver) put_mkDriver(MyDriver) + put_add_driver(MyDriver) put_postamble(MyDriver) put_read_config(MyDriver) put_check_config(MyDriver)