Skip to content

Commit

Permalink
Move quoting analysis to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
gforcada committed Oct 7, 2013
1 parent fe51291 commit 83a24ea
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 80 deletions.
82 changes: 2 additions & 80 deletions plone/recipe/codeanalysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from plone.recipe.codeanalysis.imports import code_analysis_imports
from plone.recipe.codeanalysis.jshint import code_analysis_jshint
from plone.recipe.codeanalysis.pep3101 import code_analysis_pep3101
from plone.recipe.codeanalysis.quoting import \
code_analysis_prefer_single_quotes
from plone.recipe.codeanalysis.utils import _find_files
from plone.recipe.codeanalysis.zptlint import code_analysis_zptlint

Expand Down Expand Up @@ -430,83 +432,3 @@ def _code_analysis_clean_lines_parser(lines, file_path):
file_path,
linenumber, ))
return errors


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 = _code_analysis_prefer_single_quotes_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 _code_analysis_prefer_single_quotes_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
84 changes: 84 additions & 0 deletions plone/recipe/codeanalysis/quoting.py
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

0 comments on commit 83a24ea

Please sign in to comment.