-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move quoting analysis to its own file
- Loading branch information
Showing
2 changed files
with
86 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# -*- coding: utf-8 -*- | ||
from plone.recipe.codeanalysis.utils import _find_files | ||
|
||
import sys | ||
|
||
|
||
def code_analysis_prefer_single_quotes(options): | ||
sys.stdout.write('Double quotes ') | ||
sys.stdout.flush() | ||
|
||
files = _find_files(options, '.*\.py') | ||
if not files: | ||
print(' [\033[00;32m OK \033[0m]') | ||
return True | ||
|
||
total_errors = [] | ||
file_paths = files.strip().split('\n') | ||
for file_path in file_paths: | ||
with open(file_path, 'r') as file_handler: | ||
errors = _lines_parser( | ||
file_handler.readlines(), file_path) | ||
|
||
if len(errors) > 0: | ||
total_errors += errors | ||
|
||
if len(total_errors) > 0: | ||
print(' [\033[00;31m FAILURE \033[0m]') | ||
for err in total_errors: | ||
print(err) | ||
return False | ||
else: | ||
print(' [\033[00;32m OK \033[0m]') | ||
return True | ||
|
||
|
||
def _lines_parser(lines, file_path): | ||
errors = [] | ||
multiline = False | ||
linenumber = 0 | ||
|
||
for line in lines: | ||
linenumber += 1 | ||
|
||
# if there is no double quote sign | ||
# there's nothing to do | ||
if line.find('"') == -1: | ||
continue | ||
|
||
# if it's a comment line ignore it | ||
if line.strip().startswith('#'): | ||
continue | ||
|
||
# if it's a multiline string, is | ||
# ok to have doublequotes | ||
if line.find('"""') != -1: | ||
# don't get trapped on multiline | ||
# strings that are on a single line | ||
if line.count('"""') == 2: | ||
continue | ||
elif multiline: | ||
multiline = False | ||
else: | ||
multiline = True | ||
continue | ||
|
||
# until the multiline is finished | ||
# it doesn't matter if single or | ||
# double quotes are used | ||
if multiline: | ||
continue | ||
|
||
# if in the same line are both single | ||
# and double quotes, ignore it | ||
if line.find('"') != -1 and \ | ||
line.find("'") != -1: | ||
continue | ||
|
||
double_quotes_count = line.count('"') | ||
errors.append('{0}:{1}: found {2} double quotes'.format( | ||
file_path, | ||
linenumber, | ||
double_quotes_count, )) | ||
|
||
return errors |