casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
taskutil.py
Go to the documentation of this file.
00001 """
00002 Functions used in converting other functions into tasks.
00003 
00004 Use: import taskutil
00005 """
00006 
00007 import inspect, string, sys
00008 
00009 def get_global_namespace(ns_label='ipython console'):
00010     """
00011     Returns a dictionary of the globals in the lowest stacklevel containing
00012     ns_label in its label.
00013     """
00014     a=inspect.stack()
00015     stacklevel=0
00016     for k in range(len(a)):
00017         if (string.find(a[k][1], 'ipython console') > 0):
00018             stacklevel=k
00019 #            break             # Would this be an improvement?
00020     return sys._getframe(stacklevel).f_globals
00021 
00022 ## def update_myf(myf):
00023 ##     """
00024 ##     Fills unfilled parameters with defaults, and handles globals or user
00025 ##     override of arguments.
00026 ##     """
00027 ##     ###fill unfilled parameters with defaults
00028 ##     myf['update_params'](func=myf['taskname'], printtext=False)
00029 
00030 ##     #Handle globals or user over-ride of arguments
00031 ##     function_signature_defaults=dict(zip(radplot.func_code.co_varnames,
00032 ##                                          radplot.func_defaults))
00033 ##     for item in function_signature_defaults.iteritems():
00034 ##         key,val = item
00035 ##         keyVal = eval(key)
00036 ##         if (keyVal == None):
00037 ##             pass              # user hasn't set it - use global/default
00038 ##         else:
00039 ##             myf[key] = keyVal # user has set it - use over-ride
00040 
00041 def check_param_types(taskname, arg_desc):
00042     """
00043     Checks the values of taskname's arguments against their allowed types.
00044     Suitable for tasks without any menu parameters.
00045     Returns None if everything seems OK, and the exception otherwise (after
00046     printing an error message).
00047     """
00048     e = None
00049     arg_names = arg_desc.keys()
00050     arg_values=[]
00051     arg_types =[]
00052     for arg in arg_names:
00053         arg_values.append(arg_desc[arg][0])
00054         arg_types.append(arg_desc[arg][1])
00055     try:
00056         parameter_checktype(arg_names, arg_values, arg_types)
00057     except TypeError, e:
00058         print taskname, "-- TypeError: ", e
00059     except ValueError, e:
00060         print taskname, " -- OptionError: ", e
00061     return e
00062 
00063 def startlog(casalog, taskname):
00064     """
00065     Starts an entry in casalogger for taskname.
00066     """
00067     casalog.origin(taskname)
00068     casalog.post('')
00069     casalog.post('###############################################')
00070     casalog.post('###  Begin Task: ' + taskname + ' ' * (27 - len(taskname)) + '###')
00071     return
00072 
00073 def endlog(casalog, taskname):
00074     """
00075     Finishes an entry in casalogger for taskname.
00076     """
00077     casalog.post('###  End of task: ' + taskname + ' ' * (29 - len(taskname)) + '###')
00078     casalog.post('###############################################')
00079     casalog.post('')
00080     return