~ubuntu-branches/ubuntu/saucy/kate/saucy

« back to all changes in this revision

Viewing changes to addons/kate/pate/src/plugins/python_utils/python_checkers/pyflakes_checker.py

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell, Rohan Garg, Jonathan Riddell
  • Date: 2013-06-21 00:48:29 UTC
  • mfrom: (1.1.28)
  • Revision ID: package-import@ubuntu.com-20130621004829-y2ui02eg0j47h94y
Tags: 4:4.10.80-0ubuntu1
[ Rohan Garg ]
* New upstream release
  - Update and sort install files
  - Drop kubuntu_pate_find_python.diff, kubuntu_kate_initial_preference.patch,
    kubuntu_find_python.diff from debian/patches , not required

[ Jonathan Riddell ]
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright (c) 2013 by Pablo Martín <goinnn@gmail.com> and
 
3
# Alejandro Blanco <alejandro.b.e@gmail.com>
 
4
#
 
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.
 
9
#
 
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.
 
14
#
 
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/>.
 
17
 
 
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>
 
20
 
 
21
import _ast
 
22
 
 
23
import kate
 
24
import re
 
25
import sys
 
26
 
 
27
from PyKDE4.kdecore import i18n
 
28
 
 
29
from pyflakes.checker import Checker
 
30
from pyflakes.messages import Message
 
31
 
 
32
from libkatepate.errors import showOk, showErrors
 
33
 
 
34
from python_checkers.all_checker import checkAll
 
35
from python_checkers.utils import canCheckDocument
 
36
from python_settings import KATE_ACTIONS
 
37
 
 
38
encoding_line = re.compile("#( )*-\*-( )*(encoding|coding):( )*(?P<encoding>[\w-]+)( )*-\*-")
 
39
 
 
40
 
 
41
def pyflakes(codeString, filename):
 
42
    """
 
43
    Check the Python source given by C{codeString} for flakes.
 
44
    """
 
45
    # First, compile into an AST and handle syntax errors.
 
46
    try:
 
47
        if sys.version_info.major == 2 and codeString:
 
48
            first_line = codeString.split("\n")[0]
 
49
            m = encoding_line.match(first_line)
 
50
            if m:
 
51
                encoding = m.groupdict().get('encoding', None)
 
52
                if encoding:
 
53
                    codeString = codeString.encode(encoding)
 
54
        tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
 
55
    except SyntaxError as value:
 
56
        msg = value.args[0]
 
57
        lineno = value.lineno
 
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
 
62
            # unknown.
 
63
            msg = i18n("Problem decoding source")
 
64
            lineno = 1
 
65
        error = Message(filename, lineno)
 
66
        error.message = msg + "%s"
 
67
        error.message_args = ""
 
68
        return [error]
 
69
    else:
 
70
        # Okay, it's syntactically valid.  Now check it.
 
71
        w = Checker(tree, filename)
 
72
        return w.messages
 
73
 
 
74
 
 
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):
 
79
        return
 
80
    if refresh:
 
81
        checkAll.f(currentDocument, ['checkPyflakes'],
 
82
                   exclude_all=not currentDocument)
 
83
    move_cursor = not currentDocument
 
84
    currentDocument = currentDocument or kate.activeDocument()
 
85
 
 
86
    path = currentDocument.url().path()
 
87
    mark_key = '%s-pyflakes' % path
 
88
 
 
89
    text = currentDocument.text()
 
90
    errors = pyflakes(text, path)
 
91
    errors_to_show = []
 
92
 
 
93
    if len(errors) == 0:
 
94
        showOk(i18n("Pyflakes Ok"))
 
95
        return
 
96
 
 
97
    # Prepare errors found for painting
 
98
    for error in errors:
 
99
        errors_to_show.append({
 
100
            "message": error.message % error.message_args,
 
101
            "line": error.lineno,
 
102
        })
 
103
 
 
104
    showErrors(i18n('Pyflakes Errors:'), errors_to_show,
 
105
               mark_key, currentDocument,
 
106
               move_cursor=move_cursor)
 
107
 
 
108
# kate: space-indent on; indent-width 4;