summaryrefslogtreecommitdiff
path: root/external/plyer/tools/pep8checker/pep8kivy.py
blob: 245928e572ec7defb497f16972c11d59cc63d07c (plain) (blame)
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
import sys
from os import walk
from os.path import isdir, join, abspath, dirname
import pep8
import time

htmlmode = False

pep8_ignores = (
    'E125',  # continuation line does not
             # distinguish itself from next logical line
    'E126',  # continuation line over-indented for hanging indent
    'E127',  # continuation line over-indented for visual indent
    'E128')  # continuation line under-indented for visual indent

class KivyStyleChecker(pep8.Checker):

    def __init__(self, filename):
        pep8.Checker.__init__(self, filename, ignore=pep8_ignores)

    def report_error(self, line_number, offset, text, check):
        if htmlmode is False:
            return pep8.Checker.report_error(self,
                line_number, offset, text, check)

        # html generation
        print('<tr><td>{0}</td><td>{1}</td></tr>'.format(line_number, text))


if __name__ == '__main__':

    def usage():
        print('Usage: python pep8kivy.py [-html] <file_or_folder_to_check>*')
        print('Folders will be checked recursively.')
        sys.exit(1)

    if len(sys.argv) < 2:
        usage()
    if sys.argv[1] == '-html':
        if len(sys.argv) < 3:
            usage()
        else:
            htmlmode = True
            targets = sys.argv[-1].split()
    elif sys.argv == 2:
        targets = sys.argv[-1]
    else:
        targets = sys.argv[-1].split()

    def check(fn):
        try:
            checker = KivyStyleChecker(fn)
        except IOError:
            # File couldn't be opened, so was deleted apparently.
            # Don't check deleted files.
            return 0
        return checker.check_all()

    errors = 0
    exclude_dirs = ['/lib', '/coverage', '/pep8', '/doc']
    exclude_files = ['kivy/gesture.py', 'osx/build.py', 'win32/build.py',
                     'kivy/tools/stub-gl-debug.py',
                     'kivy/modules/webdebugger.py',
                     'kivy/modules/_webdebugger.py']
    for target in targets:
        if isdir(target):
            if htmlmode:
                path = join(dirname(abspath(__file__)), 'pep8base.html')
                print(open(path, 'r').read())
                print('''<p>Generated: %s</p><table>''' % (time.strftime('%c')))

            for dirpath, dirnames, filenames in walk(target):
                cont = False
                for pat in exclude_dirs:
                    if pat in dirpath:
                        cont = True
                        break
                if cont:
                    continue
                for filename in filenames:
                    if not filename.endswith('.py'):
                        continue
                    cont = False
                    complete_filename = join(dirpath, filename)
                    for pat in exclude_files:
                        if complete_filename.endswith(pat):
                            cont = True
                    if cont:
                        continue

                    if htmlmode:
                        print('<tr><th colspan="2">%s</td></tr>' \
                             % complete_filename)
                    errors += check(complete_filename)

            if htmlmode:
                print('</div></div></table></body></html>')

        else:
            # Got a single file to check
            for pat in exclude_dirs + exclude_files:
                if pat in target:
                    break
            else:
                if target.endswith('.py'):
                    errors += check(target)

    # If errors is 0 we return with 0. That's just fine.
    sys.exit(errors)