ENH: Add Python2.7 support

This commit is contained in:
Eric Larson
2016-04-27 10:23:09 -04:00
parent b400e4250d
commit dc39dfcf53
2 changed files with 49 additions and 55 deletions

View File

@ -20,6 +20,7 @@ Copyright (C) 2011 ProFUSION embedded systems
from __future__ import print_function
import codecs
import sys
import re
from optparse import OptionParser
@ -50,7 +51,7 @@ default_dictionary = os.path.join(os.path.dirname(os.path.realpath(__file__)),
# file1 .. fileN Files to check spelling
class QuietLevels:
class QuietLevels(object):
NONE = 0
ENCODING = 1
BINARY_FILE = 2
@ -59,7 +60,7 @@ class QuietLevels:
FIXES = 16
class GlobMatch:
class GlobMatch(object):
def __init__(self, pattern):
if pattern:
self.pattern_list = pattern.split(',')
@ -77,14 +78,14 @@ class GlobMatch:
return False
class Misspelling:
class Misspelling(object):
def __init__(self, data, fix, reason):
self.data = data
self.fix = fix
self.reason = reason
class TermColors:
class TermColors(object):
def __init__(self):
self.FILE = '\033[33m'
self.WWORD = '\033[31m'
@ -98,7 +99,7 @@ class TermColors:
self.DISABLE = ''
class Summary:
class Summary(object):
def __init__(self):
self.summary = {}
@ -118,7 +119,7 @@ class Summary:
width=15 - len(key)) for key in keys])
class FileOpener:
class FileOpener(object):
def __init__(self, use_chardet):
self.use_chardet = use_chardet
if use_chardet:
@ -142,7 +143,7 @@ class FileOpener:
def open_with_chardet(self, filename):
self.encdetector.reset()
with open(filename, 'rb') as f:
with codecs.open(filename, 'rb') as f:
for line in f:
self.encdetector.feed(line)
if self.encdetector.done:
@ -151,8 +152,7 @@ class FileOpener:
encoding = self.encdetector.result['encoding']
try:
f = open(filename, 'r', encoding=encoding, newline='')
lines = f.readlines()
f = codecs.open(filename, 'r', encoding=encoding)
except UnicodeDecodeError:
print('ERROR: Could not detect encoding: %s' % filename,
file=sys.stderr)
@ -161,7 +161,8 @@ class FileOpener:
print('ERROR: %s -- Don\'t know how to handle encoding %s'
% (filename, encoding), file=sys.stderr)
raise
finally:
else:
lines = f.readlines()
f.close()
return lines, encoding
@ -172,9 +173,7 @@ class FileOpener:
while True:
try:
f = open(filename, 'r', encoding=encodings[curr], newline='')
lines = f.readlines()
break
f = codecs.open(filename, 'r', encoding=encodings[curr])
except UnicodeDecodeError:
if not quiet_level & QuietLevels.ENCODING:
print('WARNING: Decoding file %s' % filename,
@ -188,10 +187,10 @@ class FileOpener:
pass
curr += 1
finally:
else:
lines = f.readlines()
f.close()
break
if not lines:
raise Exception('Unknown encoding')
@ -265,25 +264,20 @@ def parse_options(args):
(o, args) = parser.parse_args(list(args))
if not os.path.exists(o.dictionary):
print('ERROR: cannot find dictionary file!', file=sys.stderr)
parser.print_help()
sys.exit(1)
if not args:
args.append('.')
return o, args
return o, args, parser
def build_exclude_hashes(filename):
with open(filename, 'r') as f:
with codecs.open(filename, 'r') as f:
for line in f:
exclude_lines.add(line)
def build_dict(filename):
with open(filename, 'r', 1, 'utf-8') as f:
with codecs.open(filename, mode='r', buffering=1, encoding='utf-8') as f:
for line in f:
[key, data] = line.split('->')
data = data.strip()
@ -317,10 +311,9 @@ def is_hidden(filename):
def is_text_file(filename):
with open(filename, mode='rb') as f:
s = f.read(1024)
if 0 in s:
return False
return True
if b'\x00' in s:
return False
return True
def fix_case(word, fixword):
@ -399,25 +392,22 @@ def parse_file(filename, colors, summary):
lines = f.readlines()
else:
# ignore binary files
try:
text = is_text_file(filename)
except FileNotFoundError:
if not os.path.isfile(filename):
return 0
text = is_text_file(filename)
if not text:
if not quiet_level & QuietLevels.BINARY_FILE:
print("WARNING: Binary file: %s " % filename, file=sys.stderr)
return 0
try:
lines, encoding = file_opener.open(filename)
except:
except Exception:
return 0
i = 1
bad_count = 0
rx = re.compile(r"[\w\-']+")
for line in lines:
for i, line in enumerate(lines):
if line in exclude_lines:
i += 1
continue
fixed_words = set()
@ -438,7 +428,7 @@ def parse_file(filename, colors, summary):
if summary and fix:
summary.update(lword)
if word in fixed_words:
if word in fixed_words: # can skip because of re.sub below
continue
if options.write_changes and fix:
@ -487,7 +477,6 @@ def parse_file(filename, colors, summary):
% {'LINE': cline, 'STRLINE': line.strip(),
'WRONGWORD': cwrongword,
'RIGHTWORD': crightword, 'REASON': creason})
i += 1
if changed:
if filename == '-':
@ -499,18 +488,25 @@ def parse_file(filename, colors, summary):
print("%sFIXED:%s %s"
% (colors.FWORD, colors.DISABLE, filename),
file=sys.stderr)
f = open(filename, 'w', encoding=encoding)
f.writelines(lines)
f.close()
with codecs.open(filename, 'w', encoding=encoding) as f:
f.writelines(lines)
return bad_count
def main(*args):
"""Contains flow control"""
global options
global quiet_level
global file_opener
(options, args) = parse_options(args)
options, args, parser = parse_options(args)
if not os.path.exists(options.dictionary):
print(default_dictionary)
print('ERROR: cannot find dictionary file:\n%s' % options.dictionary,
file=sys.stderr)
parser.print_help()
sys.exit(1)
build_dict(options.dictionary)
colors = TermColors()
@ -540,26 +536,24 @@ def main(*args):
if os.path.isdir(filename):
for root, dirs, files in os.walk(filename):
i = 0
for d in dirs:
if is_hidden(d):
del dirs[i]
else:
i += 1
for file in files:
fname = os.path.join(root, file)
# i = 0
# for d in dirs:
# if is_hidden(d):
# del dirs[i]
# else:
# i += 1
for file_ in files:
fname = os.path.join(root, file_)
if not os.path.isfile(fname):
continue
if not os.path.getsize(fname):
continue
if glob_match.match(file):
if glob_match.match(file_):
continue
bad_count += parse_file(fname, colors, summary)
continue
bad_count += parse_file(filename, colors, summary)
else:
bad_count += parse_file(filename, colors, summary)
if summary:
print("\n-------8<-------\nSUMMARY:")