from xml.dom import minidom
__FUNCTION = 'override_args'
__LOGLEVEL_IN_FUNCTION = 'INFO'
from pprint import pprint
def xml_constraints_injector(func):
"""Decorator which loads constraints from a casatask XML file and apply them to the arguments of the decorated casatask.
This method is designed as decorator for task methods. It executes as below:
1. converts a constraints element of a CASA task XML to a Python code.
2. evaluates the code, then a Python function is generated.
3. executes the function and overrides task arguments to values defined by constraints tag.
a constraints tag of a CASA task XML:
<notequals type="string" value="">
<default param="timespan"><value type="string"/></default>
<default param="nfit"><value type="vector"><value>0</value></value></default>
<default param="thresh"><value>5.0</value></default>
<default param="avg_limit"><value>4</value></default>
<default param="minwidth"><value>4</value></default>
<default param="edge"><value type="vector"><value>0</value></value></default>
<equals value="interact">
<default param="nfit"><value type="vector"><value>0</value></value></default>
generated Python function code from the above XML:
def override_args(_a, _d, _s): # _a: position args based on *args
# _d: dict[key: position name, val: corresponding position index of a key]
# to use to get a position index of args by position name
# _s: boolean array, it is the same length as the position args,
# and the positions of user-supplied arguments are set to True
if _d.get('timebin') is not None and _a[_d['timebin']] != '':
if _d.get('timespan') is not None and _s[_d['timespan']] is False and _a[_d['timespan']] == "":
casatasks.casalog.post("overrode argument: timespan -> ''", "INFO")