casa
$Rev:20696$
|
00001 # 00002 # This file was generated using xslt from its XML file 00003 # 00004 # Copyright 2008, Associated Universities Inc., Washington DC 00005 # 00006 import sys 00007 import os 00008 #from casac import * 00009 import casac 00010 import string 00011 import time 00012 import inspect 00013 import gc 00014 import numpy 00015 from odict import odict 00016 from taskmanager import tm 00017 from task_clearstat import clearstat 00018 class clearstat_cli_: 00019 __name__ = "clearstat" 00020 __async__ = {} 00021 rkey = None 00022 i_am_a_casapy_task = None 00023 # The existence of the i_am_a_casapy_task attribute allows help() 00024 # (and other) to treat casapy tasks as a special case. 00025 00026 def __init__(self) : 00027 self.__bases__ = (clearstat_cli_,) 00028 self.__doc__ = self.__call__.__doc__ 00029 00030 self.parameters={ 'async':None} 00031 00032 00033 def result(self, key=None): 00034 #### here we will scan the task-ids in __async__ 00035 #### and add any that have completed... 00036 if key is not None and self.__async__.has_key(key) and self.__async__[key] is not None: 00037 ret = tm.retrieve(self.__async__[key]) 00038 if ret['state'] == "done" : 00039 self.__async__[key] = None 00040 elif ret['state'] == 'crashed' : 00041 self.__async__[key] = None 00042 return ret 00043 return None 00044 00045 00046 def __call__(self, async=None): 00047 00048 """Clear all autolock locks 00049 00050 This task is useful if another task that is running indicates 00051 that it is trying to obtain a lock on a file. 00052 00053 Typing 'go clearstat()' will not change the current task being scrutinized 00054 Typing 'clearstat()' will change the current task assignment to clearpstat 00055 which is generally not what is desired. 00056 00057 00058 """ 00059 if not hasattr(self, "__globals__") or self.__globals__ == None : 00060 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00061 #casac = self.__globals__['casac'] 00062 casalog = self.__globals__['casalog'] 00063 #casalog = casac.casac.logsink() 00064 self.__globals__['__last_task'] = 'clearstat' 00065 self.__globals__['taskname'] = 'clearstat' 00066 ### 00067 self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__) 00068 ### 00069 ### 00070 #Handle globals or user over-ride of arguments 00071 # 00072 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00073 useLocalDefaults = False 00074 00075 for item in function_signature_defaults.iteritems(): 00076 key,val = item 00077 keyVal = eval(key) 00078 if (keyVal == None): 00079 #user hasn't set it - use global/default 00080 pass 00081 else: 00082 #user has set it - use over-ride 00083 if (key != 'self') : 00084 useLocalDefaults = True 00085 00086 myparams = {} 00087 if useLocalDefaults : 00088 for item in function_signature_defaults.iteritems(): 00089 key,val = item 00090 keyVal = eval(key) 00091 exec('myparams[key] = keyVal') 00092 self.parameters[key] = keyVal 00093 if (keyVal == None): 00094 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00095 keyVal = eval(key) 00096 if(type(keyVal) == dict) : 00097 if len(keyVal) > 0 : 00098 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00099 else : 00100 exec('myparams[key] = ' + key + ' = {}') 00101 00102 else : 00103 async = self.parameters['async'] 00104 00105 00106 result = None 00107 00108 # 00109 # The following is work around to avoid a bug with current python translation 00110 # 00111 mytmp = {} 00112 00113 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00114 trec = casac.casac.utils().torecord(pathname+'clearstat.xml') 00115 00116 casalog.origin('clearstat') 00117 try : 00118 #if not trec.has_key('clearstat') or not casac.casac.utils().verify(mytmp, trec['clearstat']) : 00119 #return False 00120 00121 casac.casac.utils().verify(mytmp, trec['clearstat'], True) 00122 scriptstr=[''] 00123 saveinputs = self.__globals__['saveinputs'] 00124 saveinputs('clearstat', 'clearstat.last', myparams, self.__globals__,scriptstr=scriptstr) 00125 if async : 00126 count = 0 00127 keybase = time.strftime("%y%m%d.%H%M%S") 00128 key = keybase + "_" + str(count) 00129 while self.__async__.has_key(key) : 00130 count += 1 00131 key = keybase + "_" + str(count) 00132 result = tm.execute('clearstat', ) 00133 print "Use: " 00134 print " tm.retrieve(return_value) # to retrieve the status" 00135 print 00136 self.rkey = key 00137 self.__async__[key] = result 00138 else : 00139 tname = 'clearstat' 00140 spaces = ' '*(18-len(tname)) 00141 casalog.post('\n##########################################'+ 00142 '\n##### Begin Task: ' + tname + spaces + ' #####') 00143 casalog.post(scriptstr[1][1:]+'\n', 'INFO') 00144 result = clearstat() 00145 casalog.post('##### End Task: ' + tname + ' ' + spaces + ' #####'+ 00146 '\n##########################################') 00147 00148 except Exception, instance: 00149 if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) : 00150 raise 00151 else : 00152 #print '**** Error **** ',instance 00153 tname = 'clearstat' 00154 casalog.post('An error occurred running task '+tname+'.', 'ERROR') 00155 pass 00156 00157 gc.collect() 00158 return result 00159 # 00160 # 00161 # 00162 def paramgui(self, useGlobals=True, ipython_globals=None): 00163 """ 00164 Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00165 """ 00166 import paramgui 00167 if not hasattr(self, "__globals__") or self.__globals__ == None : 00168 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00169 00170 if useGlobals: 00171 if ipython_globals == None: 00172 myf=self.__globals__ 00173 else: 00174 myf=ipython_globals 00175 00176 paramgui.setGlobals(myf) 00177 else: 00178 paramgui.setGlobals({}) 00179 00180 paramgui.runTask('clearstat', myf['_ip']) 00181 paramgui.setGlobals({}) 00182 00183 # 00184 # 00185 # 00186 def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None): 00187 if not hasattr(self, "__globals__") or self.__globals__ == None : 00188 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00189 if ipython_globals == None: 00190 myf=self.__globals__ 00191 else: 00192 myf=ipython_globals 00193 00194 a = odict() 00195 00196 a['async']=False 00197 00198 ### This function sets the default values but also will return the list of 00199 ### parameters or the default value of a given parameter 00200 if(param == None): 00201 myf['__set_default_parameters'](a) 00202 elif(param == 'paramkeys'): 00203 return a.keys() 00204 else: 00205 if(paramvalue==None and subparam==None): 00206 if(a.has_key(param)): 00207 return a[param] 00208 else: 00209 return self.itsdefault(param) 00210 else: 00211 retval=a[param] 00212 if(type(a[param])==dict): 00213 for k in range(len(a[param])): 00214 valornotval='value' 00215 if(a[param][k].has_key('notvalue')): 00216 valornotval='notvalue' 00217 if((a[param][k][valornotval])==paramvalue): 00218 retval=a[param][k].copy() 00219 retval.pop(valornotval) 00220 if(subparam != None): 00221 if(retval.has_key(subparam)): 00222 retval=retval[subparam] 00223 else: 00224 retval=self.itsdefault(subparam) 00225 else: 00226 retval=self.itsdefault(subparam) 00227 return retval 00228 00229 00230 # 00231 # 00232 def check_params(self, param=None, value=None, ipython_globals=None): 00233 if ipython_globals == None: 00234 myf=self.__globals__ 00235 else: 00236 myf=ipython_globals 00237 # print 'param:', param, 'value:', value 00238 try : 00239 if str(type(value)) != "<type 'instance'>" : 00240 value0 = value 00241 value = myf['cu'].expandparam(param, value) 00242 matchtype = False 00243 if(type(value) == numpy.ndarray): 00244 if(type(value) == type(value0)): 00245 myf[param] = value.tolist() 00246 else: 00247 #print 'value:', value, 'value0:', value0 00248 #print 'type(value):', type(value), 'type(value0):', type(value0) 00249 myf[param] = value0 00250 if type(value0) != list : 00251 matchtype = True 00252 else : 00253 myf[param] = value 00254 value = myf['cu'].verifyparam({param:value}) 00255 if matchtype: 00256 value = False 00257 except Exception, instance: 00258 #ignore the exception and just return it unchecked 00259 myf[param] = value 00260 return value 00261 # 00262 # 00263 def description(self, key='clearstat', subkey=None): 00264 desc={'clearstat': 'Clear all autolock locks', 00265 00266 'async': 'If true the taskname must be started using clearstat(...)' 00267 } 00268 00269 if(desc.has_key(key)) : 00270 return desc[key] 00271 00272 def itsdefault(self, paramname) : 00273 a = {} 00274 00275 #a = sys._getframe(len(inspect.stack())-1).f_globals 00276 00277 if a.has_key(paramname) : 00278 return a[paramname] 00279 clearstat_cli = clearstat_cli_()