Skip to content
This repository has been archived by the owner on Apr 1, 2020. It is now read-only.

Commit

Permalink
Unload modules if name == bundle module
Browse files Browse the repository at this point in the history
  • Loading branch information
zrzka committed Oct 4, 2017
1 parent e07c880 commit 32fb44e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 30 deletions.
15 changes: 0 additions & 15 deletions blackmamba/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,24 +177,9 @@ def _check_compatibility_and_updates():
blackmamba.update.check()


def setup_lib_path():
"""
Add blackmamba.lib to sys.path if it's not there.
"""
import sys
import os

lib = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib'))
try:
sys.path.index(lib)
except ValueError:
sys.path.insert(0, lib)


@system.Pythonista()
@system.catch_exceptions
def _main(config=None):
setup_lib_path()
# It's here because Sphinx doesn't show documentation for decorated
# functions
from blackmamba.config import update_config_with_dict, get_config_value
Expand Down
51 changes: 51 additions & 0 deletions blackmamba/module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!python

import sys
import os


_BUNDLED_MODULES = [
'mccabe', 'flake8', 'docutils', 'pep8', 'pycodestyle', 'pyflakes', 'setuptools'
]


def _bundled_modules_path():
return os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib'))


def _setup_bundled_modules_path():
path = _bundled_modules_path()
try:
sys.path.index(path)
except ValueError:
sys.path.insert(0, path)


def _unload_modules(modules_to_unload, force=False):
from blackmamba.log import debug

lib = _bundled_modules_path()
for mod_name in list(sys.modules.keys()):
name = mod_name.split('.')[0]
if name in modules_to_unload:
mod = sys.modules[mod_name]

if not hasattr(mod, '__file__'):
continue

mod_path = mod.__file__

if mod_path.startswith(lib) and not force:
debug('Skipping {}'.format(mod_name))
continue

if not os.access(mod_path, os.W_OK):
continue

debug('Unloading module {}'.format(mod_name))
del sys.modules[mod_name]


def preflight(force=False):
_unload_modules(_BUNDLED_MODULES, force)
_setup_bundled_modules_path()
32 changes: 17 additions & 15 deletions blackmamba/script/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
This script is configurable, see :ref:`configuration`.
"""

import blackmamba
blackmamba.setup_lib_path()
import blackmamba.module as module

module.preflight()

import io
import pep8
import re
from enum import Enum
import pyflakes.api as pyflakes
import editor
import console
from blackmamba.ide.annotation import Annotation, Style
Expand Down Expand Up @@ -78,21 +77,22 @@ def __lt__(self, other):
# pep8
#

class _Pep8AnnotationReport(pep8.BaseReport):
def __init__(self, options):
super().__init__(options)
self.annotations = []
def _pep8_annotations(text, ignore=None, max_line_length=None):
import pep8

def error(self, line_number, offset, text, check):
# If super doesn't return code, this one is ignored
if not super().error(line_number, offset, text, check):
return
class _Pep8AnnotationReport(pep8.BaseReport):
def __init__(self, options):
super().__init__(options)
self.annotations = []

annotation = _AnalyzerAnnotation(self.line_offset + line_number, text, _Source.pep8, Style.warning)
self.annotations.append(annotation)
def error(self, line_number, offset, text, check):
# If super doesn't return code, this one is ignored
if not super().error(line_number, offset, text, check):
return

annotation = _AnalyzerAnnotation(self.line_offset + line_number, text, _Source.pep8, Style.warning)
self.annotations.append(annotation)

def _pep8_annotations(text, ignore=None, max_line_length=None):
# pep8 requires you to include \n at the end of lines
lines = text.splitlines(True)

Expand Down Expand Up @@ -157,6 +157,8 @@ def _get_annotations(path, stream, style):


def _pyflakes_annotations(path, text):
import pyflakes.api as pyflakes

warning_stream = io.StringIO()
error_stream = io.StringIO()
reporter = pyflakes.modReporter.Reporter(warning_stream, error_stream)
Expand Down

0 comments on commit 32fb44e

Please sign in to comment.