Skip to content

Commit 8a0bcc8

Browse files
committed
Update pymode
1 parent f848054 commit 8a0bcc8

File tree

166 files changed

+180
-89
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+180
-89
lines changed

Makefile

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
PYMODE = $(CURDIR)/pymode
2+
LIBS = $(PYMODE)/libs
3+
PYLAMA = $(LIBS)/pylama
4+
15
.PHONY: clean
26
clean:
37
find . -name "*.pyc" -delete
@@ -9,6 +13,12 @@ test:
913

1014
.PHONY: pylama
1115
pylama:
12-
rm -rf pylibs/pylama
13-
cp -r ~/Dropbox/projects/pylama/pylama pylibs/pylama
14-
cp -r ~/Dropbox/projects/pylama/plugins/pylama_pylint/pylama_pylint/ pylibs/pylama/lint/pylama_pylint
16+
rm -rf $(PYLAMA)
17+
make $(PYLAMA)
18+
make $(PYLAMA)/lint/pylama_pylint
19+
20+
$(PYLAMA):
21+
cp -r ~/Dropbox/projects/pylama/pylama $(PYLAMA)
22+
23+
$(PYLAMA)/lint/pylama_pylint:
24+
cp -r ~/Dropbox/projects/pylama/plugins/pylama_pylint/pylama_pylint/ $(PYLAMA)/lint/pylama_pylint

pymode/pylama/__init__.py renamed to pymode/libs/pylama/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
"""
77

8-
version_info = 2, 0, 1
8+
version_info = 2, 0, 3
99

1010
__version__ = version = '.'.join(map(str, version_info))
1111
__project__ = __name__

pymode/pylama/config.py renamed to pymode/libs/pylama/config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
from . import version
1010
from .core import LOGGER, STREAM
11-
from .inirama import Namespace
12-
from .lint import LINTERS
11+
from .libs.inirama import Namespace
12+
from .lint.extensions import LINTERS
1313

1414

1515
#: A default checkers
@@ -127,6 +127,8 @@ def parse_linters(csp_str):
127127
linter = LINTERS.get(name)
128128
if linter:
129129
result.append((name, linter))
130+
else:
131+
logging.warn("Linter `%s` not found." % name)
130132
return result
131133

132134
parser.add_argument(

pymode/pylama/core.py renamed to pymode/libs/pylama/core.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66
import logging
77
import re
8-
from .lint import LINTERS
8+
from .lint.extensions import LINTERS
99

1010
#: The skip pattern
1111
SKIP_PATTERN = re.compile(r'# *noqa\b', re.I).search
@@ -20,7 +20,9 @@
2020
LOGGER.addHandler(STREAM)
2121

2222

23-
def run(path, ignore=None, select=None, linters=None, config=None, **meta):
23+
def run(
24+
path, ignore=None, select=None, linters=None, config=None, code=None,
25+
**meta):
2426
""" Run a code checkers with given params.
2527
2628
:return errors: list of dictionaries with error's information
@@ -29,11 +31,9 @@ def run(path, ignore=None, select=None, linters=None, config=None, **meta):
2931
errors = []
3032
linters = linters or LINTERS.items()
3133
params = dict(ignore=ignore, select=select)
32-
code = None
3334
try:
34-
with open(path, 'rU') as f:
35-
code = f.read() + '\n\n'
36-
35+
with CodeContext(code, path) as ctx:
36+
code = ctx.code
3737
params = prepare_params(
3838
parse_modeline(code), config, ignore=ignore, select=select
3939
)
@@ -78,7 +78,7 @@ def run(path, ignore=None, select=None, linters=None, config=None, **meta):
7878

7979
errors = [er for er in errors if filter_errors(er, **params)]
8080

81-
if code:
81+
if code and errors:
8282
errors = filter_skiplines(code, errors)
8383

8484
return sorted(errors, key=lambda x: x['lnum'])
@@ -158,3 +158,23 @@ def filter_skiplines(code, errors):
158158
errors = [er for er in errors if not er['lnum'] in removed]
159159

160160
return errors
161+
162+
163+
class CodeContext(object):
164+
165+
""" Read file if code is None. """
166+
167+
def __init__(self, code, path):
168+
self.code = code
169+
self.path = path
170+
self._file = None
171+
172+
def __enter__(self):
173+
if self.code is None:
174+
self._file = open(self.path, 'rU')
175+
self.code = self._file.read() + '\n\n'
176+
return self
177+
178+
def __exit__(self):
179+
if not self._file is None:
180+
self._file.close()

pymode/pylama/hook.py renamed to pymode/libs/pylama/hook.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ def run(command):
2929

3030
def git_hook():
3131
""" Run pylama after git commit. """
32-
3332
from .main import check_files
33+
3434
_, files_modified, _ = run("git diff-index --cached --name-only HEAD")
3535

3636
options = parse_options()
3737
setup_logger(options)
38-
check_files(
39-
[f for f in map(str, files_modified) if f.endswith('.py')], options
40-
)
38+
check_files([f for f in map(str, files_modified)], options)
4139

4240

4341
def hg_hook(ui, repo, node=None, **kwargs):
@@ -53,8 +51,7 @@ def hg_hook(ui, repo, node=None, **kwargs):
5351
if file_ in seen or not op.exists(file_):
5452
continue
5553
seen.add(file_)
56-
if file_.endswith('.py'):
57-
paths.append(file_)
54+
paths.append(file_)
5855

5956
options = parse_options()
6057
setup_logger(options)

pymode/libs/pylama/libs/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
""" Support libs. """

pymode/libs/pylama/libs/importlib.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Backport of importlib.import_module from 3.x."""
2+
# While not critical (and in no way guaranteed!), it would be nice to keep this
3+
# code compatible with Python 2.3.
4+
import sys
5+
6+
def _resolve_name(name, package, level):
7+
"""Return the absolute name of the module to be imported."""
8+
if not hasattr(package, 'rindex'):
9+
raise ValueError("'package' not set to a string")
10+
dot = len(package)
11+
for x in xrange(level, 1, -1):
12+
try:
13+
dot = package.rindex('.', 0, dot)
14+
except ValueError:
15+
raise ValueError("attempted relative import beyond top-level "
16+
"package")
17+
return "%s.%s" % (package[:dot], name)
18+
19+
20+
def import_module(name, package=None):
21+
"""Import a module.
22+
23+
The 'package' argument is required when performing a relative import. It
24+
specifies the package to use as the anchor point from which to resolve the
25+
relative import to an absolute import.
26+
27+
"""
28+
if name.startswith('.'):
29+
if not package:
30+
raise TypeError("relative imports require the 'package' argument")
31+
level = 0
32+
for character in name:
33+
if character != '.':
34+
break
35+
level += 1
36+
name = _resolve_name(name[level:], package, level)
37+
__import__(name)
38+
return sys.modules[name]
File renamed without changes.

pymode/libs/pylama/lint/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
""" Custom module loader. """
2+
3+
4+
class Linter(object): # noqa
5+
6+
""" Abstract class for linter plugin. """
7+
8+
@staticmethod
9+
def allow(path):
10+
""" Check path is relevant for linter.
11+
12+
:return bool:
13+
14+
"""
15+
16+
return path.endswith('.py')
17+
18+
@staticmethod
19+
def run(path, **meta):
20+
""" Method 'run' should be defined. """
21+
22+
raise NotImplementedError(__doc__)

pymode/libs/pylama/lint/extensions.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
""" Load extensions. """
2+
3+
from os import listdir, path as op
4+
5+
6+
CURDIR = op.dirname(__file__)
7+
LINTERS = dict()
8+
PREFIX = 'pylama_'
9+
10+
try:
11+
from importlib import import_module
12+
except ImportError:
13+
from ..libs.importlib import import_module
14+
15+
for p in listdir(CURDIR):
16+
if p.startswith(PREFIX) and op.isdir(op.join(CURDIR, p)):
17+
name = p[len(PREFIX):]
18+
module = import_module('.lint.%s%s' % (PREFIX, name), 'pylama')
19+
LINTERS[name] = getattr(module, 'Linter')()
20+
21+
try:
22+
from pkg_resources import iter_entry_points
23+
24+
for entry in iter_entry_points('pylama.linter'):
25+
LINTERS[entry.name] = entry.load()()
26+
except ImportError:
27+
pass

0 commit comments

Comments
 (0)