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")
if _d.get('fitmode') is not None and _a[_d['fitmode']] == 'list':
if _d.get('nfit') is not None and _s[_d['nfit']] is False and _a[_d['nfit']] == "":
casatasks.casalog.post("overrode argument: nfit -> [0]", "INFO")
if _d.get('fitmode') is not None and _a[_d['fitmode']] == 'auto':
if _d.get('thresh') is not None and _s[_d['thresh']] is False and _a[_d['thresh']] == "":
casatasks.casalog.post("overrode argument: thresh -> 5.0", "INFO")
if _d.get('avg_limit') is not None and _s[_d['avg_limit']] is False and _a[_d['avg_limit']] == "":
casatasks.casalog.post("overrode argument: avg_limit -> 4", "INFO")
if _d.get('minwidth') is not None and _s[_d['minwidth']] is False and _a[_d['minwidth']] == "":
casatasks.casalog.post("overrode argument: minwidth -> 4", "INFO")
if _d.get('edge') is not None and _s[_d['edge']] is False and _a[_d['edge']] == "":
casatasks.casalog.post("overrode argument: edge -> [0]", "INFO")
if _d.get('fitmode') is not None and _a[_d['fitmode']] == 'interact':
if _d.get('nfit') is not None and _s[_d['nfit']] is False and _a[_d['nfit']] == "":
casatasks.casalog.post("overrode argument: nfit -> [0]", "INFO")
Note: handling of <kwarg/> tag of task XML files
Subparameters whose default value is the empty string '' - but where the empty string means in fact that
the real default value must be set to some non-empty string - require special care. One must be able to
determine whether the empty string was user-supplied or not.
To make this determination possible, the <kwarg/> tag must be set in the <param> tag definition of such
parameters in the task XML file. This is currently the case only for parameter 'intent' of task sdcal,