diff --git a/site_ansto/instrument/util/gen_sct.py b/site_ansto/instrument/util/gen_sct.py index 52bda187..e87665a6 100755 --- a/site_ansto/instrument/util/gen_sct.py +++ b/site_ansto/instrument/util/gen_sct.py @@ -371,14 +371,26 @@ def p_driver_assignment(p): # def p_group(p): ''' - group : GROUP group_id EQUALS LBRACE group_statement_list RBRACE + group : named_group + | unnamed_group + ''' + p[0] = p[1] + +def p_named_group(p): + ''' + named_group : GROUP group_id EQUALS LBRACE group_statement_list RBRACE ''' p[0] = { 'Group' : [{'name': p[2]}] + p[5] } +def p_unnamed_group(p): + ''' + unnamed_group : GROUP EQUALS LBRACE group_statement_list RBRACE + ''' + p[0] = { 'Group' : [{'name': None}] + p[4] } + def p_group_id(p): ''' group_id : id_or_str - | empty ''' p[0] = p[1] @@ -433,6 +445,8 @@ def p_variable_statement(p): '''variable_statement : var_typ_ass | var_val_ass | property + | variable + | group ''' p[0] = p[1] @@ -629,11 +643,11 @@ def p_error(t): # # Utility functions # -def make_path(MyVar): +def make_path(MyVar, ch = '_'): path = MyVar['path'] if len(path) > 0: - path = path.replace('/', '_') - path += '_' + path = path.replace('/', ch) + path += ch path += MyVar['name'] return path @@ -745,6 +759,8 @@ def build_variable(MyDriver, p): print 'Variable:', p MyVar = {} MyVar['Property'] = {} + MyVar['Group'] = {} + MyVar['Variable'] = {} # Copy items for this variable for item in p: if Verbose: @@ -752,6 +768,10 @@ def build_variable(MyDriver, p): for key in item.keys(): if key == 'Property': MyVar['Property'][item[key][0]] = item[key][1] + elif key == 'Group': + pass # process nested groups at the bottom + elif key == 'Variable': + pass # process nested variables at the bottom else: MyVar[key] = item[key] # copy the defaults for missing items @@ -804,6 +824,30 @@ def build_variable(MyDriver, p): if node_type not in MyDriver['Permlink']: MyDriver['Permlink'][node_type] = [] MyDriver['Permlink'][node_type] += [make_path(MyVar)] + # Process the nested groups + for item in p: + for key in item.keys(): + if key == 'Group': + if Verbose: + print "SubGroup Item:", item[key] + push_context() + if len(ContextStack[ContextIndex]['path']) > 0: + ContextStack[ContextIndex]['path'] += '/' + ContextStack[ContextIndex]['path'] += MyVar['name'] + MyVar['Group'][item[key][0]['name']] = build_group(MyDriver, item[key]) + pop_context() + # Process the nested variables + for item in p: + for key in item.keys(): + if key == 'Variable': + if Verbose: + print "SubVariable Item:", item[key] + push_context() + if len(ContextStack[ContextIndex]['path']) > 0: + ContextStack[ContextIndex]['path'] += '/' + ContextStack[ContextIndex]['path'] += MyVar['name'] + MyVar['Variable'][item[key][0]['name']] = build_variable(MyDriver, item[key]) + pop_context() if Verbose: print '==>>MyVar:', MyVar return MyVar @@ -983,21 +1027,24 @@ def dump_driver_vars(vars, indent): global FunctionTypes global DriveableFunctionTypes for item in sorted(vars): - print indent + ' VAR %s = {' % item + print indent + 'VAR %s = {' % item Comments = ['name', 'path'] - Deferred = ['Property'] + Comments + FunctionTypes + DriveableFunctionTypes + Deferred = ['Property', 'Variable', 'Group'] + Comments + FunctionTypes + DriveableFunctionTypes for Comment in sorted(Comments): if Comment in vars[item]: - print indent + ' # %s = \'%s\'' % (Comment, vars[item][Comment]) + print indent + ' # %s = \'%s\'' % (Comment, vars[item][Comment]) for subitem in sorted([i for i in vars[item] if i not in Deferred]): - print indent + ' %s = \'%s\'' % (subitem, vars[item][subitem]) + print indent + ' %s = \'%s\'' % (subitem, vars[item][subitem]) for subitem in sorted([i for i in vars[item] if i in FunctionTypes]): - print indent + ' %s = \'%s\'' % (subitem, vars[item][subitem]) + print indent + ' %s = \'%s\'' % (subitem, vars[item][subitem]) for subitem in sorted([i for i in vars[item] if i in DriveableFunctionTypes]): - print indent + ' %s = \'%s\'' % (subitem, vars[item][subitem]) + print indent + ' %s = \'%s\'' % (subitem, vars[item][subitem]) for subitem in sorted([i for i in vars[item]['Property']]): - print indent + ' Property \'%s\' = \'%s\'' % (subitem, vars[item]['Property'][subitem]) - print indent + ' }' + print indent + ' Property \'%s\' = \'%s\'' % (subitem, vars[item]['Property'][subitem]) + # Dump the nested groups and vars + dump_driver_groups(vars[item]['Group'], indent + ' ') + dump_driver_vars(vars[item]['Variable'], indent + ' ') + print indent + '}' def dump_driver_groups(groups, indent): for item in sorted(groups): @@ -1015,7 +1062,7 @@ def dump_driver_groups(groups, indent): if 'GroupProperty' in groups[item]: for subitem in groups[item]['GroupProperty']: print indent + ' GroupProperty', subitem, '= \'%s\'' % groups[item]['GroupProperty'][subitem] - dump_driver_vars(groups[item]['Vars'], indent) + dump_driver_vars(groups[item]['Vars'], indent + ' ') dump_driver_groups(groups[item]['Groups'], indent + ' ') print indent + '}' @@ -1401,10 +1448,11 @@ def put_var(MyDriver, MyGroup, MyVar): readable_or_writeable = False txt = [] postfix = [] - if MyGroup['name']: - nodename = MyGroup['path'] + '/' + MyVar['name'] - else: - nodename = MyVar['name'] + nodename = make_path(MyVar, '/') + + # Debugging + #txt += ['# path = ' + MyVar['path']] + #txt += ['# name = ' + nodename] # Check driveable attributes are present if required if 'driveable' in MyVar and MyVar['driveable']: @@ -1536,6 +1584,20 @@ def put_var(MyDriver, MyGroup, MyVar): txt += [' hsetprop ${scobj_hpath}/%s simulated true' % nodename] txt += [' }'] + # Process nested groups + for grp in sorted(MyVar['Group']): + txt += [''] + infix = put_group(MyDriver, MyVar['Group'][grp]) + txt += infix + + # Process nested variables + for var in sorted(MyVar['Variable']): + txt += [''] + MySubVar = MyVar['Variable'][var] + infix, dfr = put_var(MyDriver, MyGroup, MySubVar) + txt += infix + postfix += dfr + if 'conditional' in MyVar: for idx, line in enumerate(txt): if len(line) > 0: @@ -1553,7 +1615,7 @@ def put_var(MyDriver, MyGroup, MyVar): def put_group(MyDriver, MyGroup): txt = [] - dfr = [] + postfix = [] if MyGroup['name']: txt += [''] txt += [' hfactory ${scobj_hpath}/%s plain spy none' % MyGroup['path']] @@ -1563,14 +1625,16 @@ def put_group(MyDriver, MyGroup): for var in sorted(MyGroup['Vars']): txt += [''] MyVar = MyGroup['Vars'][var] - infix, postfix = put_var(MyDriver, MyGroup, MyVar) + infix, dfr = put_var(MyDriver, MyGroup, MyVar) txt += infix - dfr += postfix + postfix += dfr if MyGroup['name']: if 'GroupProperty' in MyGroup: for key in sorted(MyGroup['GroupProperty']): txt += [' hsetprop ${scobj_hpath}/%s %s "%s"' % (MyGroup['path'], key, MyGroup['GroupProperty'][key])] + elif len(MyGroup['path']) > 0: + pass else: if 'GroupProperty' in MyGroup: txt += [''] @@ -1580,7 +1644,7 @@ def put_group(MyDriver, MyGroup): for grp in sorted(MyGroup['Groups']): txt += put_group(MyDriver, MyGroup['Groups'][grp]) - txt += dfr + txt += postfix if 'conditional' in MyGroup: for idx, line in enumerate(txt):