Tue, 15 Apr 2014 22:41:08 +0200
Merge with default branch.
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
1 | # -*- coding: utf-8 -*- |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
2 | |
3161
06f57a834adf
Updated copyright for 2014.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
3065
diff
changeset
|
3 | # Copyright (c) 2011 - 2014 Detlev Offenbach <detlev@die-offenbachs.de> |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
4 | # |
3173
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
5 | # pylint: disable=C0103 |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
6 | |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
7 | """ |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
8 | Module implementing the syntax check for Python 2/3. |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
9 | """ |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
10 | import re |
3159
02cb2adb4868
First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3065
diff
changeset
|
11 | import sys |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
12 | import traceback |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
13 | |
3173
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
14 | try: |
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
15 | from pyflakes.checker import Checker |
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
16 | from pyflakes.messages import ImportStarUsed |
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
17 | except ImportError: |
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
18 | pass |
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
19 | |
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
20 | |
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
21 | def initService(): |
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
22 | """ |
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
23 | Initialize the service and return the entry point. |
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
24 | |
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
25 | @return the entry point for the background client (function) |
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
26 | """ |
1fb284abe46e
Interface to add user-defined services, e.g. in plugins. Auto syntax check working. Little cleanup.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3159
diff
changeset
|
27 | return syntaxAndPyflakesCheck |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
28 | |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
29 | |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
30 | def normalizeCode(codestring): |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
31 | """ |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
32 | Function to normalize the given code. |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
33 | |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
34 | @param codestring code to be normalized (string) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
35 | @return normalized code (string) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
36 | """ |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
37 | codestring = codestring.replace("\r\n", "\n").replace("\r", "\n") |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
38 | |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
39 | if codestring and codestring[-1] != '\n': |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
40 | codestring = codestring + '\n' |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
41 | |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
42 | # Check type for py2: if not str it's unicode |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
43 | if sys.version_info[0] == 2: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
44 | try: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
45 | codestring = codestring.encode('utf-8') |
3159
02cb2adb4868
First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3065
diff
changeset
|
46 | except UnicodeError: |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
47 | pass |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
48 | |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
49 | return codestring |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
50 | |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
51 | |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
52 | def extractLineFlags(line, startComment="#", endComment=""): |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
53 | """ |
3065
070b35dde35e
Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
2571
diff
changeset
|
54 | Function to extract flags starting and ending with '__' from a line |
070b35dde35e
Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
2571
diff
changeset
|
55 | comment. |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
56 | |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
57 | @param line line to extract flags from (string) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
58 | @keyparam startComment string identifying the start of the comment (string) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
59 | @keyparam endComment string identifying the end of a comment (string) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
60 | @return list containing the extracted flags (list of strings) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
61 | """ |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
62 | flags = [] |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
63 | |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
64 | pos = line.rfind(startComment) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
65 | if pos >= 0: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
66 | comment = line[pos + len(startComment):].strip() |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
67 | if endComment: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
68 | comment = comment.replace("endComment", "") |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
69 | flags = [f.strip() for f in comment.split() |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
70 | if (f.startswith("__") and f.endswith("__"))] |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
71 | return flags |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
72 | |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
73 | |
3412
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
74 | def syntaxAndPyflakesCheck(filename, codestring, checkFlakes=True, |
3159
02cb2adb4868
First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3065
diff
changeset
|
75 | ignoreStarImportWarnings=False): |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
76 | """ |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
77 | Function to compile one Python source file to Python bytecode |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
78 | and to perform a pyflakes check. |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
79 | |
3159
02cb2adb4868
First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3065
diff
changeset
|
80 | @param filename source filename (string) |
3412
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
81 | @param codestring string containing the code to compile (string) |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
82 | @keyparam checkFlakes flag indicating to do a pyflakes check (boolean) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
83 | @keyparam ignoreStarImportWarnings flag indicating to |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
84 | ignore 'star import' warnings (boolean) |
3412
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
85 | @return dictionary with the keys 'error' and 'warnings' which |
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
86 | hold a list containing details about the error/ warnings |
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
87 | (file name, line number, column, codestring (only at syntax |
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
88 | errors), the message, a list with arguments for the message) |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
89 | """ |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
90 | try: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
91 | import builtins |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
92 | except ImportError: |
3515
1b8381afe38f
Merge with default branch.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3456
diff
changeset
|
93 | import __builtin__ as builtins # __IGNORE_WARNING__ |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
94 | |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
95 | try: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
96 | if sys.version_info[0] == 2: |
3159
02cb2adb4868
First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3065
diff
changeset
|
97 | file_enc = filename.encode(sys.getfilesystemencoding()) |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
98 | else: |
3159
02cb2adb4868
First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3065
diff
changeset
|
99 | file_enc = filename |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
100 | |
3412
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
101 | # It also encode the code back to avoid 'Encoding declaration in |
3159
02cb2adb4868
First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3065
diff
changeset
|
102 | # unicode string' exception on Python2 |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
103 | codestring = normalizeCode(codestring) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
104 | |
3159
02cb2adb4868
First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3065
diff
changeset
|
105 | if filename.endswith('.ptl'): |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
106 | try: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
107 | import quixote.ptl_compile |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
108 | except ImportError: |
3412
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
109 | return [{'error': (filename, 0, 0, '', |
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
110 | 'Quixote plugin not found.')}] |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
111 | template = quixote.ptl_compile.Template(codestring, file_enc) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
112 | template.compile() |
3412
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
113 | else: |
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
114 | # ast.PyCF_ONLY_AST = 1024, speed optimisation |
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
115 | module = builtins.compile(codestring, file_enc, 'exec', 1024) |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
116 | except SyntaxError as detail: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
117 | index = 0 |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
118 | code = "" |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
119 | error = "" |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
120 | lines = traceback.format_exception_only(SyntaxError, detail) |
3159
02cb2adb4868
First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3065
diff
changeset
|
121 | if sys.version_info[0] == 2: |
02cb2adb4868
First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3065
diff
changeset
|
122 | lines = [x.decode(sys.getfilesystemencoding()) for x in lines] |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
123 | match = re.match('\s*File "(.+)", line (\d+)', |
3159
02cb2adb4868
First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3065
diff
changeset
|
124 | lines[0].replace('<string>', '{0}'.format(filename))) |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
125 | if match is not None: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
126 | fn, line = match.group(1, 2) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
127 | if lines[1].startswith('SyntaxError:'): |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
128 | error = re.match('SyntaxError: (.+)', lines[1]).group(1) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
129 | else: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
130 | code = re.match('(.+)', lines[1]).group(1) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
131 | for seLine in lines[2:]: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
132 | if seLine.startswith('SyntaxError:'): |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
133 | error = re.match('SyntaxError: (.+)', seLine).group(1) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
134 | elif seLine.rstrip().endswith('^'): |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
135 | index = len(seLine.rstrip()) - 4 |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
136 | else: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
137 | fn = detail.filename |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
138 | line = detail.lineno or 1 |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
139 | error = detail.msg |
3412
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
140 | return [{'error': (fn, int(line), index, code.strip(), error)}] |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
141 | except ValueError as detail: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
142 | try: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
143 | fn = detail.filename |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
144 | line = detail.lineno |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
145 | error = detail.msg |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
146 | except AttributeError: |
3159
02cb2adb4868
First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3065
diff
changeset
|
147 | fn = filename |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
148 | line = 1 |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
149 | error = str(detail) |
3412
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
150 | return [{'error': (fn, line, 0, "", error)}] |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
151 | except Exception as detail: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
152 | try: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
153 | fn = detail.filename |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
154 | line = detail.lineno |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
155 | error = detail.msg |
3412
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
156 | return [{'error': (fn, line, 0, "", error)}] |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
157 | except: # this catchall is intentional |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
158 | pass |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
159 | |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
160 | # pyflakes |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
161 | if not checkFlakes: |
3412
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
162 | return [{}] |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
163 | |
3412
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
164 | results = [] |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
165 | lines = codestring.splitlines() |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
166 | try: |
3159
02cb2adb4868
First implementation for the BackgroundService.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3065
diff
changeset
|
167 | warnings = Checker(module, filename) |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
168 | warnings.messages.sort(key=lambda a: a.lineno) |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
169 | for warning in warnings.messages: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
170 | if ignoreStarImportWarnings and \ |
3065
070b35dde35e
Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
2571
diff
changeset
|
171 | isinstance(warning, ImportStarUsed): |
070b35dde35e
Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
2571
diff
changeset
|
172 | continue |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
173 | |
3177
5af61402d74d
Update pyflakes to 0.7.3
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3173
diff
changeset
|
174 | _fn, lineno, col, message, msg_args = warning.getMessageData() |
3065
070b35dde35e
Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
2571
diff
changeset
|
175 | if "__IGNORE_WARNING__" not in extractLineFlags( |
070b35dde35e
Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
2571
diff
changeset
|
176 | lines[lineno - 1].strip()): |
3412
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
177 | results.append((_fn, lineno, col, "", message, msg_args)) |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
178 | except SyntaxError as err: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
179 | if err.text.strip(): |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
180 | msg = err.text.strip() |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
181 | else: |
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
182 | msg = err.msg |
3412
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
183 | results.append((filename, err.lineno, 0, "FLAKES_ERROR", msg, [])) |
2571
e6bb19eb87ea
Fixes for autocodecheck with pyflakes and update of py2flakes to 0.6.1.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff
changeset
|
184 | |
3412
9364dab2d472
Plug-in docu updated, now the return values of the syntax checker is a dictionary
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
3228
diff
changeset
|
185 | return [{'warnings': results}] |