Skip to content

Commit e7c1a7f

Browse files
author
MomIsBestFriend
committed
Refactored the code to use a generator
1 parent bfad571 commit e7c1a7f

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

scripts/validate_string_concatenation.py

+27-30
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
>>> foo = ("bar " "baz")
1818
1919
Black is not considering this as an
20-
issue (see https://github.com/psf/black/issues/1051), so we are checking
21-
it here.
20+
issue (see issue https://github.com/psf/black/issues/1051),
21+
so we are checking it here.
2222
"""
2323

2424
import os
@@ -30,38 +30,31 @@
3030
FILE_EXTENSIONS_TO_CHECK = frozenset((".pxd", ".py", ".pyx", ".pyx.ini"))
3131

3232

33-
def is_concatenated(file_path):
33+
def strings_to_concatenate(file_path):
3434
"""
35-
Checking if the file containing strings that needs to be concatenated.
35+
Yielding the strings that needs to be concatenated in a given file.
3636
3737
Parameters
3838
----------
3939
file_path : str
4040
File path pointing to a single file.
4141
42-
Returns
43-
-------
44-
int
45-
Status code representing if the file needs a fix.
46-
0 - All good.
47-
1 - Needs to be fixed.
42+
Yields
43+
------
44+
str
45+
Message containing info about the string that needs to be concatenated.
4846
"""
49-
need_fix = False
5047
with open(file_path, "r") as file_name:
5148
tokens = list(tokenize.generate_tokens(file_name.readline))
52-
for current_token, next_token in zip(tokens, tokens[1:]):
53-
if current_token[0] == next_token[0] == token.STRING:
54-
need_fix = True
55-
print(
56-
"{file_path}:{line_number}:\t{start} and {end}".format(
57-
file_path=file_path,
58-
line_number=current_token[2][0],
59-
start=current_token[1],
60-
end=next_token[1],
61-
)
62-
)
6349

64-
return int(need_fix)
50+
for current_token, next_token in zip(tokens, tokens[1:]):
51+
if current_token[0] == next_token[0] == token.STRING:
52+
yield "{file_path}:{line_number}:\t{start} and {end}\n".format(
53+
file_path=file_path,
54+
line_number=current_token[2][0],
55+
start=current_token[1],
56+
end=next_token[1],
57+
)
6558

6659

6760
if __name__ == "__main__":
@@ -70,19 +63,23 @@ def is_concatenated(file_path):
7063
if not os.path.exists(path):
7164
raise ValueError("Please enter a valid path, to a file/directory.")
7265

66+
passed = True
67+
7368
if os.path.isfile(path):
74-
# Means that the given path is of a single file.
75-
sys.exit(is_concatenated(path))
69+
for msg in strings_to_concatenate(path):
70+
if msg:
71+
passed = False
72+
print(msg)
7673

77-
failures = 0
78-
# Means that the given path is of a directory.
7974
for subdir, _, files in os.walk(path):
8075
for file_name in files:
8176
if any(
8277
file_name.endswith(extension) for extension in FILE_EXTENSIONS_TO_CHECK
8378
):
8479
file_extension = os.path.join(subdir, file_name)
85-
failures += is_concatenated(os.path.join(subdir, file_name))
8680

87-
exit_code = 1 if failures >= 1 else 0
88-
sys.exit(exit_code)
81+
for msg in strings_to_concatenate(os.path.join(subdir, file_name)):
82+
if msg:
83+
passed = False
84+
print(msg)
85+
sys.exit(passed)

0 commit comments

Comments
 (0)