Skip to content

Commit fc0def6

Browse files
author
MomIsBestFriend
committed
Fixes for datapythonista's review
1 parent e50cbb9 commit fc0def6

File tree

2 files changed

+106
-40
lines changed

2 files changed

+106
-40
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"
+101-39
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,128 @@
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
3030

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

38+
AVAILABLE_OUTPUT_FORMATS: Tuple[str, str] = ("default", "azure")
39+
40+
OUTPUT_FORMATS: Dict[str, str] = {
41+
"default": "{source_path}:{line_number}:\t between {start} and {end}\n",
42+
"azure": (
43+
"##vso[task.logissue type=error; {source_path}:{line_number}:\t"
44+
"between {start} and {end}\n]"
45+
),
46+
}
3547

36-
def strings_to_concatenate(file_path: str) -> Generator[str, None, None]:
48+
49+
def main(*, source_path: str = ".", output_format: str = "default") -> bool:
50+
"""
51+
Main entry point of the script.
52+
53+
Parameters
54+
----------
55+
source_path : str, default '.'
56+
Source path representing path to a file/directory.
57+
output_format : {'default', 'azure'}, default 'default'
58+
Output format of the script.
59+
60+
Returns
61+
-------
62+
bool
63+
True if found any strings that needs to be concatenated.
64+
65+
Raises
66+
------
67+
ValueError
68+
If the `source_path` is not pointing to existing file/directory.
69+
"""
70+
if not os.path.exists(source_path):
71+
raise ValueError(
72+
"Please enter a valid path, pointing to a valid file/directory."
73+
)
74+
75+
is_failed: bool = False
76+
msg: str = OUTPUT_FORMATS.get(output_format)
77+
78+
if os.path.isfile(source_path):
79+
for values in strings_to_concatenate(source_path):
80+
is_failed = True
81+
print(msg.format(**values))
82+
83+
for subdir, _, files in os.walk(source_path):
84+
for file_name in files:
85+
if any(
86+
file_name.endswith(extension) for extension in FILE_EXTENSIONS_TO_CHECK
87+
):
88+
for values in strings_to_concatenate(os.path.join(subdir, file_name)):
89+
is_failed = True
90+
print(msg.format(**values))
91+
return is_failed
92+
93+
94+
def strings_to_concatenate(source_path: str) -> Generator[Dict[str, str], None, None]:
3795
"""
3896
Yielding the strings that needs to be concatenated in a given file.
3997
4098
Parameters
4199
----------
42-
file_path : str
100+
source_path : str
43101
File path pointing to a single file.
44102
45103
Yields
46104
------
47-
str
48-
Message containing info about the string that needs to be concatenated.
105+
Dict[str, str]
106+
Containing:
107+
source_path
108+
Source file path.
109+
line_number
110+
Line number of unconcatenated string.
111+
start
112+
Starting string of unconcatenated string.
113+
end
114+
Ending string of unconcatenated string.
49115
"""
50-
with open(file_path, "r") as file_name:
116+
with open(source_path, "r") as file_name:
51117
tokens: List = list(tokenize.generate_tokens(file_name.readline))
52118

53119
for current_token, next_token in zip(tokens, tokens[1:]):
54120
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"
121+
yield {
122+
"source_path": source_path,
123+
"line_number": current_token[2][0],
124+
"start": current_token[1],
125+
"end": next_token[1],
126+
}
59127

60128

61129
if __name__ == "__main__":
62-
path: str = sys.argv[1]
63-
64-
if not os.path.exists(path):
65-
raise ValueError("Please enter a valid path, to a file/directory.")
66-
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)
81-
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)
130+
argparser = argparse.ArgumentParser(description="Validate concatenated strings")
131+
argparser.add_argument(
132+
"--format",
133+
"-f",
134+
default="default",
135+
choices=AVAILABLE_OUTPUT_FORMATS,
136+
help=(
137+
f"Format of the output when validating strings "
138+
f"it can be {AVAILABLE_OUTPUT_FORMATS}."
139+
),
140+
)
141+
142+
argparser.add_argument(
143+
"--path", "-p", default=".", help=f"Source path of file/directory to check.",
144+
)
145+
146+
args = argparser.parse_args()
147+
148+
sys.exit(main(source_path=args.path, output_format=args.format))

0 commit comments

Comments
 (0)