<s> from copy import deepcopy , copy <EOL> import logging <EOL> from treedict import TreeDict <EOL> import re <EOL> from axisproxy import AxisProxy <EOL> class PModule : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def name ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . _name <EOL> @ classmethod <EOL> def preprocessParameters ( cls , local_parameters ) : <EOL> """<STR_LIT>""" <EOL> return local_parameters <EOL> @ classmethod <EOL> def _preprocessParameters ( cls , parameters ) : <EOL> pb = parameters [ cls . _name ] <EOL> for t in [ [ ] , [ pb ] , [ pb , parameters . copy ( freeze = True ) ] ] : <EOL> try : <EOL> p = cls . preprocessParameters ( * t ) <EOL> break <EOL> except TypeError , te : <EOL> if "<STR_LIT>" in str ( te ) : <EOL> continue <EOL> else : <EOL> raise <EOL> return p if p is not None else parameters [ cls . _name ] <EOL> @ classmethod <EOL> def _getDependencies ( cls , parameters ) : <EOL> def getDep ( dep_type ) : <EOL> dep_attr = dep_type + '<STR_LIT>' <EOL> def process_dependency ( dl ) : <EOL> def wrong_type ( s ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> % ( dep_type , str ( type ( s ) ) , cls . _name ) ) <EOL> def clean ( s ) : <EOL> if type ( s ) is str : <EOL> return s . strip ( ) . lower ( ) <EOL> elif not getattr ( s , "<STR_LIT>" , False ) : <EOL> wrong_type ( s ) <EOL> return s <EOL> if type ( dl ) is str or getattr ( dl , "<STR_LIT>" , False ) : <EOL> s = [ dl ] <EOL> elif type ( dl ) in [ list , tuple , set ] : <EOL> s = list ( dl ) <EOL> elif dl is None : <EOL> s = [ ] <EOL> else : <EOL> wrong_type ( dl ) <EOL> s = [ clean ( se ) for se in s ] <EOL> return [ se for se in s if se != "<STR_LIT>" ] <EOL> if hasattr ( cls , dep_attr ) : <EOL> dependency_function = getattr ( cls , dep_attr ) <EOL> if type ( dependency_function ) in [ list , tuple , set , str ] : <EOL> return process_dependency ( dependency_function ) <EOL> null_ = "<STR_LIT>" <EOL> deps = null_ <EOL> pb = parameters [ cls . _name ] <EOL> for t in [ [ ] , [ pb ] , [ pb , parameters ] ] : <EOL> try : <EOL> deps = dependency_function ( * t ) <EOL> break <EOL> except TypeError , te : <EOL> if dep_attr in str ( te ) : <EOL> continue <EOL> else : <EOL> raise <EOL> if deps is null_ : <EOL> raise TypeError ( ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> % ( dep_attr , cls . _name ) ) <EOL> return process_dependency ( deps ) <EOL> else : <EOL> return [ ] <EOL> mod_dep = getDep ( "<STR_LIT>" ) <EOL> res_dep = getDep ( "<STR_LIT:result>" ) <EOL> par_dep = getDep ( "<STR_LIT>" ) <EOL> return ( mod_dep , res_dep , par_dep ) <EOL> @ classmethod <EOL> def _getVersion ( cls ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( cls , "<STR_LIT:version>" ) : <EOL> return cls . version <EOL> else : <EOL> return None <EOL> @ classmethod <EOL> def _getLogger ( cls ) : <EOL> """<STR_LIT>""" <EOL> return logging . getLogger ( cls . _name ) <EOL> @ classmethod <EOL> def __getBinaryAttr ( self , attr , parameters ) : <EOL> x = getattr ( self , attr ) <EOL> if x in [ True , False ] : <EOL> return x <EOL> else : <EOL> pb = parameters [ self . _name ] <EOL> for t in [ [ ] , [ pb ] , [ pb , parameters ] ] : <EOL> try : <EOL> ret = x ( * t ) <EOL> break <EOL> except TypeError , te : <EOL> if attr in str ( te ) : <EOL> continue <EOL> else : <EOL> raise <EOL> if ret not in [ True , False ] : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % attr ) <EOL> return ret <EOL> @ classmethod <EOL> def _allowsResultCaching ( self , parameters ) : <EOL> if not self . _allowsCaching ( parameters ) : <EOL> return False <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> return not self . __getBinaryAttr ( "<STR_LIT>" , parameters ) <EOL> elif hasattr ( self , "<STR_LIT>" ) : <EOL> return not self . __getBinaryAttr ( "<STR_LIT>" , parameters ) <EOL> else : <EOL> return True <EOL> @ classmethod <EOL> def _allowsCaching ( self , parameters ) : <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> return not self . __getBinaryAttr ( "<STR_LIT>" , parameters ) <EOL> else : <EOL> return True <EOL> def _setResults ( self , r ) : <EOL> self . local_results = r <EOL> def _destroy ( self ) : <EOL> del self . local_results <EOL> del self . modules <EOL> del self . results <EOL> del self . parameters <EOL> del self . p <EOL> self . __container_map . clear ( ) <EOL> self . log . debug ( "<STR_LIT>" % self . _name ) <EOL> self . _pnode = None <EOL> def __init__ ( self , pnode , parameters , results , modules ) : <EOL> name = self . _name <EOL> self . _pnode = pnode <EOL> self . log . info ( '<STR_LIT>' % self . _name ) <EOL> self . parameters = parameters <EOL> self . p = parameters [ self . _name ] <EOL> self . results = results <EOL> self . modules = modules <EOL> self . __container_map = { } <EOL> self . setup ( ) <EOL> self . log . debug ( "<STR_LIT>" % self . _name ) <EOL> def setup ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ classmethod <EOL> def reportResults ( cls , parameters , p , r ) : <EOL> """<STR_LIT>""" <EOL> cls . log . debug ( "<STR_LIT>" % cls . _name ) <EOL> pass <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def itemHash ( self , * items ) : <EOL> """<STR_LIT>""" <EOL> t = TreeDict ( ) <EOL> for n , it in enumerate ( items ) : <EOL> t [ "<STR_LIT>" % n ] = it <EOL> return t . hash ( ) <EOL> def _cacheAction ( self , action , <EOL> obj_name , key , <EOL> ignore_module , <EOL> ignore_local , <EOL> ignore_dependencies , <EOL> is_disk_writable , <EOL> is_persistent , <EOL> creation_function = None , <EOL> obj = None ) : <EOL> if key is not None : <EOL> key = TreeDict ( key = key ) . hash ( ) <EOL> d_key = ( obj_name , key , ignore_module , ignore_local , <EOL> ignore_dependencies , is_persistent ) <EOL> try : <EOL> container = self . __container_map [ d_key ] <EOL> except KeyError : <EOL> container = self . __container_map [ d_key ] = self . _pnode . getCacheContainer ( <EOL> obj_name , key , ignore_module , ignore_local , <EOL> ignore_dependencies , is_disk_writable , is_persistent ) <EOL> if not is_disk_writable : <EOL> container . disableDiskWriting ( ) <EOL> if action == "<STR_LIT>" : <EOL> return container . objectIsLoaded ( ) <EOL> elif action == "<STR_LIT>" : <EOL> if not container . objectIsLoaded ( ) : <EOL> if creation_function is None : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> container . setObject ( creation_function ( ) ) <EOL> return container . getObject ( ) <EOL> elif action == "<STR_LIT>" : <EOL> container . setObject ( obj ) <EOL> elif action == "<STR_LIT:key>" : <EOL> return container . getKeyAsString ( ) <EOL> else : <EOL> assert False <EOL> def inCache ( self , obj_name , key = None , <EOL> ignore_module = False , <EOL> ignore_local = False , <EOL> disk_writable = True , <EOL> ignore_dependencies = False ) : <EOL> """<STR_LIT>""" <EOL> return self . _cacheAction ( "<STR_LIT>" , obj_name , key , ignore_module , <EOL> ignore_local , ignore_dependencies , True , True ) <EOL> def loadFromCache ( self , obj_name , key = None , <EOL> ignore_module = False , <EOL> ignore_local = False , <EOL> ignore_dependencies = False , <EOL> disk_writable = True , <EOL> persistent = True , <EOL> creation_function = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _cacheAction ( "<STR_LIT>" , obj_name , key , ignore_module , <EOL> ignore_local , ignore_dependencies , disk_writable , <EOL> persistent , creation_function = creation_function ) <EOL> def saveToCache ( self , obj_name , obj , <EOL> key = None , <EOL> ignore_module = False , <EOL> ignore_local = False , <EOL> ignore_dependencies = False , <EOL> persistent = True , <EOL> disk_writable = True ) : <EOL> """<STR_LIT>""" <EOL> return self . _cacheAction ( <EOL> "<STR_LIT>" , obj_name , key , ignore_module , ignore_local , <EOL> ignore_dependencies , disk_writable , persistent , obj = obj ) <EOL> def key ( self , obj_name = None , <EOL> key = None , <EOL> ignore_module = False , <EOL> ignore_local = False , <EOL> ignore_dependencies = False ) : <EOL> """<STR_LIT>""" <EOL> return self . _cacheAction ( "<STR_LIT:key>" , "<STR_LIT>" if obj_name is None else obj_name , <EOL> key , ignore_module , ignore_local , <EOL> ignore_dependencies , True , True ) <EOL> def getParameters ( self , n ) : <EOL> """<STR_LIT>""" <EOL> return self . _pnode . getSpecific ( "<STR_LIT>" , n ) <EOL> def getResults ( self , r ) : <EOL> """<STR_LIT>""" <EOL> return self . _pnode . getSpecific ( "<STR_LIT>" , r ) <EOL> def getModule ( self , m ) : <EOL> """<STR_LIT:U+0020>""" <EOL> return self . _pnode . getSpecific ( "<STR_LIT>" , m ) </s>
