1
# -*- coding: utf-8 -*-
2
# Copyright (c) 2013 by Pablo Martín <goinnn@gmail.com> and
3
# Alejandro Blanco <alejandro.b.e@gmail.com>
5
# This software is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
10
# This software is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU Lesser General Public License for more details.
15
# You should have received a copy of the GNU Lesser General Public License
16
# along with this software. If not, see <http://www.gnu.org/licenses/>.
18
# This file originally was in this repository:
19
# <https://github.com/goinnn/Kate-plugins/tree/master/kate_plugins/pyte_plugins/check_plugins/pyflakes_plugins.py>
27
from PyKDE4.kdecore import i18n
29
from pyflakes.checker import Checker
30
from pyflakes.messages import Message
32
from libkatepate.errors import showOk, showErrors
34
from python_checkers.all_checker import checkAll
35
from python_checkers.utils import canCheckDocument
36
from python_settings import KATE_ACTIONS
38
encoding_line = re.compile("#( )*-\*-( )*(encoding|coding):( )*(?P<encoding>[\w-]+)( )*-\*-")
41
def pyflakes(codeString, filename):
43
Check the Python source given by C{codeString} for flakes.
45
# First, compile into an AST and handle syntax errors.
47
if sys.version_info.major == 2 and codeString:
48
first_line = codeString.split("\n")[0]
49
m = encoding_line.match(first_line)
51
encoding = m.groupdict().get('encoding', None)
53
codeString = codeString.encode(encoding)
54
tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
55
except SyntaxError as value:
58
# If there's an encoding problem with the file, the text is None.
59
if value.text is None:
60
# Avoid using msg, since for the only known case, it contains a
61
# bogus message that claims the encoding the file declared was
63
msg = i18n("Problem decoding source")
65
error = Message(filename, lineno)
66
error.message = msg + "%s"
67
error.message_args = ""
70
# Okay, it's syntactically valid. Now check it.
71
w = Checker(tree, filename)
75
@kate.action(**KATE_ACTIONS['checkPyflakes'])
76
def checkPyflakes(currentDocument=None, refresh=True):
77
"""Check the pyflakes errors of the document"""
78
if not canCheckDocument(currentDocument):
81
checkAll.f(currentDocument, ['checkPyflakes'],
82
exclude_all=not currentDocument)
83
move_cursor = not currentDocument
84
currentDocument = currentDocument or kate.activeDocument()
86
path = currentDocument.url().path()
87
mark_key = '%s-pyflakes' % path
89
text = currentDocument.text()
90
errors = pyflakes(text, path)
94
showOk(i18n("Pyflakes Ok"))
97
# Prepare errors found for painting
99
errors_to_show.append({
100
"message": error.message % error.message_args,
101
"line": error.lineno,
104
showErrors(i18n('Pyflakes Errors:'), errors_to_show,
105
mark_key, currentDocument,
106
move_cursor=move_cursor)
108
# kate: space-indent on; indent-width 4;