~ubuntu-branches/ubuntu/trusty/pylint/trusty

« back to all changes in this revision

Viewing changes to checkers/misc.py

  • Committer: Bazaar Package Importer
  • Author(s): Sylvain Thénault
  • Date: 2006-09-25 16:46:40 UTC
  • mfrom: (1.2.1 upstream) (2.1.4 feisty)
  • Revision ID: james.westby@ubuntu.com-20060925164640-obkb6g34gqtyk20n
new uptream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
Check source code is ascii only or has an encoding declaration (PEP 263)
18
18
"""
19
19
 
20
 
__revision__ = '$Id: misc.py,v 1.17 2005/01/20 15:12:55 syt Exp $'
 
20
__revision__ = '$Id: misc.py,v 1.19 2005-11-02 09:21:47 syt Exp $'
21
21
 
22
22
import re
23
23
 
24
 
from logilab.pylint.interfaces import IRawChecker
25
 
from logilab.pylint.checkers import BaseChecker
 
24
from pylint.interfaces import IRawChecker
 
25
from pylint.checkers import BaseChecker
26
26
 
27
27
def is_ascii(string):
28
28
    """return true if non ascii characters are detected in the given string
31
31
        return max([ord(char) for char in string]) < 128
32
32
    return True
33
33
    
34
 
 
35
 
EMACS_ENCODING_RGX = re.compile('[^#]*[#\s]*-\*-\s*coding: ([^\s]*)\s*-\*-\s*')
36
 
VIM_ENCODING_RGX = re.compile('[^#]*[#\s]*vim:fileencoding=\s*([^\s]*)\s*')
 
34
# regexp matching both emacs and vim declaration
 
35
ENCODING_RGX = re.compile("[^#]*#*.*coding[:=]\s*([^\s]+)")
37
36
 
38
37
def guess_encoding(string):
39
38
    """try to guess encoding from a python file as string
43
42
    # check for UTF-8 byte-order mark
44
43
    if string.startswith('\xef\xbb\xbf'):
45
44
        return 'UTF-8'
46
 
    
47
 
    first_lines = string.split('\n')[:2]
 
45
    first_lines = string.split('\n', 2)[:2]
48
46
    for line in first_lines:
49
47
        # check for emacs / vim encoding declaration
50
 
        match = EMACS_ENCODING_RGX.match(line) or VIM_ENCODING_RGX.match(line)
 
48
        match = ENCODING_RGX.match(line)
51
49
        if match is not None:
52
50
            return match.group(1)
53
51
 
82
80
                {'type' : 'csv', 'metavar' : '<comma separated values>',
83
81
                 'default' : ('FIXME', 'XXX', 'TODO'),
84
82
                 'help' : 'List of note tags to take in consideration, \
85
 
separated by a comma. Default to FIXME, XXX, TODO'
 
83
separated by a comma.'
86
84
                 }),               
87
85
               )
88
86