|
1 |
| -#!/usr/bin/env python3 |
| 1 | +#!/usr/bin/env python |
2 | 2 | """
|
3 | 3 | GH #30454
|
4 | 4 |
|
|
12 | 12 | ... "baz"
|
13 | 13 | ... )
|
14 | 14 |
|
15 |
| -
|
16 | 15 | into this:
|
17 | 16 |
|
18 | 17 | >>> foo = ("bar " "baz")
|
|
22 | 21 | so we are checking it here.
|
23 | 22 | """
|
24 | 23 |
|
| 24 | +import argparse |
25 | 25 | import os
|
26 | 26 | import sys
|
27 | 27 | import token
|
28 | 28 | import tokenize
|
29 |
| -from typing import FrozenSet, Generator, List |
| 29 | +from typing import Dict, Generator, List, Tuple |
30 | 30 |
|
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", |
33 | 36 | )
|
34 | 37 |
|
35 | 38 |
|
36 |
| -def strings_to_concatenate(file_path: str) -> Generator[str, None, None]: |
| 39 | +def main(*, source_path: str = ".", output_format: str) -> bool: |
| 40 | + """ |
| 41 | + Main entry point of the script. |
| 42 | +
|
| 43 | + Parameters |
| 44 | + ---------- |
| 45 | + source_path : str, default '.' |
| 46 | + Source path representing path to a file/directory. |
| 47 | + output_format : str |
| 48 | + Output format of the script. |
| 49 | +
|
| 50 | + Returns |
| 51 | + ------- |
| 52 | + bool |
| 53 | + True if found any strings that needs to be concatenated. |
| 54 | +
|
| 55 | + Raises |
| 56 | + ------ |
| 57 | + ValueError |
| 58 | + If the `source_path` is not pointing to existing file/directory. |
| 59 | + """ |
| 60 | + if not os.path.exists(source_path): |
| 61 | + raise ValueError( |
| 62 | + "Please enter a valid path, pointing to a valid file/directory." |
| 63 | + ) |
| 64 | + |
| 65 | + is_failed: bool = False |
| 66 | + |
| 67 | + if os.path.isfile(source_path): |
| 68 | + for values in strings_to_concatenate(source_path): |
| 69 | + is_failed = True |
| 70 | + print(output_format.format(**values)) |
| 71 | + |
| 72 | + for subdir, _, files in os.walk(source_path): |
| 73 | + for file_name in files: |
| 74 | + if any( |
| 75 | + file_name.endswith(extension) for extension in FILE_EXTENSIONS_TO_CHECK |
| 76 | + ): |
| 77 | + for values in strings_to_concatenate(os.path.join(subdir, file_name)): |
| 78 | + is_failed = True |
| 79 | + print(output_format.format(**values)) |
| 80 | + return is_failed |
| 81 | + |
| 82 | + |
| 83 | +def strings_to_concatenate(source_path: str) -> Generator[Dict[str, str], None, None]: |
37 | 84 | """
|
38 | 85 | Yielding the strings that needs to be concatenated in a given file.
|
39 | 86 |
|
40 | 87 | Parameters
|
41 | 88 | ----------
|
42 |
| - file_path : str |
| 89 | + source_path : str |
43 | 90 | File path pointing to a single file.
|
44 | 91 |
|
45 | 92 | Yields
|
46 | 93 | ------
|
47 |
| - str |
48 |
| - Message containing info about the string that needs to be concatenated. |
| 94 | + Dict[str, str] |
| 95 | + Containing: |
| 96 | + source_path |
| 97 | + Source file path. |
| 98 | + line_number |
| 99 | + Line number of unconcatenated string. |
| 100 | + start |
| 101 | + Starting string of unconcatenated string. |
| 102 | + end |
| 103 | + Ending string of unconcatenated string. |
49 | 104 | """
|
50 |
| - with open(file_path, "r") as file_name: |
| 105 | + with open(source_path, "r") as file_name: |
51 | 106 | tokens: List = list(tokenize.generate_tokens(file_name.readline))
|
52 | 107 |
|
53 | 108 | for current_token, next_token in zip(tokens, tokens[1:]):
|
54 | 109 | 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" |
| 110 | + yield { |
| 111 | + "source_path": source_path, |
| 112 | + "line_number": current_token[2][0], |
| 113 | + "start": current_token[1], |
| 114 | + "end": next_token[1], |
| 115 | + } |
59 | 116 |
|
60 | 117 |
|
61 | 118 | if __name__ == "__main__":
|
62 |
| - path: str = sys.argv[1] |
| 119 | + parser = argparse.ArgumentParser(description="Validate concatenated strings") |
63 | 120 |
|
64 |
| - if not os.path.exists(path): |
65 |
| - raise ValueError("Please enter a valid path, to a file/directory.") |
| 121 | + parser.add_argument( |
| 122 | + "path", nargs="?", default=".", help="Source path of file/directory to check." |
| 123 | + ) |
| 124 | + parser.add_argument( |
| 125 | + "--format", |
| 126 | + "-f", |
| 127 | + default="{source_path}:{line_number}:\t BETWEEN {start} AND {end}\n", |
| 128 | + help="Output format of the unconcatenated strings.", |
| 129 | + ) |
66 | 130 |
|
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) |
| 131 | + args = parser.parse_args() |
81 | 132 |
|
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) |
| 133 | + sys.exit(main(source_path=args.path, output_format=args.format)) |
0 commit comments