From 1373e1d67aebe66fcc1db4329a5c24ba63a33d28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xavier=20dupr=C3=A9?= Date: Tue, 9 Feb 2021 18:02:19 +0100 Subject: [PATCH 1/4] Add option CRB to put \r back in a file --- _unittests/ut_pycode/test_clean_file.py | 15 +++++++++++++++ src/pyquickhelper/pycode/clean_helper.py | 22 ++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/_unittests/ut_pycode/test_clean_file.py b/_unittests/ut_pycode/test_clean_file.py index b04464fba..487dc78ef 100644 --- a/_unittests/ut_pycode/test_clean_file.py +++ b/_unittests/ut_pycode/test_clean_file.py @@ -23,6 +23,21 @@ def test_clean_file_cr(self): res = clean_files(folder, fLOG=fLOG, posreg="test_clean.*[.]py$") self.assertEmpty(res) + def test_clean_file_crb(self): + fLOG( + __file__, + self._testMethodName, + OutputPrint=__name__ == "__main__") + + folder = os.path.abspath(os.path.dirname(__file__)) + self.assertRaise(lambda: clean_files(folder, op="op"), ValueError) + res = clean_files(folder, fLOG=fLOG, op='CRB', + posreg="test_clean_readme.*[.]py$") + self.assertEmpty(res) + res = clean_files(folder, fLOG=fLOG, op='CR', + posreg="test_clean_readme.*[.]py$") + self.assertEmpty(res) + def test_clean_file_cr_nefg_pattern(self): fLOG( __file__, diff --git a/src/pyquickhelper/pycode/clean_helper.py b/src/pyquickhelper/pycode/clean_helper.py index a02319c59..5b25bab8d 100644 --- a/src/pyquickhelper/pycode/clean_helper.py +++ b/src/pyquickhelper/pycode/clean_helper.py @@ -57,6 +57,7 @@ def clean_files(folder=".", posreg='.*[.]((py)|(rst))$', The following cleaning are available: * ``'CR'``: replaces ``'\\r\\n'`` by ``'\\n'`` + * ``'CRB'``: replaces end of lines ``'\\n'`` by ``'\\r\\n'`` * ``'pep8'``: applies :epkg:`pep8` convention """ def clean_file_cr(name): @@ -67,11 +68,28 @@ def clean_file_cr(name): with open(name, "wb") as f: f.write(new_content) return True - else: - return False + return False + + def clean_file_cr_back(name): + with open(name, "rb") as f: + lines = f.read().split(b'\n') + new_lines = [] + changes = False + for li in lines: + if not li.endswith(b'\r'): + new_lines.append(li + b'\r') + changes = True + else: + new_lines.append(li) + if changes: + with open(name, "wb") as f: + f.write(b'\n'.join(new_lines)) + return changes if op == 'CR': clean_file = clean_file_cr + elif op == 'CRB': + clean_file = clean_file_cr_back elif op == 'pep8': from .code_helper import remove_extra_spaces_and_pep8 clean_file = remove_extra_spaces_and_pep8 From 0d4e555cfa029168d31422d7d706844a93683566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xavier=20dupr=C3=A9?= Date: Tue, 9 Feb 2021 18:26:35 +0100 Subject: [PATCH 2/4] fixes unit test --- _unittests/ut_pycode/test_clean_file.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/_unittests/ut_pycode/test_clean_file.py b/_unittests/ut_pycode/test_clean_file.py index 487dc78ef..8849f04f6 100644 --- a/_unittests/ut_pycode/test_clean_file.py +++ b/_unittests/ut_pycode/test_clean_file.py @@ -31,9 +31,8 @@ def test_clean_file_crb(self): folder = os.path.abspath(os.path.dirname(__file__)) self.assertRaise(lambda: clean_files(folder, op="op"), ValueError) - res = clean_files(folder, fLOG=fLOG, op='CRB', - posreg="test_clean_readme.*[.]py$") - self.assertEmpty(res) + clean_files(folder, fLOG=fLOG, op='CRB', + posreg="test_clean_readme.*[.]py$") res = clean_files(folder, fLOG=fLOG, op='CR', posreg="test_clean_readme.*[.]py$") self.assertEmpty(res) @@ -73,9 +72,8 @@ def test_clean_file_pep8(self): OutputPrint=__name__ == "__main__") folder = os.path.abspath(os.path.dirname(__file__)) - res = clean_files(folder, fLOG=fLOG, - posreg="test_clean.*[.]py$", op='pep8') - self.assertEmpty(res) + clean_files(folder, fLOG=fLOG, + posreg="test_clean.*[.]py$", op='pep8') if __name__ == "__main__": From b65ce39b9fcc12736b1699ee9da7ec5dea22790d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xavier=20dupr=C3=A9?= Date: Tue, 9 Feb 2021 18:28:40 +0100 Subject: [PATCH 3/4] documentation --- src/pyquickhelper/pycode/clean_helper.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pyquickhelper/pycode/clean_helper.py b/src/pyquickhelper/pycode/clean_helper.py index 5b25bab8d..8eec6af67 100644 --- a/src/pyquickhelper/pycode/clean_helper.py +++ b/src/pyquickhelper/pycode/clean_helper.py @@ -50,7 +50,8 @@ def clean_files(folder=".", posreg='.*[.]((py)|(rst))$', :param folder: folder to clean :param posreg: regular expression to select files to process :param negreg: regular expression to skip files to process - :param op: kind of cleaning to do, see below for the available option + :param op: kind of cleaning to do, options are CR, CRb, pep8, + see below for more details :param fLOG: logging function :return: list of processed files From 8b0c92374f895dce26513556ebfdaf30de04b99f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xavier=20dupr=C3=A9?= Date: Tue, 9 Feb 2021 19:01:38 +0100 Subject: [PATCH 4/4] fix unit test --- _unittests/ut_pycode/test_clean_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_unittests/ut_pycode/test_clean_file.py b/_unittests/ut_pycode/test_clean_file.py index 8849f04f6..2c9dd0bb4 100644 --- a/_unittests/ut_pycode/test_clean_file.py +++ b/_unittests/ut_pycode/test_clean_file.py @@ -35,7 +35,7 @@ def test_clean_file_crb(self): posreg="test_clean_readme.*[.]py$") res = clean_files(folder, fLOG=fLOG, op='CR', posreg="test_clean_readme.*[.]py$") - self.assertEmpty(res) + self.assertNotEmpty(res) def test_clean_file_cr_nefg_pattern(self): fLOG(