I want to write a script that will search a tagpath recursively, and configure an alarm on each tag.
alarms = [{"name":"Alarm", "mode":"Equality", "setpointA":1}]
config = system.tag.getConfiguration("[default]", True)
#[{u'path': [default],
# u'tagType': Provider,
# u'tags': [{u'name': u'root level tag',
# u'path': root level tag,
# u'tagType': AtomicTag,
# u'value': 1,
# u'valueSource': u'memory'},
# {u'name': u'root level folder',
# u'path': root level folder,
# u'tagType': Folder,
# u'tags': [{u'name': u'nested tag',
# u'path': nested tag,
# u'tagType': AtomicTag,
# u'value': 2,
# u'valueSource': u'memory'},
# {u'name': u'nested folder',
# u'path': nested folder,
# u'tagType': Folder,
# u'tags': [{u'name': u'double nested tag',
# u'path': double nested tag,
# u'tagType': AtomicTag,
# u'valueSource': u'memory'}]}]},
# {u'path': _types_, u'tagType': Folder}]}]
for tag in config: # What type of iteration needs to go HERE so I hit every AtomicTag exactly once?
tag['alarms'] = alarms
system.tag.configure("[default]", config, "o")
What is the best way, in python, to script going through a tag hierarchy dictionary like that? I can do it with a recursive function like this:
def make_alarms(tagdict):
if "value" in tagdict.keys():
tagdict['alarms'] = [{"name":"Alarm", "mode":"Equality", "setpointA":1}]
return tagdict
if "tags" in tagdict.keys():
for tag in tagdict['tags']:
tag = make_alarms(tag)
return tagdict
Is this really the best way to accomplish this? Is there a simpler way?