Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pep8_check.py
blob: c240237fe59c381378cb2b994f7e7a454c30a45c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python

#   pep8_check.py by/por:
#   Agustin Zubiaga <aguzubiaga97@gmail.com>
#   Daniel Francis <santiago.danielfrancis@gmail.com>
#   Sugarlabs - CeibalJAM! - Uruguay

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import gobject
import gtk

import commands


class PEP8_Check(gobject.GObject):
    __gsignals__ = {"hide-bar": (gobject.SIGNAL_RUN_LAST,
                                 gobject.TYPE_NONE,
                                 tuple()),
                    "show-bar": (gobject.SIGNAL_RUN_LAST,
                                 gobject.TYPE_NONE,
                                 tuple()),
                    "bar-text": (gobject.SIGNAL_RUN_LAST,
                                 gobject.TYPE_NONE,
                                 (gobject.TYPE_STRING,)),}
    def __init__(self):
        gobject.GObject.__init__(self)

    def check_file(self, editor):
        text = editor._get_all_text()
        tmp_file = open("/tmp/jamedit-pep8-chk.py", "w")
        tmp_file.write(text)
        tmp_file.close()

        chk = self.get_check()

        self.highlight_errors(text, chk, editor)

    def highlight_errors(self, text, chk, editor):
        buffer = editor.get_buffer()
        buffer.set_text("")
        num = 0
        for line in text.split("\n"):
            num += 1
            if str(num) in chk:
                line_iter = buffer.get_iter_at_line(num + 2)
                if num == len(text.split("\n")) - 1:
                    buffer.insert_with_tags(line_iter, line, \
                                            editor.error_tag)
                else:
                    buffer.insert_with_tags(line_iter, line + "\n", \
                                            editor.error_tag)
            else:
                line_iter = buffer.get_iter_at_line(num)
                if num == len(text.split("\n")) - 1:
                    buffer.insert_with_tags_by_name(line_iter, line)
                else:
                    buffer.insert_with_tags_by_name(line_iter, \
                                                           line + "\n")
        editor.connect("move-cursor", self.set_bar_text, chk)

    def get_check(self):
        (status, output) = commands.getstatusoutput(
                "pep8 --repeat /tmp/jamedit-pep8-chk.py")
        check = self.interpret_output(output)
        return check

    def interpret_output(self, output):
        checks = {}
        outputs = output.split("\n")
        for out in outputs:
            try:
                splits = out.split(":")
                line = splits[1]
                character = splits[2]
                error = splits[3]
                if line not in checks:
                    checks[line] = '%s:%s' % (character, error)
                else:
                    checks[line] = '%s; %s:%s' % (
                        checks[line], character, error)
            except:
                pass

        return checks

    def set_bar_text(self, widget, step_size, count, extend_selection, check):
        cursor_position = widget.buffer.get_property("cursor-position")
        offset_iter = widget.buffer.get_iter_at_offset(cursor_position)
        line = offset_iter.get_line()
        if str(line) in check:
            this_line_error = check[str(line)]
            char = this_line_error.split(":")[0]
            this_line_error = this_line_error.split(":")[1]
            self.emit("bar-text",
                      str(line) + ":" + char + " " + this_line_error)
            print this_line_error
            self.emit("show-bar")
        else:
            pass

    def check_exit(self, buffer, text):
        buffer.set_text("")
        num = 0
        for line in text.split("\n"):
            num += 1
            line_iter = buffer.get_iter_at_line(num)
            if num == len(text.split("\n")) - 1:
                buffer.insert_with_tags_by_name(line_iter, line)
            else:
                buffer.insert_with_tags_by_name(line_iter, line + "\n")

        self.emit("hide-bar")
        self.emit("bar-text", "")