Skip to content

Commit ecd2e93

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

File tree

2 files changed

+89
-37
lines changed

2 files changed

+89
-37
lines changed

ci/code_checks.sh

+5-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ if [[ -z "$CHECK" || "$CHECK" == "lint" ]]; then
101101
RET=$(($RET + $?)) ; echo $MSG "DONE"
102102

103103
MSG='Check for use of not concatenated strings' ; echo $MSG
104-
$BASE_DIR/scripts/validate_string_concatenation.py pandas
104+
if [[ "$GITHUB_ACTIONS" == "true" ]]; then
105+
$BASE_DIR/scripts/validate_string_concatenation.py --format "azure" --path "."
106+
else
107+
$BASE_DIR/scripts/validate_string_concatenation.py --path "."
108+
fi
105109
RET=$(($RET + $?)) ; echo $MSG "DONE"
106110

107111
echo "isort --version-number"
+84-36
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,114 @@
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, Tuple
30+
3031

31-
FILE_EXTENSIONS_TO_CHECK: FrozenSet[str] = frozenset(
32-
(".pxd", ".py", ".pyx", ".pyx.ini")
32+
FILE_EXTENSIONS_TO_CHECK: Tuple[str, str, str, str] = (
33+
".py",
34+
".pyx",
35+
".pyx.ini",
36+
".pxd",
3337
)
3438

3539

36-
def strings_to_concatenate(file_path: str) -> Generator[str, None, None]:
40+
def main(*, source_path: str = ".", output_format: str) -> bool:
41+
"""
42+
Main entry point of the script.
43+
44+
Parameters
45+
----------
46+
source_path : str, default '.'
47+
Source path representing path to a file/directory.
48+
output_format : str
49+
Output format of the script.
50+
51+
Returns
52+
-------
53+
bool
54+
True if found any strings that needs to be concatenated.
55+
56+
Raises
57+
------
58+
ValueError
59+
If the `source_path` is not pointing to existing file/directory.
60+
"""
61+
if not os.path.exists(source_path):
62+
raise ValueError(
63+
"Please enter a valid path, pointing to a valid file/directory."
64+
)
65+
66+
is_failed: bool = False
67+
68+
if os.path.isfile(source_path):
69+
for values in strings_to_concatenate(source_path):
70+
is_failed = True
71+
print(output_format.format(**values))
72+
73+
for subdir, _, files in os.walk(source_path):
74+
for file_name in files:
75+
if any(
76+
file_name.endswith(extension) for extension in FILE_EXTENSIONS_TO_CHECK
77+
):
78+
for values in strings_to_concatenate(os.path.join(subdir, file_name)):
79+
is_failed = True
80+
print(output_format.format(**values))
81+
return is_failed
82+
83+
84+
def strings_to_concatenate(source_path: str) -> Generator[Dict[str, str], None, None]:
3785
"""
3886
Yielding the strings that needs to be concatenated in a given file.
3987
4088
Parameters
4189
----------
42-
file_path : str
90+
source_path : str
4391
File path pointing to a single file.
4492
4593
Yields
4694
------
47-
str
48-
Message containing info about the string that needs to be concatenated.
95+
Dict[str, str]
96+
Containing:
97+
source_path
98+
Source file path.
99+
line_number
100+
Line number of unconcatenated string.
101+
start
102+
Starting string of unconcatenated string.
103+
end
104+
Ending string of unconcatenated string.
49105
"""
50-
with open(file_path, "r") as file_name:
106+
with open(source_path, "r") as file_name:
51107
tokens: List = list(tokenize.generate_tokens(file_name.readline))
52108

53109
for current_token, next_token in zip(tokens, tokens[1:]):
54110
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"
111+
yield {
112+
"source_path": source_path,
113+
"line_number": current_token[2][0],
114+
"start": current_token[1],
115+
"end": next_token[1],
116+
}
59117

60118

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

64-
if not os.path.exists(path):
65-
raise ValueError("Please enter a valid path, to a file/directory.")
122+
parser.add_argument(
123+
"path", nargs="?", default=".", help="Source path of file/directory to check."
124+
)
125+
parser.add_argument(
126+
"--format",
127+
"-f",
128+
default="{source_path}:{line_number}:\t BETWEEN {start} AND {end}\n",
129+
help="Output format of the unconcatenated strings.",
130+
)
66131

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)
132+
args = parser.parse_args()
81133

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)
134+
sys.exit(main(source_path=args.path, output_format=args.format))

0 commit comments

Comments
 (0)