8000 Update pylama. Fix #438 · python-mode/python-mode@ca0078b · GitHub
[go: up one dir, main page]

Skip to content

Commit ca0078b

Browse files
committed
Update pylama. Fix #438
1 parent 304a68e commit ca0078b

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

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__ = "4.0.1"
8+
__version__ = "5.0.1"
99
__project__ = "pylama"
1010
__author__ = "Kirill Klenov <horneds@gmail.com>"
1111
__license__ = "GNU LGPL"

pymode/libs/pylama/errors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
# multiple statements on one line
3333
[('pylint', 'C0321'), ('pep8', 'E702')],
3434

35+
# bad indentation
36+
[('pylint', 'W0311'), ('pep8', 'E111')],
37+
3538
)
3639

3740
DUPLICATES = dict((key, values) for values in DUPLICATES for key in values)
@@ -57,6 +60,10 @@ def __getattr__(self, name):
5760
def __getitem__(self, name):
5861
return self._info[name]
5962

63+
def get(self, name, default=None):
64+
""" Implement dictionary `get` method. """
65+
return self._info.get(name, default)
66+
6067
def __repr__(self):
6168
return "<Error: %s %s>" % (self.number, self.linter)
6269

pymode/libs/pylama/pytest.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
""" py.test plugin for checking files with pylama. """
2+
from __future__ import absolute_import
3+
4+
from os import path as op
5+
6+
import py
7+
import pytest
8+
9+
10+
HISTKEY = "pylama/mtimes"
11+
12+
13+
def pytest_addoption(parser):
14+
group = parser.getgroup("general")
15+
group.addoption(
16+
'--pylama', action='store_true',
17+
help="perform some pylama code checks on .py files")
18+
19+
20+
def pytest_sessionstart(session):
21+
config = session.config
22+
if config.option.pylama and getattr(config, 'cache', None):
23+
config._pylamamtimes = config.cache.get(HISTKEY, {})
24+
25+
26+
def pytest_sessionfinish(session):
27+
config = session.config
28+
if hasattr(config, "_pylamamtimes"):
29+
config.cache.set(HISTKEY, config._pylamamtimes)
30+
31+
32+
def pytest_collect_file(path, parent):
33+
config = parent.config
34+
if config.option.pylama and path.ext == '.py':
35+
return PylamaItem(path, parent)
36+
37+
38+
class PylamaError(Exception):
39+
""" indicates an error during pylama checks. """
40+
41+
42+
class PylamaItem(pytest.Item, pytest.File):
43+
44+
def __init__(self, path, parent):
45+
super(PylamaItem, self).__init__(path, parent)
46+
self.add_marker("pep8")
47+
self.cache = None
48+
self._pylamamtimes = None
49+
50+
def setup(self):
51+
if not getattr(self.config, 'cache', None):
52+
return False
53+
54+
self.cache = True
55+
self._pylamamtimes = self.fspath.mtime()
56+
pylamamtimes = self.config._pylamamtimes
57+
old = pylamamtimes.get(str(self.fspath), 0)
58+
if old == self._pylamamtimes:
59+
pytest.skip("file(s) previously passed Pylama checks")
60+
61+
def runtest(self):
62+
call = py.io.StdCapture.call
63+
errors, out, err = call(check_file, self.fspath)
64+
# errors = check_file(self.fspath)
65+
if errors:
66+
raise PylamaError(out, err)
67+
# update mtime only if test passed
68+
# otherwise failures would not be re-run next time
69+
if self.cache:
70+
self.config._pylamamtimes[str(self.fspath)] = self._pylamamtimes
71+
72+
def repr_failure(self, excinfo):
73+
if excinfo.errisinstance(PylamaError):
74+
return excinfo.value.args[0]
75+
return super(PylamaItem, self).repr_failure(excinfo)
76+
77+
78+
def check_file(path):
79+
from pylama.main import parse_options, check_files
80+
from pylama.config import CURDIR
81+
82+
options = parse_options()
83+
path = op.relpath(str(path), CURDIR)
84+
return check_files([path], options, error=False)
85+
86+
# pylama:ignore=D,E1002,W0212

0 commit comments

Comments
 (0)
0