From ac568e695d3b7c5d9725473f8f8bdf67771f902a Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 5 Jan 2020 13:40:18 +0200 Subject: [PATCH 1/2] TST: Added test case for spaces at the beggining of concatneted strings --- ci/code_checks.sh | 8 + .../validate_spaces_over_concatned_strings.py | 140 ++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100755 scripts/validate_spaces_over_concatned_strings.py diff --git a/ci/code_checks.sh b/ci/code_checks.sh index a90774d2e8ff1..598d687180f1b 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -108,6 +108,14 @@ if [[ -z "$CHECK" || "$CHECK" == "lint" ]]; then fi RET=$(($RET + $?)) ; echo $MSG "DONE" + MSG='Check for wrong space in a string' ; echo $MSG + if [[ "$GITHUB_ACTIONS" == "true" ]]; then + $BASE_DIR/scripts/validate_spaces_over_concatned_strings.py --format="[error]{source_path}:{line_number}:{msg}" . + else + $BASE_DIR/scripts/validate_spaces_over_concatned_strings.py . + fi + RET=$(($RET + $?)) ; echo $MSG "DONE" + echo "isort --version-number" isort --version-number diff --git a/scripts/validate_spaces_over_concatned_strings.py b/scripts/validate_spaces_over_concatned_strings.py new file mode 100755 index 0000000000000..3f75c0596aa21 --- /dev/null +++ b/scripts/validate_spaces_over_concatned_strings.py @@ -0,0 +1,140 @@ +#!/usr/bin/env python +""" +Test case for leading spaces in concated strings. + +For example: + +# Good +foo = ( + "bar " + "baz" +) + + +# Bad +foo = ( + "bar" + " baz" +) +""" + +import argparse +import os +import sys +import token +import tokenize +from typing import Generator, List, Tuple + +FILE_EXTENSIONS_TO_CHECK = (".py", ".pyx", ".pyx.ini", ".pxd") + +MSG = "String have a space at the beggining instead, at the end of the previos string." + + +def main(source_path: str, output_format: str) -> bool: + """ + Main entry point of the script. + + Parameters + ---------- + source_path : str + Source path representing path to a file/directory. + output_format : str + Output format of the script. + + Returns + ------- + bool + True if found any strings that have a leading space in the wrong line. + + Raises + ------ + ValueError + If the `source_path` is not pointing to existing file/directory. + """ + if not os.path.exists(source_path): + raise ValueError( + "Please enter a valid path, pointing to a valid file/directory." + ) + + is_failed: bool = False + + if os.path.isfile(source_path): + for source_path, line_number in strings_with_wrong_space(source_path): + is_failed = True + print( + output_format.format( + source_path=source_path, line_number=line_number, msg=MSG + ) + ) + + for subdir, _, files in os.walk(source_path): + for file_name in files: + if any( + file_name.endswith(extension) for extension in FILE_EXTENSIONS_TO_CHECK + ): + for source_path, line_number in strings_with_wrong_space( + os.path.join(subdir, file_name) + ): + is_failed = True + print( + output_format.format( + source_path=source_path, line_number=line_number, msg=MSG + ) + ) + return is_failed + + +def strings_with_wrong_space( + source_path: str, +) -> Generator[Tuple[str, int], None, None]: + """ + Yielding the file path and the line number of the string to fix. + + Parameters + ---------- + source_path : str + File path pointing to a single file. + + Yields + ------ + source_path : str + Source file path. + line_number : int + Line number of the wrong placed space. + """ + with open(source_path, "r") as file_name: + tokens: List = list(tokenize.generate_tokens(file_name.readline)) + + for first_token, second_token, third_token in zip(tokens, tokens[1:], tokens[2:]): + if ( + first_token[0] == third_token[0] == token.STRING + and second_token[0] == token.NL + ): + # Means we are in a block of concated string + + # Striping the quotes + first_string = first_token[1][1:-1] + second_string = third_token[1][1:-1] + + if (not first_string.endswith(" ")) and (second_string.startswith(" ")): + yield source_path, third_token[2][0] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Validate spaces over concated strings" + ) + + parser.add_argument( + "path", nargs="?", default=".", help="Source path of file/directory to check." + ) + parser.add_argument( + "--format", + "-f", + default="{source_path}:{line_number}:{msg}", + help="Output format of the error message.", + ) + + args = parser.parse_args() + + sys.exit(main(source_path=args.path, output_format=args.format)) From ee3deea6041ab9af9bf6e7726b0986d688cf5de2 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <50263213+MomIsBestFriend@users.noreply.github.com> Date: Sun, 5 Jan 2020 15:26:09 +0200 Subject: [PATCH 2/2] Update scripts/validate_spaces_over_concatned_strings.py Co-Authored-By: Simon Hawkins --- scripts/validate_spaces_over_concatned_strings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/validate_spaces_over_concatned_strings.py b/scripts/validate_spaces_over_concatned_strings.py index 3f75c0596aa21..5590549c991e6 100755 --- a/scripts/validate_spaces_over_concatned_strings.py +++ b/scripts/validate_spaces_over_concatned_strings.py @@ -27,7 +27,7 @@ FILE_EXTENSIONS_TO_CHECK = (".py", ".pyx", ".pyx.ini", ".pxd") -MSG = "String have a space at the beggining instead, at the end of the previos string." +MSG = "String has a space at the beginning instead of the end of the previous string." def main(source_path: str, output_format: str) -> bool: