"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "codespell_lib/_codespell.py" between
codespell-1.16.0.tar.gz and codespell-1.17.1.tar.gz

About: codespell-.* codespell checks source code and other text files for common misspellings.

_codespell.py  (codespell-1.16.0):_codespell.py  (codespell-1.17.1)
skipping to change at line 34 skipping to change at line 34
import fnmatch import fnmatch
import os import os
import re import re
import sys import sys
word_regex_def = u"[\\w\\-'’`]+" word_regex_def = u"[\\w\\-'’`]+"
encodings = ('utf-8', 'iso-8859-1') encodings = ('utf-8', 'iso-8859-1')
USAGE = """ USAGE = """
\t%prog [OPTIONS] [file1 file2 ... fileN] \t%prog [OPTIONS] [file1 file2 ... fileN]
""" """
VERSION = '1.16.0' VERSION = '1.17.1'
# Users might want to link this file into /usr/local/bin, so we resolve the # Users might want to link this file into /usr/local/bin, so we resolve the
# symbolic link path to the real path if necessary. # symbolic link path to the real path if necessary.
default_dictionary = os.path.join(os.path.dirname(os.path.realpath(__file__)), _data_root = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
'data', 'dictionary.txt') _builtin_dictionaries = (
# name, desc, name, err in aspell, correction in aspell
# The aspell tests here aren't the ideal state, but the None's are
# realistic for obscure words
('clear', 'for unambiguous errors', '', False, None),
('rare', 'for rare but valid words', '_rare', None, None),
('informal', 'for informal words', '_informal', True, True),
('code', 'for words common to code and/or mathematics', '_code', None, None)
, # noqa: E501
('names', 'for valid proper names that might be typos', '_names', None, None
), # noqa: E501
('en-GB_to_en-US', 'for corrections from en-GB to en-US', '_en-GB_to_en-US',
True, True), # noqa: E501
)
_builtin_default = 'clear,rare'
# OPTIONS: # OPTIONS:
# #
# ARGUMENTS: # ARGUMENTS:
# dict_filename The file containing the dictionary of misspellings. # dict_filename The file containing the dictionary of misspellings.
# If set to '-', it will be read from stdin # If set to '-', it will be read from stdin
# file1 .. fileN Files to check spelling # file1 .. fileN Files to check spelling
class QuietLevels(object): class QuietLevels(object):
NONE = 0 NONE = 0
ENCODING = 1 ENCODING = 1
BINARY_FILE = 2 BINARY_FILE = 2
DISABLED_FIXES = 4 DISABLED_FIXES = 4
NON_AUTOMATIC_FIXES = 8 NON_AUTOMATIC_FIXES = 8
FIXES = 16 FIXES = 16
class GlobMatch(object): class GlobMatch(object):
def __init__(self, pattern): def __init__(self, pattern):
if pattern: if pattern:
self.pattern_list = pattern.split(',') # Pattern might be a list of comma-delimited strings
self.pattern_list = ','.join(pattern).split(',')
else: else:
self.pattern_list = None self.pattern_list = None
def match(self, filename): def match(self, filename):
if self.pattern_list is None: if self.pattern_list is None:
return False return False
for p in self.pattern_list: for p in self.pattern_list:
if fnmatch.fnmatch(filename, p): if fnmatch.fnmatch(filename, p):
return True return True
skipping to change at line 211 skipping to change at line 223
parser.add_argument('-c', '--enable-colors', parser.add_argument('-c', '--enable-colors',
action='store_true', dest='colors', action='store_true', dest='colors',
help='enable colors, even when not printing to ' help='enable colors, even when not printing to '
'terminal') 'terminal')
parser.add_argument('-w', '--write-changes', parser.add_argument('-w', '--write-changes',
action='store_true', default=False, action='store_true', default=False,
help='write changes in place if possible') help='write changes in place if possible')
parser.add_argument('-D', '--dictionary', parser.add_argument('-D', '--dictionary',
action='append', metavar='FILE', action='append',
help='Custom dictionary file that contains spelling ' help='Custom dictionary file that contains spelling '
'corrections. If this flag is not specified or ' 'corrections. If this flag is not specified or '
'equals "-" then the default dictionary is used. ' 'equals "-" then the default dictionary is used. '
'This option can be specified multiple times.') 'This option can be specified multiple times.')
builtin_opts = ', '.join(
'%r %s' % (d[0], d[1]) for d in _builtin_dictionaries)
parser.add_argument('--builtin',
dest='builtin', default=_builtin_default,
metavar='BUILTIN-LIST',
help='Comma-separated list of builtin dictionaries '
'to include (when "-D -" or no "-D" is passed). '
'Current options are:\n%s. The default is '
'"--builtin %s".'
% (builtin_opts, _builtin_default))
parser.add_argument('-I', '--ignore-words', parser.add_argument('-I', '--ignore-words',
action='append', metavar='FILE', action='append', metavar='FILE',
help='File that contains words which will be ignored ' help='File that contains words which will be ignored '
'by codespell. File must contain 1 word per line.' 'by codespell. File must contain 1 word per line.'
' Words are case sensitive based on how they are ' ' Words are case sensitive based on how they are '
'written in the dictionary file') 'written in the dictionary file')
parser.add_argument('-L', '--ignore-words-list', parser.add_argument('-L', '--ignore-words-list',
action='append', metavar='WORDS', action='append', metavar='WORDS',
help='Comma separated list of words to be ignored ' help='Comma separated list of words to be ignored '
'by codespell. Words are case sensitive based on ' 'by codespell. Words are case sensitive based on '
skipping to change at line 239 skipping to change at line 261
help='Regular expression which is used to find words. ' help='Regular expression which is used to find words. '
'By default any alphanumeric character, the ' 'By default any alphanumeric character, the '
'underscore, the hyphen, and the apostrophe is ' 'underscore, the hyphen, and the apostrophe is '
'used to build words. This option cannot be ' 'used to build words. This option cannot be '
'specified together with --write-changes.') 'specified together with --write-changes.')
parser.add_argument('-s', '--summary', parser.add_argument('-s', '--summary',
action='store_true', default=False, action='store_true', default=False,
help='print summary of fixes') help='print summary of fixes')
parser.add_argument('-S', '--skip', parser.add_argument('-S', '--skip',
action='append',
help='Comma-separated list of files to skip. It ' help='Comma-separated list of files to skip. It '
'accepts globs as well. E.g.: if you want ' 'accepts globs as well. E.g.: if you want '
'codespell to skip .eps and .txt files, ' 'codespell to skip .eps and .txt files, '
'you\'d give "*.eps,*.txt" to this option.') 'you\'d give "*.eps,*.txt" to this option.')
parser.add_argument('-x', '--exclude-file', type=str, metavar='FILE', parser.add_argument('-x', '--exclude-file', type=str, metavar='FILE',
help='FILE with lines that should not be changed') help='FILE with lines that should not be changed')
parser.add_argument('-i', '--interactive', parser.add_argument('-i', '--interactive',
action='store', type=int, default=0, action='store', type=int, default=0,
skipping to change at line 303 skipping to change at line 326
options.files.append('.') options.files.append('.')
return options, parser return options, parser
def build_exclude_hashes(filename, exclude_lines): def build_exclude_hashes(filename, exclude_lines):
with codecs.open(filename, 'r') as f: with codecs.open(filename, 'r') as f:
for line in f: for line in f:
exclude_lines.add(line) exclude_lines.add(line)
def build_ignore_words(filename, ignore_words): def build_ignore_words(filename, ignore_words):
with codecs.open(filename, mode='r', buffering=1, encoding='utf-8') as f: with codecs.open(filename, mode='r', encoding='utf-8') as f:
for line in f: for line in f:
ignore_words.add(line.strip()) ignore_words.add(line.strip())
def build_dict(filename, misspellings, ignore_words): def build_dict(filename, misspellings, ignore_words):
with codecs.open(filename, mode='r', buffering=1, encoding='utf-8') as f: with codecs.open(filename, mode='r', encoding='utf-8') as f:
for line in f: for line in f:
[key, data] = line.split('->') [key, data] = line.split('->')
# TODO for now, convert both to lower. Someday we can maybe add # TODO for now, convert both to lower. Someday we can maybe add
# support for fixing caps. # support for fixing caps.
key = key.lower() key = key.lower()
data = data.lower() data = data.lower()
if key in ignore_words: if key in ignore_words:
continue continue
data = data.strip() data = data.strip()
fix = data.rfind(',') fix = data.rfind(',')
skipping to change at line 337 skipping to change at line 360
else: else:
reason = data[fix + 1:].strip() reason = data[fix + 1:].strip()
data = data[:fix] data = data[:fix]
fix = False fix = False
misspellings[key] = Misspelling(data, fix, reason) misspellings[key] = Misspelling(data, fix, reason)
def is_hidden(filename, check_hidden): def is_hidden(filename, check_hidden):
bfilename = os.path.basename(filename) bfilename = os.path.basename(filename)
if bfilename != '' and bfilename != '.' and bfilename != '..' \ return bfilename not in ('', '.', '..') and \
and (not check_hidden and bfilename[0] == '.'): (not check_hidden and bfilename[0] == '.')
return True
return False
def is_text_file(filename): def is_text_file(filename):
with open(filename, mode='rb') as f: with open(filename, mode='rb') as f:
s = f.read(1024) s = f.read(1024)
if b'\x00' in s: if b'\x00' in s:
return False return False
return True return True
def fix_case(word, fixword): def fix_case(word, fixword):
if word == word.capitalize(): if word == word.capitalize():
skipping to change at line 367 skipping to change at line 387
return fixword return fixword
def ask_for_word_fix(line, wrongword, misspelling, interactivity): def ask_for_word_fix(line, wrongword, misspelling, interactivity):
if interactivity <= 0: if interactivity <= 0:
return misspelling.fix, fix_case(wrongword, misspelling.data) return misspelling.fix, fix_case(wrongword, misspelling.data)
if misspelling.fix and interactivity & 1: if misspelling.fix and interactivity & 1:
r = '' r = ''
fixword = fix_case(wrongword, misspelling.data) fixword = fix_case(wrongword, misspelling.data)
while not r: while not r:
print("%s\t%s ==> %s (Y/n) " % (line, wrongword, fixword), end='') print("%s\t%s ==> %s (Y/n) " % (line, wrongword, fixword), end='')
r = sys.stdin.readline().strip().upper() r = sys.stdin.readline().strip().upper()
if not r: if not r:
r = 'Y' r = 'Y'
if r != 'Y' and r != 'N': if r != 'Y' and r != 'N':
print("Say 'y' or 'n'") print("Say 'y' or 'n'")
r = '' r = ''
if r == 'N': if r == 'N':
misspelling.fix = False misspelling.fix = False
misspelling.fixword = '' misspelling.fixword = ''
skipping to change at line 457 skipping to change at line 477
creason = " | %s%s%s" % (colors.FILE, creason = " | %s%s%s" % (colors.FILE,
misspellings[lword].reason, misspellings[lword].reason,
colors.DISABLE) colors.DISABLE)
else: else:
if options.quiet_level & QuietLevels.NON_AUTOMATIC_FIXES: if options.quiet_level & QuietLevels.NON_AUTOMATIC_FIXES:
continue continue
creason = '' creason = ''
bad_count += 1 bad_count += 1
print("%(FILENAME)s: %(WRONGWORD)s " print("%(FILENAME)s: %(WRONGWORD)s"
" ==> %(RIGHTWORD)s%(REASON)s" " ==> %(RIGHTWORD)s%(REASON)s"
% {'FILENAME': cfilename, % {'FILENAME': cfilename,
'WRONGWORD': cwrongword, 'WRONGWORD': cwrongword,
'RIGHTWORD': crightword, 'REASON': creason}) 'RIGHTWORD': crightword, 'REASON': creason})
text = is_text_file(filename) text = is_text_file(filename)
if not text: if not text:
if not options.quiet_level & QuietLevels.BINARY_FILE: if not options.quiet_level & QuietLevels.BINARY_FILE:
print("WARNING: Binary file: %s " % filename, file=sys.stderr) print("WARNING: Binary file: %s " % filename, file=sys.stderr)
return 0 return 0
skipping to change at line 539 skipping to change at line 559
creason = '' creason = ''
# If we get to this point (uncorrected error) we should change # If we get to this point (uncorrected error) we should change
# our bad_count and thus return value # our bad_count and thus return value
bad_count += 1 bad_count += 1
if (not context_shown) and (context is not None): if (not context_shown) and (context is not None):
print_context(lines, i, context) print_context(lines, i, context)
if filename != '-': if filename != '-':
print("%(FILENAME)s:%(LINE)s: %(WRONGWORD)s " print("%(FILENAME)s:%(LINE)s: %(WRONGWORD)s "
" ==> %(RIGHTWORD)s%(REASON)s" "==> %(RIGHTWORD)s%(REASON)s"
% {'FILENAME': cfilename, 'LINE': cline, % {'FILENAME': cfilename, 'LINE': cline,
'WRONGWORD': cwrongword, 'WRONGWORD': cwrongword,
'RIGHTWORD': crightword, 'REASON': creason}) 'RIGHTWORD': crightword, 'REASON': creason})
else: else:
print('%(LINE)s: %(STRLINE)s\n\t%(WRONGWORD)s ' print("%(LINE)s: %(STRLINE)s\n\t%(WRONGWORD)s "
'==> %(RIGHTWORD)s%(REASON)s' "==> %(RIGHTWORD)s%(REASON)s"
% {'LINE': cline, 'STRLINE': line.strip(), % {'LINE': cline, 'STRLINE': line.strip(),
'WRONGWORD': cwrongword, 'WRONGWORD': cwrongword,
'RIGHTWORD': crightword, 'REASON': creason}) 'RIGHTWORD': crightword, 'REASON': creason})
if changed: if changed:
if filename == '-': if filename == '-':
print("---") print("---")
for line in lines: for line in lines:
print(line, end='') print(line, end='')
else: else:
skipping to change at line 589 skipping to change at line 609
word_regex = re.compile(word_regex) word_regex = re.compile(word_regex)
except re.error as err: except re.error as err:
print('ERROR: invalid regular expression "%s" (%s)' % print('ERROR: invalid regular expression "%s" (%s)' %
(word_regex, err), file=sys.stderr) (word_regex, err), file=sys.stderr)
parser.print_help() parser.print_help()
return 1 return 1
ignore_words_files = options.ignore_words or [] ignore_words_files = options.ignore_words or []
ignore_words = set() ignore_words = set()
for ignore_words_file in ignore_words_files: for ignore_words_file in ignore_words_files:
if not os.path.exists(ignore_words_file): if not os.path.isfile(ignore_words_file):
print('ERROR: cannot find ignore-words file: %s' % print('ERROR: cannot find ignore-words file: %s' %
ignore_words_file, file=sys.stderr) ignore_words_file, file=sys.stderr)
parser.print_help() parser.print_help()
return 1 return 1
build_ignore_words(ignore_words_file, ignore_words) build_ignore_words(ignore_words_file, ignore_words)
ignore_words_list = options.ignore_words_list or [] ignore_words_list = options.ignore_words_list or []
for comma_separated_words in ignore_words_list: for comma_separated_words in ignore_words_list:
for word in comma_separated_words.split(','): for word in comma_separated_words.split(','):
ignore_words.add(word.strip()) ignore_words.add(word.strip())
dictionaries = options.dictionary or [default_dictionary] if options.dictionary:
misspellings = dict() dictionaries = options.dictionary
else:
dictionaries = ['-']
use_dictionaries = list()
for dictionary in dictionaries: for dictionary in dictionaries:
if dictionary == "-": if dictionary == "-":
dictionary = default_dictionary # figure out which builtin dictionaries to use
if not os.path.exists(dictionary): use = sorted(set(options.builtin.split(',')))
print('ERROR: cannot find dictionary file: %s' % dictionary, for u in use:
file=sys.stderr) for builtin in _builtin_dictionaries:
parser.print_help() if builtin[0] == u:
return 1 use_dictionaries.append(
os.path.join(_data_root, 'dictionary%s.txt'
% (builtin[2],)))
break
else:
print('ERROR: Unknown builtin dictionary: %s' % (u,),
file=sys.stderr)
parser.print_help()
return 1
else:
if not os.path.isfile(dictionary):
print('ERROR: cannot find dictionary file: %s' % dictionary,
file=sys.stderr)
parser.print_help()
return 1
use_dictionaries.append(dictionary)
misspellings = dict()
for dictionary in use_dictionaries:
build_dict(dictionary, misspellings, ignore_words) build_dict(dictionary, misspellings, ignore_words)
colors = TermColors() colors = TermColors()
if not options.colors or sys.platform == 'win32': if not options.colors or sys.platform == 'win32':
colors.disable() colors.disable()
if options.summary: if options.summary:
summary = Summary() summary = Summary()
else: else:
summary = None summary = None
 End of changes. 16 change blocks. 
26 lines changed or deleted 69 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)