77from typing import Sequence
88
99
10- def _fix_file (filename , is_markdown , chars_to_strip ):
10+ def _fix_file (filename , is_markdown , chars ):
1111 # type: (str, bool, Optional[bytes]) -> bool
1212 with open (filename , mode = 'rb' ) as file_processed :
1313 lines = file_processed .readlines ()
14- newlines = [
15- _process_line (line , is_markdown , chars_to_strip )
16- for line
17- in lines
18- ]
14+ newlines = [_process_line (line , is_markdown , chars ) for line in lines ]
1915 if newlines != lines :
2016 with open (filename , mode = 'wb' ) as file_processed :
2117 for line in newlines :
@@ -25,7 +21,7 @@ def _fix_file(filename, is_markdown, chars_to_strip):
2521 return False
2622
2723
28- def _process_line (line , is_markdown , chars_to_strip ):
24+ def _process_line (line , is_markdown , chars ):
2925 # type: (bytes, bool, Optional[bytes]) -> bytes
3026 if line [- 2 :] == b'\r \n ' :
3127 eol = b'\r \n '
@@ -37,8 +33,8 @@ def _process_line(line, is_markdown, chars_to_strip):
3733 eol = b''
3834 # preserve trailing two-space for non-blank lines in markdown files
3935 if is_markdown and (not line .isspace ()) and line .endswith (b' ' ):
40- return line [:- 2 ].rstrip (chars_to_strip ) + b' ' + eol
41- return line .rstrip (chars_to_strip ) + eol
36+ return line [:- 2 ].rstrip (chars ) + b' ' + eol
37+ return line .rstrip (chars ) + eol
4238
4339
4440def main (argv = None ): # type: (Optional[Sequence[str]]) -> int
@@ -60,8 +56,10 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
6056 )
6157 parser .add_argument (
6258 '--chars' ,
63- help = 'The set of characters to strip from the end of lines. '
64- 'Defaults to all whitespace characters.' ,
59+ help = (
60+ 'The set of characters to strip from the end of lines. '
61+ 'Defaults to all whitespace characters.'
62+ ),
6563 )
6664 parser .add_argument ('filenames' , nargs = '*' , help = 'Filenames to fix' )
6765 args = parser .parse_args (argv )
@@ -86,16 +84,12 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
8684 " (probably filename; use '--markdown-linebreak-ext=EXT')"
8785 .format (ext ),
8886 )
89-
87+ chars = None if args . chars is None else args . chars . encode ( 'utf-8' )
9088 return_code = 0
9189 for filename in args .filenames :
9290 _ , extension = os .path .splitext (filename .lower ())
9391 md = all_markdown or extension in md_exts
94- if _fix_file (
95- filename ,
96- md ,
97- None if args .chars is None else args .chars .encode ('utf-8' ),
98- ):
92+ if _fix_file (filename , md , chars ):
9993 print ('Fixing {}' .format (filename ))
10094 return_code = 1
10195 return return_code
0 commit comments