def reload_package(pkg_name, dummy=True):
    if pkg_name not in sys.modules:
        dprint("error:", pkg_name, "is not loaded.")
        return

    main = sys.modules[pkg_name]

    dprint("begin", fill='=')

    modules = {main.__name__: main}
    modules.update({name: module for name, module in sys.modules.items()
                    if name.startswith(pkg_name + ".")})
    for m in modules:
        if m in sys.modules:
            sublime_plugin.unload_module(modules[m])
            del sys.modules[m]

    try:
        with intercepting_imports(modules), \
                importing_fromlist_aggresively(modules):

            reload_plugin(main.__name__)
            reload_missing(modules)
    except:
        dprint("reload failed.", fill='-')
        raise
    if dummy:
        load_dummy()
    dprint("end", fill='-')
Esempio n. 2
0
def reload_package(pkg_name, dummy=True, verbose=True):
    if pkg_name not in sys.modules:
        dprint("error:", pkg_name, "is not loaded.")
        return

    main = sys.modules[pkg_name]

    if verbose:
        dprint("begin", fill='=')

    modules = {main.__name__: main}
    modules.update({name: module for name, module in sys.modules.items()
                    if name.startswith(pkg_name + ".")})
    for m in modules:
        if m in sys.modules:
            sublime_plugin.unload_module(modules[m])
            del sys.modules[m]

    try:
        with intercepting_imports(modules, verbose), \
                importing_fromlist_aggresively(modules):

            reload_plugin(main.__name__)
    except Exception:
        dprint("reload failed.", fill='-')
        reload_missing(modules, verbose)
        raise

    if dummy:
        load_dummy(verbose)

    if verbose:
        dprint("end", fill='-')
def reload_package(pkg_name, dummy=True, verbose=True):
    if is_dependency(pkg_name):
        reload_dependency(pkg_name, dummy, verbose)
        return

    if pkg_name not in sys.modules:
        dprint("error:", pkg_name, "is not loaded.")
        return

    if verbose:
        dprint("begin", fill='=')

    modules = get_package_modules(pkg_name)

    for m in modules:
        if m in sys.modules:
            sublime_plugin.unload_module(modules[m])
            del sys.modules[m]

    try:
        with intercepting_imports(modules, verbose), \
                importing_fromlist_aggresively(modules):

            reload_plugin(pkg_name)
    except Exception:
        dprint("reload failed.", fill='-')
        reload_missing(modules, verbose)
        raise

    if dummy:
        load_dummy(verbose)

    if verbose:
        dprint("end", fill='-')
Esempio n. 4
0
def reload_package(pkg_name, dummy=True, verbose=True, then=None):
    if pkg_name not in sys.modules:
        dprint("error:", pkg_name, "is not loaded.")
        return

    if is_dependency(pkg_name):
        dependencies, packages = resolve_dependencies(pkg_name)
    else:
        dependencies = set()
        packages = {pkg_name}

    if verbose:
        dprint("begin", fill='=')

    all_modules = {
        module_name: module
        for pkg_name in dependencies | packages
        for module_name, module in get_package_modules(pkg_name).items()
    }

    # Tell Sublime to unload plugins
    for pkg_name in packages:
        for plugin in package_plugins(pkg_name):
            module = sys.modules.get(plugin)
            if module:
                sublime_plugin.unload_module(module)

    # Unload modules
    for module_name in all_modules:
        sys.modules.pop(module_name)

    # Reload packages
    try:
        with intercepting_imports(
                all_modules,
                verbose), importing_fromlist_aggresively(all_modules):
            for pkg_name in packages:
                for plugin in package_plugins(pkg_name):
                    sublime_plugin.reload_plugin(plugin)
    except Exception:
        dprint("reload failed.", fill='-')
        reload_missing(all_modules, verbose)
        raise

    if dummy:
        load_dummy(verbose)

    if verbose:
        dprint("end", fill='-')

    if then:
        then()

    sublime.active_window().status_message('GitSavvy has 🙌 reloaded.')
def reload_package(package, dependencies=[], dummy=True, verbose=True):
    if verbose:
        dprint("begin", fill='=')

    if dummy:
        load_dummy(verbose)

    packages = [package] + dependencies
    parents = set()
    for package in packages:
        for parent in resolve_parents(package):
            parents.add(parent)
    parents = list(parents)

    modules = sorted(list(set(get_package_modules(packages + parents))),
                     key=lambda x: x[0].split('.'))

    plugins = [m for m, is_plugin in modules if is_plugin]
    # Tell Sublime to unload plugin_modules
    for plugin in plugins:
        if plugin in sys.modules:
            sublime_plugin.unload_module(sys.modules[plugin])

    # these are modules marked to be reloaded, they are not necessarily reloaded
    modules_to_reload = [
        sys.modules[m] for m, is_plugin in modules if m in sys.modules
    ]

    with ReloadingImporter(modules_to_reload, verbose) as importer:
        if plugins:
            # we only reload top level plugin_modules to mimic Sublime Text natural order
            for plugin in plugins:
                if plugin in sys.modules:
                    module = sys.modules[plugin]
                    importer.reload(module)

            for plugin in plugins:
                if plugin in sys.modules:
                    module = sys.modules[plugin]
                    sublime_plugin.load_module(module)
                else:
                    # in case we missed something
                    sublime_plugin.reload_plugin(plugin)
        else:
            # it is possibly a dependency but no packages use it
            for module in modules_to_reload:
                importer.reload(module)

    if dummy:
        load_dummy(verbose)

    if verbose:
        dprint("end", fill='-')
Esempio n. 6
0
def reload_package(pkg_name, dummy=True, verbose=True):
    if pkg_name not in sys.modules:
        dprint("error:", pkg_name, "is not loaded.")
        return

    if is_dependency(pkg_name):
        dependencies, packages = resolve_dependencies(pkg_name)
    else:
        dependencies = set()
        packages = {pkg_name}

    if verbose:
        dprint("begin", fill='=')

    all_modules = {
        module_name: module
        for pkg_name in dependencies | packages
        for module_name, module in get_package_modules(pkg_name).items()
    }

    # Tell Sublime to unload plugins
    for pkg_name in packages:
        for plugin in package_plugins(pkg_name):
            module = sys.modules.get(plugin)
            if module:
                sublime_plugin.unload_module(module)

    # Unload modules
    for module_name in all_modules:
        sys.modules.pop(module_name)

    # Reload packages
    try:
        with intercepting_imports(all_modules, verbose), importing_fromlist_aggresively(all_modules):
            for pkg_name in packages:
                for plugin in package_plugins(pkg_name):
                    sublime_plugin.reload_plugin(plugin)
    except Exception:
        dprint("reload failed.", fill='-')
        reload_missing(all_modules, verbose)
        raise

    if dummy:
        load_dummy(verbose)

    if verbose:
        dprint("end", fill='-')
Esempio n. 7
0
def reload_packages(packages: Iterable[str]) -> None:
    packages = get_dependents(packages, get_dependency_relationships())
    modules = list(get_package_modules(packages))

    sorted_modules = sorted([module for module, path in modules],
                            key=lambda module: module.__name__.split('.'))

    plugins = [module for module, path in modules if len(path.parts) == 3]

    for module in plugins:
        sublime_plugin.unload_module(module)

    with ReloadingImporter(sorted_modules) as reload:
        for module in sorted_modules:
            try:
                reload(module)
            except FileNotFoundError as e:
                print(e)

    for module in plugins:
        sublime_plugin.load_module(module)
Esempio n. 8
0
def monitor_plugins():
    """Monitor for any plugin that conflicts with anaconda
    """

    view = sublime.active_window().active_view()
    if not get_settings(view, 'auto_unload_conflictive_plugins', True):
        return

    plist = [
        'Jedi - Python autocompletion',  # breaks auto completion
        'SublimePythonIDE',  # interfere with autocompletion
        'SublimeCodeIntel'  # breaks everything, SCI is a mess
    ]
    hllist = [
        'MagicPython',  # breaks autocompletion on [dot]
        'Python 3'  # breeaks autocompletion on [dot]
    ]

    for plugin in plist:
        if plugin in sys.modules:
            [
                sublime_plugin.unload_module(m) for k, m in sys.modules.items()
                if plugin in k
            ]
            if plugin not in DISABLED_PLUGINS:
                DISABLED_PLUGINS.append(plugin)

    for highlighter in hllist:
        paths = os.listdir(sublime.packages_path()) + \
            os.listdir(sublime.installed_packages_path())

        for p in paths:
            if highlighter in p:
                fname = '{0}.sublime-settings'.format(highlighter)
                s = sublime.load_settings(fname)
                if all((s.has('auto_complete_triggers'), s.has('extensions'))):
                    break
                auto_complete = [
                    {
                        'characters': '.',
                        'selector': 'source.python - string - constant.numeric',  # noqa
                    }
                ]
                s.set('extensions', ['py'])
                s.set('auto_complete_triggers', auto_complete)
                sublime.save_settings(fname)
                break

    sublime.set_timeout_async(monitor_plugins, 500000)
Esempio n. 9
0
def monitor_plugins():
    """Monitor for any plugin that conflicts with anaconda
    """

    view = sublime.active_window().active_view()
    if not get_settings(view, 'auto_unload_conflictive_plugins', True):
        return

    plist = [
        'Jedi - Python autocompletion',  # breaks auto completion
        'SublimePythonIDE',  # interfere with autocompletion
        'SublimeCodeIntel'  # breaks everything, SCI is a mess
    ]

    for plugin in plist:
        if plugin in sys.modules:
            [
                sublime_plugin.unload_module(m)
                for k, m in sys.modules.items() if plugin in k
            ]
            if plugin not in DISABLED_PLUGINS:
                DISABLED_PLUGINS.append(plugin)

    sublime.set_timeout_async(monitor_plugins, 5 * 60 * 1000)
Esempio n. 10
0
def monitor_plugins():
    """Monitor for any plugin that conflicts with anaconda
    """

    view = sublime.active_window().active_view()
    if not get_settings(view, 'auto_unload_conflictive_plugins', True):
        return

    plist = [
        'Jedi - Python autocompletion',  # breaks auto completion
        'SublimePythonIDE',  # interfere with autocompletion
        'SublimeCodeIntel'  # breaks everything, SCI is a mess
    ]

    for plugin in plist:
        if plugin in sys.modules:
            [
                sublime_plugin.unload_module(m) for k, m in sys.modules.items()
                if plugin in k
            ]
            if plugin not in DISABLED_PLUGINS:
                DISABLED_PLUGINS.append(plugin)

    sublime.set_timeout_async(monitor_plugins, 5*60*1000)
Esempio n. 11
0
def reload_modules(main, modules, perform_reload=True):
    """Implements the machinery for reloading a given plugin module."""
    #
    # Here's the approach in general:
    #
    #   - Hide package modules from the sys.modules temporarily;
    #
    #   - Install a special import hook onto sys.meta_path;
    #
    #   - Call sublime_plugin.reload_plugin(), which imports the main
    #     module under the hood, triggering the hook;
    #
    #   - The hook, instead of creating a new module object, peeks the saved
    #     one and reloads it. Once the module encounters an import statement
    #     requesting another module, not yet reloaded, the hook reenters and
    #     processes that new module recursively, then get back to the previous
    #     one, and so on.
    #
    # This makes the modules reload in the very same order as they were loaded
    # initially, as if they were imported from scratch.
    #
    if perform_reload:
        sublime_plugin.unload_module(main)

    # Insert the main module at the beginning to make the reload
    # order be as close to the order of the "natural" import as possible.
    module_names = [main.__name__] + sorted(name for name in modules
                                            if name != main.__name__)

    # First, remove all the loaded modules from the sys.modules cache,
    # otherwise the reloading hook won't be called.
    loaded_modules = dict(sys.modules)
    for name in loaded_modules:
        if name in modules:
            del sys.modules[name]

    stack_meter = StackMeter()

    @FilteringImportHook.when(condition=lambda name: name in modules)
    def module_reloader(name):
        module = modules[name]
        sys.modules[name] = module  # restore the module back

        if perform_reload:
            with stack_meter as depth:
                dump("reloading ", ("| " * depth) + "|-- ", name)
                try:
                    return module.__loader__.load_module(name)
                except:
                    if name in sys.modules:
                        del sys.modules[name]  # to indicate an error
                    raise
        else:
            if name not in loaded_modules:
                dump("no reload ", "-- ", name)
            return module

    with intercepting_imports(module_reloader), \
            importing_fromlist_aggresively(modules):
        # Now, import all the modules back, in order, starting with the main
        # module. This will reload all the modules directly or indirectly
        # referenced by the main one, i.e. usually most of our modules.
        log(sep="", end="")
        sublime_plugin.reload_plugin(main.__name__)

        # Be sure to bring back *all* the modules that used to be loaded, not
        # only these imported through the main one. Otherwise, some of them
        # might end up being created from scratch as new module objects in
        # case of being imported after detaching the hook. In general, most of
        # the imports below (if not all) are no-ops though.
        for name in module_names:
            importlib.import_module(name)
def reload_modules(main, modules, perform_reload=True):
    """Implements the machinery for reloading a given plugin module."""
    #
    # Here's the approach in general:
    #
    #   - Hide package modules from the sys.modules temporarily;
    #
    #   - Install a special import hook onto sys.meta_path;
    #
    #   - Call sublime_plugin.reload_plugin(), which imports the main
    #     module under the hood, triggering the hook;
    #
    #   - The hook, instead of creating a new module object, peeks the saved
    #     one and reloads it. Once the module encounters an import statement
    #     requesting another module, not yet reloaded, the hook reenters and
    #     processes that new module recursively, then get back to the previous
    #     one, and so on.
    #
    # This makes the modules reload in the very same order as they were loaded
    # initially, as if they were imported from scratch.
    #
    if perform_reload:
        sublime_plugin.unload_module(main)

    # Insert the main module at the beginning to make the reload
    # order be as close to the order of the "natural" import as possible.
    module_names = [main.__name__] + sorted(
        name for name in modules if name != main.__name__)

    # First, remove all the loaded modules from the sys.modules cache,
    # otherwise the reloading hook won't be called.
    loaded_modules = dict(sys.modules)
    for name in loaded_modules:
        if name in modules:
            del sys.modules[name]

    stack_meter = StackMeter()

    @FilteringImportHook.when(condition=lambda name: name in modules)
    def module_reloader(name):
        module = modules[name]
        sys.modules[name] = module  # restore the module back

        if perform_reload:
            with stack_meter as depth:
                dump("reloading ", ("| " * depth) + "|-- ", name)
                try:
                    return module.__loader__.load_module(name)
                except:
                    if name in sys.modules:
                        del sys.modules[name]  # to indicate an error
                    raise
        else:
            if name not in loaded_modules:
                dump("no reload ", "-- ", name)
            return module

    with intercepting_imports(module_reloader), \
            importing_fromlist_aggresively(modules):
        # Now, import all the modules back, in order, starting with the main
        # module. This will reload all the modules directly or indirectly
        # referenced by the main one, i.e. usually most of our modules.
        log(sep="", end="")
        sublime_plugin.reload_plugin(main.__name__)

        # Be sure to bring back *all* the modules that used to be loaded, not
        # only these imported through the main one. Otherwise, some of them
        # might end up being created from scratch as new module objects in
        # case of being imported after detaching the hook. In general, most of
        # the imports below (if not all) are no-ops though.
        for name in module_names:
            importlib.import_module(name)
Esempio n. 13
0
def reload_package(pkg_name, dummy=True, verbose=True, then=None):
    if pkg_name not in sys.modules:
        dprint("error:", pkg_name, "is not loaded.")
        return

    if is_dependency(pkg_name):
        dependencies, packages = resolve_dependencies(pkg_name)
    else:
        dependencies = set()
        packages = {pkg_name}

    if verbose:
        dprint("begin", fill='=')

    all_modules = {
        module_name: module
        for pkg_name in dependencies | packages
        for module_name, module in get_package_modules(pkg_name).items()
    }

    plugins = [
        plugin for pkg_name in packages for plugin in package_plugins(pkg_name)
    ]

    # Tell Sublime to unload plugins
    for plugin in plugins:
        module = sys.modules.get(plugin)
        if module:
            sublime_plugin.unload_module(module)

    # Unload modules
    for module_name in all_modules:
        sys.modules.pop(module_name)

    # Reload packages
    try:
        with intercepting_imports(
                all_modules,
                verbose), importing_fromlist_aggressively(all_modules):
            for plugin in plugins:
                sublime_plugin.reload_plugin(plugin)
    except Exception:
        dprint("reload failed.", fill='-')
        # Rollback modules
        for name, module in all_modules.items():
            sys.modules[name] = module

        # Try reloading again to get the commands back. Here esp. the
        # reload command itself.
        for plugin in plugins:
            sublime_plugin.reload_plugin(plugin)

        traceback.print_exc()
        print(
            '--- Reloading GitSavvy failed. Restarting Sublime is highly recommended. ---'
        )
        sublime.active_window().status_message(
            'GitSavvy reloading 💣ed. 😒.')
        return

    if dummy:
        load_dummy(verbose)

    if verbose:
        dprint("end", fill='-')

    if then:
        then()

    sublime.active_window().status_message('GitSavvy has 🙌 reloaded.')