def _create_cache_folder(): if _cache_folder_exists(): debug("cache folder already exists") return debug("creating cache folder") os.makedirs(CACHE_FOLDER_NAME)
def _create_database(): debug("creating database") if _db_exists(): debug("database already exists") return _create_table()
def _create_folder(): debug("creating .intpy folder") if _folder_exists(): debug(".intpy folder already exists") return os.makedirs(FOLDER_NAME)
def _cache_data(func, fun_args, fun_return, elapsed_time): debug("starting caching data for {0}({1})".format(func.__name__, fun_args)) start = time.perf_counter() create_entry(func.__name__, fun_args, fun_return, inspect.getsource(func), g_argsp_v) end = time.perf_counter() debug("caching {0} took {1}".format(func.__name__, end - start))
def _cache_data(func, fun_args, fun_return, elapsed_time): debug("starting caching data for {0}({1})".format( func.__name__, fun_args)) start = time.perf_counter() fun_source = get_source_code_executed(func, g_user_script_graph) create_entry(func.__name__, fun_args, fun_return, fun_source, g_argsp_v) end = time.perf_counter() debug("caching {0} took {1}".format(func.__name__, end - start))
def _create_table(): debug("creating table") conn = DB('.intpy/intpy.db') stmt = "CREATE TABLE IF NOT EXISTS CACHE (\ id INTEGER PRIMARY KEY AUTOINCREMENT,\ cache_file TEXT UNIQUE\ );" conn.executeCmdSQLNoReturn(stmt) conn.closeConection()
def _execute_func(f, self, *method_args, **method_kwargs): start = time.perf_counter() result_value = f(self, *method_args, ** method_kwargs) if self is not None else f( *method_args, **method_kwargs) end = time.perf_counter() elapsed_time = end - start debug("{0} took {1} to run".format(f.__name__, elapsed_time)) return result_value, elapsed_time
def _create_table(): debug("creating table") conexaoBanco = Banco('.intpy/intpy.db') stmt = "CREATE TABLE IF NOT EXISTS CACHE (\ id INTEGER PRIMARY KEY AUTOINCREMENT,\ cache_file TEXT UNIQUE,\ fun_name TEXT\ );" conexaoBanco.executarComandoSQLSemRetorno(stmt) conexaoBanco.fecharConexao()
def create_entry(fun_name, fun_args, fun_return, fun_source, argsp_v): id = _get_id(fun_args, fun_source) if argsp_v == ['v01x']: global CONEXAO_BANCO CONEXAO_BANCO = Banco(os.path.join(".intpy", "intpy.db")) debug("serializing return value from {0}".format(id)) _serialize(fun_return, id) debug("inserting reference in database") _save(_get_file_name(id)) CONEXAO_BANCO.salvarAlteracoes() CONEXAO_BANCO.fecharConexao() elif (argsp_v == ['1d-ow'] or argsp_v == ['v021x'] or argsp_v == ['1d-ad'] or argsp_v == ['v022x']): DATA_DICTIONARY[id] = fun_return elif (argsp_v == ['2d-ad'] or argsp_v == ['v023x'] or argsp_v == ['2d-ad-t'] or argsp_v == ['v024x'] or argsp_v == ['2d-lz'] or argsp_v == ['v027x']): NEW_DATA_DICTIONARY[id] = fun_return elif (argsp_v == ['2d-ad-f'] or argsp_v == ['v025x'] or argsp_v == ['2d-ad-ft'] or argsp_v == ['v026x']): NEW_DATA_DICTIONARY[id] = (fun_return, fun_name)
def init_env(): debug("cheking if intpy environment exists") if _env_exists(): debug("environment already exists") debug("creating intpy environment") _create_folder() _create_cache_folder() _create_database()
def wrapper(self, *method_args, **method_kwargs): debug("calling {0}".format(f.__name__)) c = _get_cache(f, method_args) if not _cache_exists(c): debug("cache miss for {0}({1})".format(f.__name__, *method_args)) return_value, elapsed_time = _execute_func(f, self, *method_args, **method_kwargs) _cache_data(f, method_args, return_value, elapsed_time) return return_value else: debug("cache hit for {0}({1})".format(f.__name__, *method_args)) return c
def _autofix(id): debug("starting autofix") debug("removing {0} from database".format(id)) _remove(_get_file_name(id)) debug("environment fixed")
def salvarNovosDadosBanco(argsp_v): if (argsp_v == ['1d-ow'] or argsp_v == ['v021x'] or argsp_v == ['1d-ad'] or argsp_v == ['v022x']): for id in DATA_DICTIONARY: debug("serializing return value from {0}".format(id)) _serialize(DATA_DICTIONARY[id], id) debug("inserting reference in database") _save(_get_file_name(id)) elif (argsp_v == ['2d-ad'] or argsp_v == ['v023x'] or argsp_v == ['2d-ad-t'] or argsp_v == ['v024x'] or argsp_v == ['2d-lz'] or argsp_v == ['v027x']): for id in NEW_DATA_DICTIONARY: debug("serializing return value from {0}".format(id)) _serialize(NEW_DATA_DICTIONARY[id], id) debug("inserting reference in database") _save(_get_file_name(id)) elif (argsp_v == ['2d-ad-f'] or argsp_v == ['v025x'] or argsp_v == ['2d-ad-ft'] or argsp_v == ['v026x']): for id in NEW_DATA_DICTIONARY: debug("serializing return value from {0}".format(id)) _serialize(NEW_DATA_DICTIONARY[id][0], id) debug("inserting reference in database") _save_fun_name(_get_file_name(id), NEW_DATA_DICTIONARY[id][1]) CONEXAO_BANCO.salvarAlteracoes() CONEXAO_BANCO.fecharConexao()