12
12
... "baz"
13
13
... )
14
14
15
+
15
16
into this:
16
17
17
18
>>> foo = ("bar " "baz")
18
19
19
20
Black is not considering this as an
20
- issue (see https://github.com/psf/black/issues/1051), so we are checking
21
- it here.
21
+ issue (see issue https://github.com/psf/black/issues/1051),
22
+ so we are checking it here.
22
23
"""
23
24
24
25
import os
30
31
FILE_EXTENSIONS_TO_CHECK = frozenset ((".pxd" , ".py" , ".pyx" , ".pyx.ini" ))
31
32
32
33
33
- def is_concatenated (file_path ):
34
+ def strings_to_concatenate (file_path ):
34
35
"""
35
- Checking if the file containing strings that needs to be concatenated.
36
+ Yielding the strings that needs to be concatenated in a given file .
36
37
37
38
Parameters
38
39
----------
39
40
file_path : str
40
41
File path pointing to a single file.
41
42
42
- Returns
43
- -------
44
- int
45
- Status code representing if the file needs a fix.
46
- 0 - All good.
47
- 1 - Needs to be fixed.
43
+ Yields
44
+ ------
45
+ str
46
+ Message containing info about the string that needs to be concatenated.
48
47
"""
49
- need_fix = False
50
48
with open (file_path , "r" ) as file_name :
51
49
tokens = list (tokenize .generate_tokens (file_name .readline ))
52
- for current_token , next_token in zip (tokens , tokens [1 :]):
53
- if current_token [0 ] == next_token [0 ] == token .STRING :
54
- need_fix = True
55
- print (
56
- "{file_path}:{line_number}:\t {start} and {end}" .format (
57
- file_path = file_path ,
58
- line_number = current_token [2 ][0 ],
59
- start = current_token [1 ],
60
- end = next_token [1 ],
61
- )
62
- )
63
50
64
- return int (need_fix )
51
+ for current_token , next_token in zip (tokens , tokens [1 :]):
52
+ if current_token [0 ] == next_token [0 ] == token .STRING :
53
+ yield "{file_path}:{line_number}:\t {start} and {end}\n " .format (
54
+ file_path = file_path ,
55
+ line_number = current_token [2 ][0 ],
56
+ start = current_token [1 ],
57
+ end = next_token [1 ],
58
+ )
65
59
66
60
67
61
if __name__ == "__main__" :
@@ -70,19 +64,23 @@ def is_concatenated(file_path):
70
64
if not os .path .exists (path ):
71
65
raise ValueError ("Please enter a valid path, to a file/directory." )
72
66
67
+ failed = False
68
+
73
69
if os .path .isfile (path ):
74
- # Means that the given path is of a single file.
75
- sys .exit (is_concatenated (path ))
70
+ for msg in strings_to_concatenate (path ):
71
+ if msg :
72
+ failed = True
73
+ print (msg )
76
74
77
- failures = 0
78
- # Means that the given path is of a directory.
79
75
for subdir , _ , files in os .walk (path ):
80
76
for file_name in files :
81
77
if any (
82
78
file_name .endswith (extension ) for extension in FILE_EXTENSIONS_TO_CHECK
83
79
):
84
80
file_extension = os .path .join (subdir , file_name )
85
- failures += is_concatenated (os .path .join (subdir , file_name ))
86
81
87
- exit_code = 1 if failures >= 1 else 0
88
- sys .exit (exit_code )
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 )
0 commit comments