Post-review fixes

This commit is contained in:
Lucas Cimon 2016-08-31 02:02:33 +01:00
parent c3c870c398
commit cb23c48b0d
2 changed files with 16 additions and 17 deletions

View file

@ -3,26 +3,25 @@ from __future__ import print_function
import argparse import argparse
import os import os
import sys import sys
import tempfile
from pre_commit_hooks.util import cmd_output from pre_commit_hooks.util import cmd_output
def _fix_file(filename, markdown=False): def _fix_file(filename, is_markdown):
with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as tmp_file: with open(filename, mode='rb') as file_processed:
with open(filename, mode='rb') as original_file: lines = file_processed.readlines()
for line in original_file.readlines(): lines = [_process_line(line, is_markdown) for line in lines]
with open(filename, mode='wb') as file_processed:
for line in lines:
file_processed.write(line)
def _process_line(line, is_markdown):
# preserve trailing two-space for non-blank lines in markdown files # preserve trailing two-space for non-blank lines in markdown files
eol = b'\r\n' if line[-2:] == b'\r\n' else b'\n' eol = b'\r\n' if line[-2:] == b'\r\n' else b'\n'
if markdown and (not line.isspace()) and line.endswith(b' ' + eol): if is_markdown and (not line.isspace()) and line.endswith(b' ' + eol):
line = line.rstrip(b' \r\n') # restricted stripping: e.g. \t are not stripped return line.rstrip() + b' ' + eol
# only preserve if there are no trailing tabs or unusual whitespace return line.rstrip() + eol
if not line[-1:].isspace():
tmp_file.write(line + b' ' + eol)
continue
tmp_file.write(line.rstrip() + eol)
os.remove(filename)
os.rename(tmp_file.name, filename)
def fix_trailing_whitespace(argv=None): def fix_trailing_whitespace(argv=None):

View file

@ -42,7 +42,7 @@ def test_fixes_trailing_markdown_whitespace(filename, input_s, output, tmpdir):
MD_TESTS_2 = ( MD_TESTS_2 = (
('foo.txt', 'foo \nbar \n \n', 'foo \nbar\n\n'), ('foo.txt', 'foo \nbar \n \n', 'foo \nbar\n\n'),
('bar.Markdown', 'bar \nbaz\t\n\t\n', 'bar \nbaz\n\n'), ('bar.Markdown', 'bar \nbaz\t\n\t\n', 'bar \nbaz\n\n'),
('bar.MD', 'bar \nbaz\t \n\t\n', 'bar \nbaz\n\n'), ('bar.MD', 'bar \nbaz\t \n\t\n', 'bar \nbaz \n\n'),
('.txt', 'baz \nquux \t\n\t\n', 'baz\nquux\n\n'), ('.txt', 'baz \nquux \t\n\t\n', 'baz\nquux\n\n'),
('txt', 'foo \nbaz \n\t\n', 'foo\nbaz\n\n'), ('txt', 'foo \nbaz \n\t\n', 'foo\nbaz\n\n'),
) )