Handle nested GROUP and VAR constructs in gen_sct.py

This commit is contained in:
Douglas Clowes
2015-03-13 15:30:16 +11:00
parent 125ebc9f6d
commit 6ef97197ee

View File

@ -371,14 +371,26 @@ def p_driver_assignment(p):
# #
def p_group(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] } 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): def p_group_id(p):
''' '''
group_id : id_or_str group_id : id_or_str
| empty
''' '''
p[0] = p[1] p[0] = p[1]
@ -433,6 +445,8 @@ def p_variable_statement(p):
'''variable_statement : var_typ_ass '''variable_statement : var_typ_ass
| var_val_ass | var_val_ass
| property | property
| variable
| group
''' '''
p[0] = p[1] p[0] = p[1]
@ -629,11 +643,11 @@ def p_error(t):
# #
# Utility functions # Utility functions
# #
def make_path(MyVar): def make_path(MyVar, ch = '_'):
path = MyVar['path'] path = MyVar['path']
if len(path) > 0: if len(path) > 0:
path = path.replace('/', '_') path = path.replace('/', ch)
path += '_' path += ch
path += MyVar['name'] path += MyVar['name']
return path return path
@ -745,6 +759,8 @@ def build_variable(MyDriver, p):
print 'Variable:', p print 'Variable:', p
MyVar = {} MyVar = {}
MyVar['Property'] = {} MyVar['Property'] = {}
MyVar['Group'] = {}
MyVar['Variable'] = {}
# Copy items for this variable # Copy items for this variable
for item in p: for item in p:
if Verbose: if Verbose:
@ -752,6 +768,10 @@ def build_variable(MyDriver, p):
for key in item.keys(): for key in item.keys():
if key == 'Property': if key == 'Property':
MyVar['Property'][item[key][0]] = item[key][1] 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: else:
MyVar[key] = item[key] MyVar[key] = item[key]
# copy the defaults for missing items # copy the defaults for missing items
@ -804,6 +824,30 @@ def build_variable(MyDriver, p):
if node_type not in MyDriver['Permlink']: if node_type not in MyDriver['Permlink']:
MyDriver['Permlink'][node_type] = [] MyDriver['Permlink'][node_type] = []
MyDriver['Permlink'][node_type] += [make_path(MyVar)] 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: if Verbose:
print '==>>MyVar:', MyVar print '==>>MyVar:', MyVar
return MyVar return MyVar
@ -983,9 +1027,9 @@ def dump_driver_vars(vars, indent):
global FunctionTypes global FunctionTypes
global DriveableFunctionTypes global DriveableFunctionTypes
for item in sorted(vars): for item in sorted(vars):
print indent + ' VAR %s = {' % item print indent + 'VAR %s = {' % item
Comments = ['name', 'path'] Comments = ['name', 'path']
Deferred = ['Property'] + Comments + FunctionTypes + DriveableFunctionTypes Deferred = ['Property', 'Variable', 'Group'] + Comments + FunctionTypes + DriveableFunctionTypes
for Comment in sorted(Comments): for Comment in sorted(Comments):
if Comment in vars[item]: if Comment in vars[item]:
print indent + ' # %s = \'%s\'' % (Comment, vars[item][Comment]) print indent + ' # %s = \'%s\'' % (Comment, vars[item][Comment])
@ -997,7 +1041,10 @@ def dump_driver_vars(vars, indent):
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']]): for subitem in sorted([i for i in vars[item]['Property']]):
print indent + ' Property \'%s\' = \'%s\'' % (subitem, vars[item]['Property'][subitem]) print indent + ' Property \'%s\' = \'%s\'' % (subitem, vars[item]['Property'][subitem])
print indent + ' }' # 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): def dump_driver_groups(groups, indent):
for item in sorted(groups): for item in sorted(groups):
@ -1015,7 +1062,7 @@ def dump_driver_groups(groups, indent):
if 'GroupProperty' in groups[item]: if 'GroupProperty' in groups[item]:
for subitem in groups[item]['GroupProperty']: for subitem in groups[item]['GroupProperty']:
print indent + ' GroupProperty', subitem, '= \'%s\'' % groups[item]['GroupProperty'][subitem] 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 + ' ') dump_driver_groups(groups[item]['Groups'], indent + ' ')
print indent + '}' print indent + '}'
@ -1401,10 +1448,11 @@ def put_var(MyDriver, MyGroup, MyVar):
readable_or_writeable = False readable_or_writeable = False
txt = [] txt = []
postfix = [] postfix = []
if MyGroup['name']: nodename = make_path(MyVar, '/')
nodename = MyGroup['path'] + '/' + MyVar['name']
else: # Debugging
nodename = MyVar['name'] #txt += ['# path = ' + MyVar['path']]
#txt += ['# name = ' + nodename]
# Check driveable attributes are present if required # Check driveable attributes are present if required
if 'driveable' in MyVar and MyVar['driveable']: 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 += [' hsetprop ${scobj_hpath}/%s simulated true' % nodename]
txt += [' }'] 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: if 'conditional' in MyVar:
for idx, line in enumerate(txt): for idx, line in enumerate(txt):
if len(line) > 0: if len(line) > 0:
@ -1553,7 +1615,7 @@ def put_var(MyDriver, MyGroup, MyVar):
def put_group(MyDriver, MyGroup): def put_group(MyDriver, MyGroup):
txt = [] txt = []
dfr = [] postfix = []
if MyGroup['name']: if MyGroup['name']:
txt += [''] txt += ['']
txt += [' hfactory ${scobj_hpath}/%s plain spy none' % MyGroup['path']] 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']): for var in sorted(MyGroup['Vars']):
txt += [''] txt += ['']
MyVar = MyGroup['Vars'][var] MyVar = MyGroup['Vars'][var]
infix, postfix = put_var(MyDriver, MyGroup, MyVar) infix, dfr = put_var(MyDriver, MyGroup, MyVar)
txt += infix txt += infix
dfr += postfix postfix += dfr
if MyGroup['name']: if MyGroup['name']:
if 'GroupProperty' in MyGroup: if 'GroupProperty' in MyGroup:
for key in sorted(MyGroup['GroupProperty']): for key in sorted(MyGroup['GroupProperty']):
txt += [' hsetprop ${scobj_hpath}/%s %s "%s"' % (MyGroup['path'], key, MyGroup['GroupProperty'][key])] txt += [' hsetprop ${scobj_hpath}/%s %s "%s"' % (MyGroup['path'], key, MyGroup['GroupProperty'][key])]
elif len(MyGroup['path']) > 0:
pass
else: else:
if 'GroupProperty' in MyGroup: if 'GroupProperty' in MyGroup:
txt += [''] txt += ['']
@ -1580,7 +1644,7 @@ def put_group(MyDriver, MyGroup):
for grp in sorted(MyGroup['Groups']): for grp in sorted(MyGroup['Groups']):
txt += put_group(MyDriver, MyGroup['Groups'][grp]) txt += put_group(MyDriver, MyGroup['Groups'][grp])
txt += dfr txt += postfix
if 'conditional' in MyGroup: if 'conditional' in MyGroup:
for idx, line in enumerate(txt): for idx, line in enumerate(txt):