|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Check where there is a string that needs to be concatenated. |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import token |
| 9 | +import tokenize |
| 10 | + |
| 11 | +FILE_EXTENTIONS_TO_CHECK = [".py", ".pyx"] |
| 12 | + |
| 13 | + |
| 14 | +def main(): |
| 15 | + path = sys.argv[1] |
| 16 | + |
| 17 | + if not os.path.exists(path): |
| 18 | + raise ValueError("Please enter a valid path, to a file/directory.") |
| 19 | + |
| 20 | + if os.path.isfile(path): |
| 21 | + # Means that the given path is of a single file. |
| 22 | + sys.exit(is_concatenated(path)) |
| 23 | + |
| 24 | + status_codes = set() |
| 25 | + # Means that the given path is of a directory. |
| 26 | + for subdir, _, files in os.walk(path): |
| 27 | + for file_name in files: |
| 28 | + ext = os.path.splitext(os.path.join(subdir, file_name))[1] |
| 29 | + if ext in FILE_EXTENTIONS_TO_CHECK: |
| 30 | + status_codes.add(is_concatenated(os.path.join(subdir, file_name))) |
| 31 | + |
| 32 | + if 1 in status_codes: |
| 33 | + sys.exit(1) |
| 34 | + |
| 35 | + sys.exit(0) |
| 36 | + |
| 37 | + |
| 38 | +def is_concatenated(file_path): |
| 39 | + """ |
| 40 | + Checking if the file containing strings that needs to be concatenated. |
| 41 | +
|
| 42 | + Parameters |
| 43 | + ---------- |
| 44 | + file_path : str |
| 45 | + File path pointing to a single file. |
| 46 | +
|
| 47 | + Returns |
| 48 | + ------- |
| 49 | + int |
| 50 | + Status code representing if the file needs a fix. |
| 51 | + 0 - All good. |
| 52 | + 1 - Needs to be fixed. |
| 53 | + """ |
| 54 | + with open(file_path, "r") as file_name: |
| 55 | + toks = list(tokenize.generate_tokens(file_name.readline)) |
| 56 | + for i in range(len(toks) - 1): |
| 57 | + tok = toks[i] |
| 58 | + tok2 = toks[i + 1] |
| 59 | + if tok[0] == token.STRING and tok[0] == tok2[0]: |
| 60 | + print( |
| 61 | + "{file_path}:{line_number}:\t{start} and {end}".format( |
| 62 | + file_path=file_path, |
| 63 | + line_number=tok[2][0], |
| 64 | + start=tok[1], |
| 65 | + end=tok2[1], |
| 66 | + ) |
| 67 | + ) |
| 68 | + return 1 |
| 69 | + return 0 |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + main() |
0 commit comments