<s> from hydro . common . execution_plan import ExecutionPlan <EOL> from hydro . transformers import Transformers <EOL> from hydro . query_engine_factory import QueryEngineFactory <EOL> from base_classes import Base <EOL> from hydro . common . utils import create_cache_key <EOL> from hydro . cache import CacheEnginesFactory <EOL> import inspect <EOL> from hydro . exceptions import HydroException <EOL> from hydro . common . configurator import Configurator <EOL> from hashlib import md5 <EOL> import os <EOL> from django . conf import settings <EOL> import django <EOL> __author__ = '<STR_LIT>' <EOL> class Topology ( Base ) : <EOL> def __init__ ( self , cache_engine = Configurator . CACHE_ENGINE_IN_MEMORY , base_dir = None , cls = None , logger = None ) : <EOL> super ( Topology , self ) . __init__ ( ) <EOL> if '<STR_LIT>' not in os . environ : <EOL> if not settings . configured : <EOL> settings . configure ( ) <EOL> django . setup ( ) <EOL> if logger : <EOL> self . logger = logger <EOL> if not base_dir : <EOL> base_dir = self . __module__ <EOL> if not cls : <EOL> cls = self . __class__ <EOL> self . cache_engines = CacheEnginesFactory . get_cache_engines ( ) <EOL> self . transformers = Transformers ( ) <EOL> self . base_dir = '<STR_LIT:.>' . join ( base_dir . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] ) <EOL> self . _modules_dir = self . base_dir <EOL> self . _templates_dir = os . path . dirname ( inspect . getabsfile ( cls ) ) <EOL> cache_engine_params = None <EOL> if type ( cache_engine ) == dict : <EOL> cache_engine_name = cache_engine [ '<STR_LIT>' ] <EOL> cache_engine_params = cache_engine [ '<STR_LIT>' ] <EOL> else : <EOL> cache_engine_name = cache_engine <EOL> cache_engine_class = self . cache_engines . get ( cache_engine_name ) <EOL> if not cache_engine_class : <EOL> cache_engine_class = self . cache_engines . get ( Configurator . CACHE_ENGINE_IN_MEMORY ) <EOL> self . cache_engine = cache_engine_class ( cache_engine_params ) <EOL> self . _execution_plan = ExecutionPlan ( ) <EOL> self . query_engine = QueryEngineFactory . get_query_engine ( self . _modules_dir , self . cache_engine , self . _execution_plan , self . logger ) <EOL> self . query_engine . set_templates_dir ( self . _templates_dir ) <EOL> self . logger . debug ( "<STR_LIT>" . <EOL> format ( type ( self ) . __name__ , self . _modules_dir , self . _templates_dir ) ) <EOL> self . _topology_cache_ttl = Configurator . CACHE_DB_KEY_EXPIRE <EOL> def _submit ( self , params ) : <EOL> raise HydroException ( '<STR_LIT>' ) <EOL> def set_topology_cache_ttl ( self , cache_ttl ) : <EOL> """<STR_LIT>""" <EOL> self . _topology_cache_ttl = cache_ttl <EOL> def topology_cache_ttl_callback ( self , cache_ttl ) : <EOL> """<STR_LIT>""" <EOL> if cache_ttl < self . _topology_cache_ttl : <EOL> self . logger . debug ( '<STR_LIT>' . format ( cache_ttl ) ) <EOL> self . set_topology_cache_ttl ( cache_ttl ) <EOL> def submit ( self , params ) : <EOL> hash_value = md5 ( ) <EOL> hash_value . update ( str ( params ) ) <EOL> cache_key = create_cache_key ( '<STR_LIT>' + self . __class__ . __name__ + hash_value . digest ( ) ) <EOL> data = self . cache_engine . get ( cache_key ) <EOL> hit = False if data is None else True <EOL> self . _execution_plan . add_phase ( self , "<STR_LIT>" , { '<STR_LIT>' : hit } ) <EOL> if not hit : <EOL> self . logger . debug ( '<STR_LIT>' . format ( cache_key ) ) <EOL> data = self . _submit ( params ) <EOL> cache_params = { '<STR_LIT:key>' : cache_key , '<STR_LIT:value>' : data } <EOL> cache_params [ '<STR_LIT>' ] = self . _topology_cache_ttl <EOL> self . cache_engine . put ( ** cache_params ) <EOL> else : <EOL> self . logger . debug ( '<STR_LIT>' . format ( cache_key ) ) <EOL> return data <EOL> def get_execution_plan ( self ) : <EOL> return self . _execution_plan </s>
