Skip to content

Commit 280d103

Browse files
author
MomIsBestFriend
committed
Fixes for datapythonista's review
1 parent d51ca6a commit 280d103

File tree

2 files changed

+85
-38
lines changed

2 files changed

+85
-38
lines changed

ci/code_checks.sh

+6-1
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,13 @@ if [[ -z "$CHECK" || "$CHECK" == "lint" ]]; then
100100
cpplint --quiet --extensions=c,h --headers=h --recursive --filter=-readability/casting,-runtime/int,-build/include_subdir pandas/_libs/src/*.h pandas/_libs/src/parser pandas/_libs/ujson pandas/_libs/tslibs/src/datetime pandas/_libs/*.cpp
101101
RET=$(($RET + $?)) ; echo $MSG "DONE"
102102

103+
# Get the output format for azure/githubaction from datapythonista
103104
MSG='Check for use of not concatenated strings' ; echo $MSG
104-
$BASE_DIR/scripts/validate_string_concatenation.py pandas
105+
if [[ "$GITHUB_ACTIONS" == "true" ]]; then
106+
$BASE_DIR/scripts/validate_string_concatenation.py --format="[error]{source_path}:{line_number}:\t BETWEEN {start} AND {end}" .
107+
else
108+
$BASE_DIR/scripts/validate_string_concatenation.py .
109+
fi
105110
RET=$(($RET + $?)) ; echo $MSG "DONE"
106111

107112
echo "isort --version-number"
+79-37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python3
1+
#!/usr/bin/env python
22
"""
33
GH #30454
44
@@ -12,7 +12,6 @@
1212
... "baz"
1313
... )
1414
15-
1615
into this:
1716
1817
>>> foo = ("bar " "baz")
@@ -22,65 +21,108 @@
2221
so we are checking it here.
2322
"""
2423

24+
import argparse
2525
import os
2626
import sys
2727
import token
2828
import tokenize
29-
from typing import FrozenSet, Generator, List
29+
from typing import Dict, Generator, List
30+
31+
FILE_EXTENSIONS_TO_CHECK = (".py", ".pyx", ".pyx.ini", ".pxd")
32+
33+
34+
def main(source_path: str = ".", output_format: str) -> bool:
35+
"""
36+
Main entry point of the script.
37+
38+
Parameters
39+
----------
40+
source_path : str, default '.'
41+
Source path representing path to a file/directory.
42+
output_format : str
43+
Output format of the script.
44+
45+
Returns
46+
-------
47+
bool
48+
True if found any strings that needs to be concatenated.
49+
50+
Raises
51+
------
52+
ValueError
53+
If the `source_path` is not pointing to existing file/directory.
54+
"""
55+
if not os.path.exists(source_path):
56+
raise ValueError(
57+
"Please enter a valid path, pointing to a valid file/directory."
58+
)
3059

31-
FILE_EXTENSIONS_TO_CHECK: FrozenSet[str] = frozenset(
32-
(".pxd", ".py", ".pyx", ".pyx.ini")
33-
)
60+
is_failed: bool = False
3461

62+
if os.path.isfile(source_path):
63+
for values in strings_to_concatenate(source_path):
64+
is_failed = True
65+
print(output_format.format(**values))
3566

36-
def strings_to_concatenate(file_path: str) -> Generator[str, None, None]:
67+
for subdir, _, files in os.walk(source_path):
68+
for file_name in files:
69+
if any(
70+
file_name.endswith(extension) for extension in FILE_EXTENSIONS_TO_CHECK
71+
):
72+
for values in strings_to_concatenate(os.path.join(subdir, file_name)):
73+
is_failed = True
74+
print(output_format.format(**values))
75+
return is_failed
76+
77+
78+
def strings_to_concatenate(source_path: str) -> Generator[Dict[str, str], None, None]:
3779
"""
3880
Yielding the strings that needs to be concatenated in a given file.
3981
4082
Parameters
4183
----------
42-
file_path : str
84+
source_path : str
4385
File path pointing to a single file.
4486
4587
Yields
4688
------
47-
str
48-
Message containing info about the string that needs to be concatenated.
89+
dict of {str: str}
90+
Containing:
91+
source_path
92+
Source file path.
93+
line_number
94+
Line number of unconcatenated string.
95+
start
96+
Starting string of unconcatenated string.
97+
end
98+
Ending string of unconcatenated string.
4999
"""
50-
with open(file_path, "r") as file_name:
100+
with open(source_path, "r") as file_name:
51101
tokens: List = list(tokenize.generate_tokens(file_name.readline))
52102

53103
for current_token, next_token in zip(tokens, tokens[1:]):
54104
if current_token[0] == next_token[0] == token.STRING:
55-
line_number = current_token[2][0]
56-
start = current_token[1]
57-
end = next_token[1]
58-
yield f"{file_path}:{line_number}:\t between {start} and {end}\n"
105+
yield {
106+
"source_path": source_path,
107+
"line_number": current_token[2][0],
108+
"start": current_token[1],
109+
"end": next_token[1],
110+
}
59111

60112

61113
if __name__ == "__main__":
62-
path: str = sys.argv[1]
114+
parser = argparse.ArgumentParser(description="Validate concatenated strings")
63115

64-
if not os.path.exists(path):
65-
raise ValueError("Please enter a valid path, to a file/directory.")
116+
parser.add_argument(
117+
"path", nargs="?", default=".", help="Source path of file/directory to check."
118+
)
119+
parser.add_argument(
120+
"--format",
121+
"-f",
122+
default="{source_path}:{line_number}:\t BETWEEN {start} AND {end}",
123+
help="Output format of the unconcatenated strings.",
124+
)
66125

67-
failed: bool = False
68-
69-
if os.path.isfile(path):
70-
for msg in strings_to_concatenate(path):
71-
if msg:
72-
failed = True
73-
print(msg)
74-
75-
for subdir, _, files in os.walk(path):
76-
for file_name in files:
77-
if any(
78-
file_name.endswith(extension) for extension in FILE_EXTENSIONS_TO_CHECK
79-
):
80-
file_extension = os.path.join(subdir, file_name)
126+
args = parser.parse_args()
81127

82-
for msg in strings_to_concatenate(os.path.join(subdir, file_name)):
83-
if msg:
84-
failed = True
85-
print(msg)
86-
sys.exit(failed)
128+
sys.exit(main(source_path=args.path, output_format=args.format))

0 commit comments

Comments
 (0)