Premier commit : Code et doc (état de publication)
[gedit/flake8] / flake8_integration / integrated.py
1 #-*- coding: utf8 -*-
2
3 """Module used to manage the results of flake8 :
4
5 > 1. get them instead of print them ;
6 > 2. format them ;
7
8 This module works with Python2 and Python3
9
10 :author: Sébastien CHAZALLET <s.chazallet@gmail.com>
11 :organization: InsPyration EURL
12 :copyright: Copyright © InsPyration EURL <www.inspyration.org>
13 :license: GPL 3 <http://www.gnu.org/licenses/gpl.html>
14
15 :version: 0.1
16 """
17
18
19 import sys
20 if sys.version_info.major == 2:
21     from io import BytesIO as IO
22 else:
23     from io import StringIO as IO
24
25 from flake8.main import check_code
26
27
28 def _intercept_printed(action, *args, **kwargs):
29     """Function used to redirect printed to returned value
30
31     :type  action: function
32     :param action: Called function that we intercept prints
33
34     :rtype:  str
35     :return: all that was printed by the call of action
36
37     >>> def do():
38     ...     print('Hello World')
39     ...
40     >>> result = _intercept_printed(do)
41     >>> print(result)
42     Hello World
43     <BLANKLINE>
44     """
45     with IO() as buff:
46         out, sys.stdout = sys.stdout, buff
47         action(*args, **kwargs)
48         sys.stdout = out
49         buff.seek(0)
50         return buff.read()
51
52
53 def _format_flake8_results(warnings):
54     """Function used to extract useful informations from flake8 results
55
56     :type  warnings: str
57     :param warnings: Warnings generated by flake8
58
59     :rtype:  list
60     :return: list of (code:str, line:int, col:int, description:str)
61
62     >>> result = _format_flake8_results(
63     ...     '''stdin:1:10: E901 SyntaxError: unexpected EOF while parsing
64     ...     stdin:2:1: E901 TokenError: EOF in multi-line statement''')
65     >>> for r in result:
66     ...     print r
67     ...
68     (' e901', 1, 10, 'SyntaxError: unexpected EOF while parsing')
69     (' e901', 2, 1, 'TokenError: EOF in multi-line statement')
70     """
71     result = []
72     for warning in warnings.splitlines():
73         cols = warning.split(':')
74         result.append((cols[3][:5].lower(),
75                        int(cols[1]),
76                        int(cols[2]),
77                        cols[3][6:] + ':' + ':'.join(cols[4:])))
78     return result
79
80
81 def check_code_and_get_formated_result(code, ignore=(), complexity=-1):
82     """Function used to return the printed result of flake8 check_code function
83
84     >>> result = check_code_with_flake8("a = 42")
85     >>> print(result)
86     <BLANKLINE>
87     >>> result = check_code_with_flake8("a = f(42")
88     >>> print(result)
89     stdin:1:10: E901 SyntaxError: unexpected EOF while parsing
90     stdin:2:1: E901 TokenError: EOF in multi-line statement
91     <BLANKLINE>
92     """
93     return _format_flake8_results(
94         _intercept_printed(
95             check_code,
96             **{"code": code, "ignore": ignore, "complexity": complexity}))
97
98
99 if __name__ == '__main__':
100     import doctest
101     doctest.testmod()
102