File: _odoo_checkers.py

package info (click to toggle)
oca-core 11.0.20180730-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 509,684 kB
  • sloc: xml: 258,806; python: 164,081; sql: 217; sh: 92; makefile: 16
file content (44 lines) | stat: -rw-r--r-- 1,412 bytes parent folder | download
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tokenize

from pylint import checkers, interfaces

class PEP3110TokenChecker(checkers.BaseTokenChecker):
    __implements__ = interfaces.ITokenChecker
    name = 'python3'
    enabled = False

    msgs = {
        'E3110': ('Python 3 uses `as` instead of comma token to catch exceptions',
                  'no-comma-exception',
                  'See http://www.python.org/dev/peps/pep-3110/',
                  {'maxversion': (3, 0)}),
    }

    def process_tokens(self, tokens):
        comma_found = in_except = False
        pcount = 0
        for tok_type, token, start, _, _ in tokens:
            if tok_type == tokenize.NAME and token == 'except':
                in_except = True
                comma_found = False
                pcount = 0

            elif tok_type == tokenize.OP and in_except:
                if token == '(':
                    pcount += 1
                elif token == ')':
                    pcount -= 1
                elif token == ',' and pcount == 0:
                    comma_found = True
                if token == ':':
                    if in_except and comma_found:
                        self.add_message('no-comma-exception', line=start[0])
                    comma_found = in_except = False
                    pcount = 0


def register(linter):
    linter.register_checker(PEP3110TokenChecker(linter))