Browse Source

BF: Thanks to insights from Rogerio -- handle both aInfo and cInfo and replicate cmds if no key provided

_tent/ipv6_via_aInfo
Yaroslav Halchenko 14 years ago
parent
commit
b14257f27f
  1. 46
      server/action.py

46
server/action.py

@ -81,13 +81,13 @@ class Action:
# @param value the property value # @param value the property value
def setCInfo(self, key, value): def setCInfo(self, key, value):
if '/' in value: if '/' in key:
logSys.debug("Evaluating the value to dict") logSys.debug("Evaluating the value to dict")
try: try:
value = eval("dict(%s)" % value) value = eval("dict(%s)" % value)
except Exception, e: except Exception, e:
logSys.error("Failed to evaluate value %r for %s as dict" logSys.error("Failed to evaluate value dict(%s) for %s "
% (value, key)) "as dict due to %r" % (value, key, e))
logSys.debug("Set cinfo %s = %r" % (key, value)) logSys.debug("Set cinfo %s = %r" % (key, value))
self.__cInfo[key] = value self.__cInfo[key] = value
@ -247,28 +247,36 @@ class Action:
for tag in aInfo: for tag in aInfo:
# simple replacement string or a dictionary # simple replacement string or a dictionary
val = aInfo[tag] val = aInfo[tag]
subs = [(tag, val)] # by default just 1 substitution pair
if '/' in tag: if '/' in tag:
# dict Info and we should take after '/' as the key # dict Info and we should take after '/' as the key
# which would determine which actual tag to take from # which would determine which actual tag to take from
# aInfo # aInfo
tag_, key_tag = tag.split('/', 1) tag_, key_tag = tag.split('/', 1)
if not key_tag in aInfo:
logSys.error(
"Failed to find information for key tag %s among %s. "
"Tag %s was ignored" % (key_tag, aInfo.keys(), tag))
continue
if not isinstance(val, dict): if not isinstance(val, dict):
logSys.error("Tags defined as X/Y must contain dictionary " logSys.error("Tags defined as X/Y must contain dictionary "
"entries. Got %r. Tag %s was ignored" "entries. Got %r. Tag %s was ignored"
% (val, tag)) % (val, tag))
continue continue
if not key_tag in aInfo:
# Need to duplicate for all known and unique
logSys.debug(
"No information for key tag %s among %s. "
"Duplicating the string for all keys"
% (key_tag, aInfo.keys()))
subs = [(tag_, v) for v in val.itervalues()]
else:
# There is a key
if not aInfo[key_tag] in val: if not aInfo[key_tag] in val:
logSys.error("There is no %s in %r. Tag %s was ignored" logSys.error("There is no value for %s in %r. Tag %s was ignored"
% (aInfo[key_tag], val, tag)) % (aInfo[key_tag], val, tag))
continue continue
tag = tag_ # TODO: pylint would scream here I guess subs = [(tag_, val[aInfo[key_tag]])]
val = aInfo[tag][aInfo[key_tag]] strings = [string.replace('<' + t + '>', str(v)) for t,v in subs]
string = string.replace('<' + tag + '>', str(val)) # only unique, so we do not run the same command multiple times,
# if that tag wasn't even in place. anyways order of them is arbitrary
# due to arbitrary order of keys in a val dict above
string = '; '.join(list(set(strings)))
# New line # New line
string = string.replace("<br>", '\n') string = string.replace("<br>", '\n')
return string return string
@ -306,14 +314,14 @@ class Action:
logSys.fatal("Unable to restore environment") logSys.fatal("Unable to restore environment")
return False return False
# Replace tags # Compose ultimate untagging dictionary with aInfo overriding
if not aInfo == None: # present in cInfo
realCmd = Action.replaceTag(cmd, aInfo) allInfo = self.__cInfo.copy()
else: if aInfo:
realCmd = cmd allInfo.update(aInfo)
# Replace static fields # Replace tags
realCmd = Action.replaceTag(realCmd, self.__cInfo) realCmd = Action.replaceTag(cmd, allInfo)
return Action.executeCmd(realCmd) return Action.executeCmd(realCmd)

Loading…
Cancel
Save