def _find_modname_from_repo(repo): import utool as ut packages = ut.get_submodules_from_dpath(repo.dpath, only_packages=True, recursive=False) if len(packages) == 1: modname = ut.get_modname_from_modpath(packages[0]) return modname
def get_regen_cmd(): try: if len(sys.argv) > 0 and ut.checkpath(sys.argv[0]): # Check if running python command if ut.is_python_module(sys.argv[0]): python_exe = ut.python_executable(check=False) modname = ut.get_modname_from_modpath(sys.argv[0]) new_argv = [python_exe, '-m', modname] + sys.argv[1:] return ' '.join(new_argv) except Exception as ex: ut.printex(ex, iswarning=True) return ' '.join(sys.argv)
def get_regen_cmd(): # TODO: move to utool try: if len(sys.argv) > 0 and ut.checkpath(sys.argv[0]): # Check if running python command if ut.is_python_module(sys.argv[0]): python_exe = ut.python_executable(check=False) modname = ut.get_modname_from_modpath(sys.argv[0]) new_argv = [python_exe, '-m', modname] + sys.argv[1:] return ' '.join(new_argv) except Exception as ex: ut.printex(ex, iswarning=True) return ' '.join(sys.argv)
def get_current_modulename(): """ returns current module being edited buffer_name = ut.truepath('~/local/vim/rc/pyvim_funcs.py') """ import vim from os.path import dirname import utool as ut ut.ENABLE_COLORS = False ut.util_str.ENABLE_COLORS = False ut.util_dbg.COLORED_EXCEPTIONS = False #ut.rrrr(verbose=False) buffer_name = vim.current.buffer.name modname = ut.get_modname_from_modpath(buffer_name) moddir = dirname(buffer_name) return modname, moddir
def makeinit(module_path, exclude_modnames=[]): #module_name = basename(module_path) module_name = ut.get_modname_from_modpath(module_path) IMPORT_TUPLES = util_importer.make_import_tuples(module_path, exclude_modnames=exclude_modnames) initstr = util_importer.make_initstr(module_name, IMPORT_TUPLES) regen_command = 'cd %s\n' % (module_path) regen_command += ' makeinit.py' if len(exclude_modnames ) > 0: regen_command += ' -x ' + ' '.join(exclude_modnames) regen_block = (ut.codeblock(''' """ Regen Command: {regen_command} """ ''').format(regen_command=regen_command)) print('### __init__.py ###') print(initstr) print('\nIMPORT_TUPLES = ' + ut.list_str(IMPORT_TUPLES)) print(regen_block)
def makeinit(mod_dpath, exclude_modnames=[], use_star=False): r""" Args: mod_dpath (str): exclude_modnames (list): (default = []) use_star (bool): (default = False) Returns: str: init_codeblock CommandLine: python -m utool.util_autogen makeinit --modname=ibeis.algo Example: >>> # SCRIPT >>> from utool.util_autogen import * # NOQA >>> import utool as ut >>> modname = ut.get_argval('--modname', str, default=None) >>> mod_dpath = (os.getcwd() if modname is None else >>> ut.get_modpath(modname, prefer_pkg=True)) >>> mod_dpath = ut.unixpath(mod_dpath) >>> mod_fpath = join(mod_dpath, '__init__.py') >>> exclude_modnames = ut.get_argval(('--exclude', '-x'), list, default=[]) >>> use_star = ut.get_argflag('--star') >>> init_codeblock = makeinit(mod_dpath, exclude_modnames, use_star) >>> ut.dump_autogen_code(mod_fpath, init_codeblock) """ from utool._internal import util_importer import utool as ut module_name = ut.get_modname_from_modpath(mod_dpath) IMPORT_TUPLES = util_importer.make_import_tuples(mod_dpath, exclude_modnames=exclude_modnames) initstr = util_importer.make_initstr(module_name, IMPORT_TUPLES) regen_command = 'cd %s\n' % (mod_dpath) regen_command += ' makeinit.py' regen_command += ' --modname={modname}'.format(modname=module_name) if use_star: regen_command += ' --star' if len(exclude_modnames ) > 0: regen_command += ' -x ' + ' '.join(exclude_modnames) regen_block = (ut.codeblock(''' """ Regen Command: {regen_command} """ ''').format(regen_command=regen_command)) importstar_codeblock = ut.codeblock( ''' """ python -c "import {module_name}" --dump-{module_name}-init python -c "import {module_name}" --update-{module_name}-init """ __DYNAMIC__ = True if __DYNAMIC__: # TODO: import all utool external prereqs. Then the imports will not import # anything that has already in a toplevel namespace # COMMENTED OUT FOR FROZEN __INIT__ # Dynamically import listed util libraries and their members. from utool._internal import util_importer # FIXME: this might actually work with rrrr, but things arent being # reimported because they are already in the modules list import_execstr = util_importer.dynamic_import(__name__, IMPORT_TUPLES) exec(import_execstr) DOELSE = False else: # Do the nonexec import (can force it to happen no matter what if alwyas set # to True) DOELSE = True if DOELSE: # <AUTOGEN_INIT> pass # </AUTOGEN_INIT> '''.format(module_name=module_name) ) ts_line = '# Autogenerated on {ts}'.format(ts=ut.get_timestamp('printable')) init_codeblock_list = ['# -*- coding: utf-8 -*-', ts_line] init_codeblock_list.append(initstr) init_codeblock_list.append('\nIMPORT_TUPLES = ' + ut.list_str(IMPORT_TUPLES)) if use_star: init_codeblock_list.append(importstar_codeblock) init_codeblock_list.append(regen_block) init_codeblock = '\n'.join(init_codeblock_list) return init_codeblock
def reload_class(self, verbose=True, reload_module=True): """ special class reloading function This function is often injected as rrr of classes """ import utool as ut verbose = verbose or VERBOSE_CLASS classname = self.__class__.__name__ try: modname = self.__class__.__module__ if verbose: print('[class] reloading ' + classname + ' from ' + modname) # --HACK-- if hasattr(self, '_on_reload'): if verbose > 1: print('[class] calling _on_reload for ' + classname) self._on_reload() elif verbose > 1: print('[class] ' + classname + ' does not have an _on_reload function') # Do for all inheriting classes def find_base_clases(_class, find_base_clases=None): class_list = [] for _baseclass in _class.__bases__: parents = find_base_clases(_baseclass, find_base_clases) class_list.extend(parents) if _class is not object: class_list.append(_class) return class_list head_class = self.__class__ # Determine if parents need reloading class_list = find_base_clases(head_class, find_base_clases) # HACK ignore = {HashComparable2} class_list = [_class for _class in class_list if _class not in ignore] for _class in class_list: if verbose: print('[class] reloading parent ' + _class.__name__ + ' from ' + _class.__module__) if _class.__module__ == '__main__': # Attempt to find the module that is the main module # This may be very hacky and potentially break main_module_ = sys.modules[_class.__module__] main_modname = ut.get_modname_from_modpath(main_module_.__file__) module_ = sys.modules[main_modname] else: module_ = sys.modules[_class.__module__] if hasattr(module_, 'rrr'): if reload_module: module_.rrr(verbose=verbose) else: if reload_module: import imp if verbose: print('[class] reloading ' + _class.__module__ + ' with imp') try: imp.reload(module_) except (ImportError, AttributeError): print('[class] fallback reloading ' + _class.__module__ + ' with imp') # one last thing to try. probably used ut.import_module_from_fpath # when importing this module imp.load_source(module_.__name__, module_.__file__) # Reset class attributes _newclass = getattr(module_, _class.__name__) reload_class_methods(self, _newclass, verbose=verbose) # --HACK-- # TODO: handle injected definitions if hasattr(self, '_initialize_self'): if verbose > 1: print('[class] calling _initialize_self for ' + classname) self._initialize_self() elif verbose > 1: print('[class] ' + classname + ' does not have an _initialize_self function') except Exception as ex: ut.printex(ex, 'Error Reloading Class', keys=[ 'modname', 'module', 'class_', 'class_list', 'self', ]) raise
def reload_class(self, verbose=True): """ special class reloading function This function is often injected as rrr of classes """ import utool as ut verbose = verbose or VERBOSE_CLASS classname = self.__class__.__name__ try: modname = self.__class__.__module__ if verbose: print('[class] reloading ' + classname + ' from ' + modname) # --HACK-- if hasattr(self, '_on_reload'): if verbose > 1: print('[class] calling _on_reload for ' + classname) self._on_reload() elif verbose > 1: print('[class] ' + classname + ' does not have an _on_reload function') # Do for all inheriting classes def find_base_clases(_class, find_base_clases=None): class_list = [] for _baseclass in _class.__bases__: parents = find_base_clases(_baseclass, find_base_clases) class_list.extend(parents) if _class is not object: class_list.append(_class) return class_list head_class = self.__class__ # Determine if parents need reloading class_list = find_base_clases(head_class, find_base_clases) # HACK ignore = {HashComparable2} class_list = [_class for _class in class_list if _class not in ignore] for _class in class_list: if verbose: print('[class] reloading parent ' + _class.__name__ + ' from ' + _class.__module__) if _class.__module__ == '__main__': # Attempt to find the module that is the main module # This may be very hacky and potentially break main_module_ = sys.modules[_class.__module__] main_modname = ut.get_modname_from_modpath(main_module_.__file__) module_ = sys.modules[main_modname] else: module_ = sys.modules[_class.__module__] if hasattr(module_, 'rrr'): module_.rrr(verbose=verbose) else: import imp if verbose: print('[class] reloading ' + _class.__module__ + ' with imp') try: imp.reload(module_) except (ImportError, AttributeError): print('[class] fallback reloading ' + _class.__module__ + ' with imp') # one last thing to try. probably used ut.import_module_from_fpath # when importing this module imp.load_source(module_.__name__, module_.__file__) # Reset class attributes _newclass = getattr(module_, _class.__name__) reload_class_methods(self, _newclass, verbose=verbose) # --HACK-- # TODO: handle injected definitions if hasattr(self, '_initialize_self'): if verbose > 1: print('[class] calling _initialize_self for ' + classname) self._initialize_self() elif verbose > 1: print('[class] ' + classname + ' does not have an _initialize_self function') except Exception as ex: ut.printex(ex, 'Error Reloading Class', keys=[ 'modname', 'module', 'class_', 'class_list', 'self', ]) raise