def checkinfo_wrapper(*args, **kwargs): suggested_fix = '' funcname = get_funcname(func) packagename = funcname.replace('_version', '') pipname_ = pipname if pipname is not None else packagename try: infodict = func(*args, **kwargs) except ImportError as ex: infodict = module_stdinfo_dict(None, name=pipname_) suggested_fix = 'pip install ' + pipname_ if not sys.platform.startswith('win32'): suggested_fix = 'sudo ' + suggested_fix return False, 'None', target, infodict, ut.formatex(ex), suggested_fix except Exception as ex: infodict = module_stdinfo_dict(None, name=pipname_) return False, 'None', target, infodict, ut.formatex(ex), 'Some unknown error in ' + packagename current_version = infodict['__version__'] # Build status text msg = ut.dict_str(infodict, strvals=True) msg += '\n' + '%s: %r >= (target=%r)?' % (funcname, current_version, target) statustext = ut.msgblock(infodict['__name__'], msg) # Check if passed passed = version_ge_target(current_version, target) # Suggest possible fix if not passed: suggested_fix = 'pip install ' + infodict['__name__'] + ' --upgrade' if not sys.platform.startswith('win32'): suggested_fix = 'sudo ' + suggested_fix return passed, current_version, target, infodict, statustext, suggested_fix
def on_engine_request(ibs, jobid, action, args, kwargs): """ Run whenever the engine recieves a message """ # Start working if VERBOSE_JOBS: print('starting job=%r' % (jobid, )) # Map actions to IBEISController calls here if action == 'helloworld': def helloworld(time_=0, *args, **kwargs): time.sleep(time_) retval = ('HELLO time_=%r ' % (time_, )) + ut.repr2((args, kwargs)) return retval action_func = helloworld else: # check for ibs func action_func = getattr(ibs, action) if VERBOSE_JOBS: print('resolving action=%r to ibeis function=%r' % (action, action_func)) try: result = action_func(*args, **kwargs) exec_status = 'ok' except Exception as ex: result = ut.formatex(ex, keys=['jobid'], tb=True) result = ut.strip_ansi(result) exec_status = 'exception' json_result = ut.to_json(result) engine_result = dict( exec_status=exec_status, json_result=json_result, jobid=jobid, ) return engine_result
def on_engine_request(ibs, jobid, action, args, kwargs): """ Run whenever the engine recieves a message """ # Start working if VERBOSE_JOBS: print('starting job=%r' % (jobid,)) # Map actions to IBEISController calls here if action == 'helloworld': def helloworld(time_=0, *args, **kwargs): time.sleep(time_) retval = ('HELLO time_=%r ' % (time_,)) + ut.repr2((args, kwargs)) return retval action_func = helloworld else: # check for ibs func action_func = getattr(ibs, action) if VERBOSE_JOBS: print('resolving action=%r to ibeis function=%r' % (action, action_func)) try: result = action_func(*args, **kwargs) exec_status = 'ok' except Exception as ex: result = ut.formatex(ex, keys=['jobid'], tb=True) result = ut.strip_ansi(result) exec_status = 'exception' json_result = ut.to_json(result) engine_result = dict( exec_status=exec_status, json_result=json_result, jobid=jobid, ) return engine_result
def auto_docstr(modname, funcname, verbose=True, moddir=None, **kwargs): r""" called from vim. Uses strings of filename and modnames to build docstr Args: modname (str): name of a python module funcname (str): name of a function in the module Returns: str: docstr CommandLine: python -m utool.util_autogen auto_docstr python -m utool --tf auto_docstr Example: >>> import utool as ut >>> from utool.util_autogen import * # NOQA >>> ut.util_autogen.rrr(verbose=False) >>> #docstr = ut.auto_docstr('ibeis.algo.hots.smk.smk_index', 'compute_negentropy_names') >>> modname = ut.get_argval('--modname', default='utool.util_autogen') >>> funcname = ut.get_argval('--funcname', default='auto_docstr') >>> moddir = ut.get_argval('--moddir', type_=str, default=None) >>> docstr = ut.util_autogen.auto_docstr(modname, funcname) >>> print(docstr) """ #import utool as ut func, module, error_str = load_func_from_module( modname, funcname, verbose=verbose, moddir=moddir) if error_str is None: try: docstr = make_default_docstr(func, **kwargs) except Exception as ex: import utool as ut error_str = ut.formatex(ex, 'Caught Error in parsing docstr', tb=True) #ut.printex(ex) error_str += ( '\n\nReplicateCommand:\n ' 'python -m utool --tf auto_docstr ' '--modname={modname} --funcname={funcname} --moddir={moddir}').format( modname=modname, funcname=funcname, moddir=moddir) error_str += '\n kwargs=' + ut.dict_str(kwargs) return error_str else: docstr = error_str return docstr
def engine_loop(id_, dbdir=None): r""" IBEIS: This will be part of a worker process with its own IBEISController instance. Needs to send where the results will go and then publish the results there. The engine_loop - receives messages, performs some action, and sends a reply, preserving the leading two message parts as routing identities """ import ibeis #base_print = print # NOQA print = partial(ut.colorprint, color='darkred') with ut.Indenter('[engine %d] ' % (id_)): if VERBOSE_JOBS: print('Initializing engine') print('connect engine_url2 = %r' % (engine_url2,)) assert dbdir is not None #ibs = ibeis.opendb(dbname) ibs = ibeis.opendb(dbdir=dbdir, use_cache=False, web=False) engine_rout_sock = ctx.socket(zmq.ROUTER) engine_rout_sock.connect(engine_url2) collect_deal_sock = ctx.socket(zmq.DEALER) collect_deal_sock.setsockopt_string(zmq.IDENTITY, 'engine.collect.DEALER') collect_deal_sock.connect(collect_url1) if VERBOSE_JOBS: print('connect collect_url1 = %r' % (collect_url1,)) print('engine is initialized') while True: idents, engine_request = rcv_multipart_json(engine_rout_sock, print=print) action = engine_request['action'] jobid = engine_request['jobid'] args = engine_request['args'] kwargs = engine_request['kwargs'] callback_url = engine_request['callback_url'] # Start working if VERBOSE_JOBS: print('starting job=%r' % (jobid,)) # Map actions to IBEISController calls here if action == 'helloworld': def helloworld(time_=0, *args, **kwargs): time.sleep(time_) retval = ('HELLO time_=%r ' % (time_,)) + ut.repr2((args, kwargs)) return retval action_func = helloworld else: # check for ibs func action_func = getattr(ibs, action) if VERBOSE_JOBS: print('resolving to ibeis function') try: result = action_func(*args, **kwargs) exec_status = 'ok' except Exception as ex: result = ut.formatex(ex, keys=['jobid'], tb=True) exec_status = 'exception' json_result = ut.to_json(result) engine_result = dict( exec_status=exec_status, json_result=json_result, jobid=jobid, ) # Store results in the collector collect_request = dict( idents=idents, action='store', jobid=jobid, engine_result=engine_result, callback_url=callback_url, ) if VERBOSE_JOBS: print('...done working. pushing result to collector') collect_deal_sock.send_json(collect_request) # ---- if VERBOSE_JOBS: print('Exiting engine loop')
def translate_ibeis_webcall(func, *args, **kwargs): r""" Called from flask request context Args: func (function): live python function Returns: tuple: (output, True, 200, None, jQuery_callback) CommandLine: python -m ibeis.control.controller_inject --exec-translate_ibeis_webcall python -m ibeis.control.controller_inject --exec-translate_ibeis_webcall --domain http://52.33.105.88 Example: >>> # xdoctest: +REQUIRES(--web) >>> from ibeis.control.controller_inject import * # NOQA >>> import ibeis >>> import time >>> import ibeis.web >>> web_ibs = ibeis.opendb_bg_web('testdb1', wait=1, start_job_queue=False) >>> aids = web_ibs.send_ibeis_request('/api/annot/', 'get') >>> uuid_list = web_ibs.send_ibeis_request('/api/annot/uuids/', aid_list=aids) >>> failrsp = web_ibs.send_ibeis_request('/api/annot/uuids/') >>> failrsp2 = web_ibs.send_ibeis_request('/api/query/chips/simple_dict//', 'get', qaid_list=[0], daid_list=[0]) >>> log_text = web_ibs.send_ibeis_request('/api/query/chips/simple_dict/', 'get', qaid_list=[0], daid_list=[0]) >>> time.sleep(.1) >>> print('\n---\nuuid_list = %r' % (uuid_list,)) >>> print('\n---\nfailrsp =\n%s' % (failrsp,)) >>> print('\n---\nfailrsp2 =\n%s' % (failrsp2,)) >>> print('Finished test') >>> web_ibs.terminate2() Ignore: app = get_flask_app() with app.app_context(): #ibs = ibeis.opendb('testdb1') func = ibs.get_annot_uuids args = tuple() kwargs = dict() """ print('Calling: %r with args: %r and kwargs: %r' % (func, args, kwargs, )) ibs = flask.current_app.ibs funcstr = ut.func_str(func, (ibs,) + args, kwargs=kwargs, truncate=True) print('[TRANSLATE] Calling: %s' % (funcstr,)) assert len(args) == 0, 'There should not be any args=%r' % (args,) try: # TODO, have better way to differentiate ibs funcs from other funcs output = func(**kwargs) except TypeError: try: output = func(ibs=ibs, **kwargs) except WebException: raise except Exception as ex2: msg_list = [] msg_list.append('Error in translate_ibeis_webcall') msg_list.append('Expected Function Definition: ' + ut.func_defsig(func)) msg_list.append('Received Function Definition: %s' % (funcstr,)) msg_list.append('kwargs = %r' % (kwargs,)) msg_list.append('args = %r' % (args,)) msg_list.append('flask.request.args = %r' % (flask.request.args,)) msg_list.append('flask.request.form = %r' % (flask.request.form,)) msg = '\n'.join(msg_list) error_msg = ut.formatex(ex2, msg, tb=True) print(error_msg) # error_msg = ut.strip_ansi(error_msg) raise Exception(error_msg) #raise resp_tup = (output, True, 200, None) return resp_tup
def translate_ibeis_webcall(func, *args, **kwargs): r""" Called from flask request context Args: func (function): live python function Returns: tuple: (output, True, 200, None, jQuery_callback) CommandLine: python -m ibeis.control.controller_inject --exec-translate_ibeis_webcall python -m ibeis.control.controller_inject --exec-translate_ibeis_webcall --domain http://52.33.105.88 Example: >>> # WEB_DOCTEST >>> from ibeis.control.controller_inject import * # NOQA >>> import ibeis >>> import time >>> import ibeis.web >>> web_ibs = ibeis.opendb_bg_web('testdb1', wait=1, start_job_queue=False) >>> aids = web_ibs.send_ibeis_request('/api/annot/', 'get') >>> uuid_list = web_ibs.send_ibeis_request('/api/annot/uuids/', aid_list=aids) >>> failrsp = web_ibs.send_ibeis_request('/api/annot/uuids/') >>> failrsp2 = web_ibs.send_ibeis_request('/api/query/chips/simple_dict//', 'get', qaid_list=[0], daid_list=[0]) >>> log_text = web_ibs.send_ibeis_request('/api/query/chips/simple_dict/', 'get', qaid_list=[0], daid_list=[0]) >>> time.sleep(.1) >>> print('\n---\nuuid_list = %r' % (uuid_list,)) >>> print('\n---\nfailrsp =\n%s' % (failrsp,)) >>> print('\n---\nfailrsp2 =\n%s' % (failrsp2,)) >>> print('Finished test') >>> web_ibs.terminate2() Ignore: app = get_flask_app() with app.app_context(): #ibs = ibeis.opendb('testdb1') func = ibs.get_annot_uuids args = tuple() kwargs = dict() """ #print('Calling: %r with args: %r and kwargs: %r' % (func, args, kwargs, )) ibs = flask.current_app.ibs funcstr = ut.func_str(func, (ibs,) + args, kwargs=kwargs, truncate=True) print('[TRANSLATE] Calling: %s' % (funcstr,)) assert len(args) == 0, 'There should not be any args=%r' % (args,) try: # TODO, have better way to differentiate ibs funcs from other funcs output = func(**kwargs) except TypeError: try: output = func(ibs=ibs, **kwargs) except WebException: raise except Exception as ex2: msg_list = [] msg_list.append('Error in translate_ibeis_webcall') msg_list.append('Expected Function Definition: ' + ut.func_defsig(func)) msg_list.append('Received Function Definition: %s' % (funcstr,)) msg_list.append('kwargs = %r' % (kwargs,)) msg_list.append('args = %r' % (args,)) msg_list.append('flask.request.args = %r' % (flask.request.args,)) msg_list.append('flask.request.form = %r' % (flask.request.form,)) msg = '\n'.join(msg_list) error_msg = ut.formatex(ex2, msg, tb=True) print(error_msg) # error_msg = ut.strip_ansi(error_msg) raise Exception(error_msg) #raise resp_tup = (output, True, 200, None) return resp_tup
def load_func_from_module(modname, funcname, verbose=True, moddir=None): r""" Args: modname (str): module name funcname (str): function name verbose (bool): verbosity flag(default = True) moddir (None): (default = None) CommandLine: python -m utool.util_autogen load_func_from_module Example: >>> # UNSTABLE_DOCTEST >>> from utool.util_autogen import * # NOQA >>> import utool as ut >>> #modname = 'plottool.plots' >>> #funcname = 'multi_plot' >>> modname = 'utool.util_path' >>> funcname = 'checkpath' >>> verbose = True >>> moddir = None >>> func, module, error_str = load_func_from_module(modname, funcname, verbose, moddir) >>> source = ut.get_func_sourcecode(func, strip_docstr=True, strip_comments=True) >>> keyname = ut.named_field('keyname', ut.REGEX_VARNAME) >>> default = ut.named_field('default', '[\'\"A-Za-z_][A-Za-z0-9_\'\"]*') >>> pattern = re.escape('kwargs.get(\'') + keyname + re.escape('\',') >>> kwarg_keys = [match.groupdict()['keyname'] for match in re.finditer(pattern, source)] """ import utool as ut from os.path import join import imp func = None module = None error_str = None print('modname = %r' % (modname,)) print('funcname = %r' % (funcname,)) print('moddir = %r' % (moddir,)) if not isinstance(modname, six.string_types): error_str = 'modname=%r is not a string. bad input' % (modname,) else: if False: basemodname_ = modname basemodname = '' parts = modname.split('.') for index in range(len(parts)): #print('index = %r' % (index,)) basemodname = '.'.join(parts[0:index + 1]) #print('basemodname = %r' % (basemodname,)) basemodule = __import__(basemodname) #print('basemodule = %r' % (basemodule,)) #if hasattr(basemodule, 'rrrr'): # basemodule.rrrr() #imp.reload(basemodule) try: module = __import__(modname) except ImportError: if moddir is not None: #parts = # There can be a weird double import error thing happening here # Rectify the dots in the filename module = ut.import_module_from_fpath(join(moddir, modname.split('.')[-1] + '.py')) else: raise #import inspect try: imp.reload(module) except Exception as ex: pass if False: # Try removing pyc if it exists if module.__file__.endswith('.pyc'): ut.delete(module.__file__, verbose=False) try: module = __import__(modname) except ImportError: if moddir is not None: module = ut.import_module_from_fpath(join(moddir, modname.split('.')[-1] + '.py')) else: raise try: imp.reload(module) except Exception as ex: pass try: # FIXME: PYTHON 3 execstr = ut.codeblock( ''' try: import {modname} module = {modname} #print('Trying to reload module=%r' % (module,)) imp.reload(module) except Exception: # If it fails maybe the module is not in the path if moddir is not None: try: import imp import os orig_dir = os.getcwd() os.chdir(moddir) modname_str = '{modname}' modinfo = imp.find_module(modname_str, [moddir]) module = imp.load_module(modname_str, *modinfo) #print('loaded module=%r' % (module,)) except Exception as ex: ut.printex(ex, 'failed to imp.load_module') pass finally: os.chdir(orig_dir) import imp import utool as ut imp.reload(ut.util_autogen) imp.reload(ut.util_inspect) try: func = module.{funcname} except AttributeError: docstr = 'Could not find attribute funcname={funcname} in modname={modname} This might be a reloading issue' imp.reload(module) ''' ).format(**locals()) exec_locals = locals() exec_globals = globals() exec(execstr, exec_globals, exec_locals) func = exec_locals.get('func', None) module = exec_locals.get('module', None) except Exception as ex2: docstr = 'error ' + str(ex2) if verbose: import utool as ut #ut.printex(ex1, 'ex1') ut.printex(ex2, 'ex2', tb=True) testcmd = 'python -c "import utool; print(utool.auto_docstr(\'%s\', \'%s\'))"' % (modname, funcname) error_str = ut.formatex(ex2, 'ex2', tb=True, keys=['modname', 'funcname', 'testcmd']) error_str += '---' + execstr return func, module, error_str
def auto_docstr(modname, funcname, verbose=True, moddir=None, **kwargs): """ called from vim. Uses strings of filename and modnames to build docstr Args: modname (str): funcname (str): Returns: docstr Example: >>> import utool >>> utool.util_autogen.rrr(verbose=False) >>> #docstr = utool.auto_docstr('ibeis.model.hots.smk.smk_index', 'compute_negentropy_names') >>> modname = 'utool.util_autogen' >>> funcname = 'auto_docstr' >>> docstr = utool.util_autogen.auto_docstr(modname, funcname) >>> print(docstr) """ import utool import utool as ut # NOQA docstr = 'error' if isinstance(modname, str): module = __import__(modname) import imp imp.reload(module) try: # FIXME: PYTHON 3 execstr = utool.codeblock( ''' try: import {modname} module = {modname} imp.reload(module) except Exception: # If it fails maybe the module is not in the path if moddir is not None: try: import imp import os orig_dir = os.getcwd() os.chdir(moddir) modname_str = '{modname}' modinfo = imp.find_module(modname_str, [moddir]) module = imp.load_module(modname_str, *modinfo) #print('loaded module=%r' % (module,)) except Exception as ex: ut.printex(ex, 'failed to imp.load_module') pass finally: os.chdir(orig_dir) import imp import utool imp.reload(utool.util_autogen) imp.reload(utool.util_inspect) #if hasattr(module, '{funcname}'): #else: try: func = module.{funcname} docstr = utool.util_autogen.make_default_docstr(func, **kwargs) except AttributeError: docstr = 'Could not find attribute funcname={funcname} in modname={modname} This might be a reloading issue' imp.reload(module) ''' ).format(**locals()) exec_globals = globals() exec_locals = locals() exec(execstr, exec_globals, exec_locals) docstr = exec_locals['docstr'] #, globals(), locals()) #return 'BARFOOO' + docstr return docstr #print(execstr) except Exception as ex2: docstr = 'error ' + str(ex2) if verbose: import utool #utool.printex(ex1, 'ex1') utool.printex(ex2, 'ex2', tb=True) testcmd = 'python -c "import utool; print(utool.auto_docstr(\'%s\', \'%s\'))"' % (modname, funcname) error_str = utool.formatex(ex2, 'ex2', tb=True, keys=['modname', 'funcname', 'testcmd']) error_str += '---' + execstr return error_str #return docstr + '\n' + execstr else: docstr = 'error' return docstr