Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions _unittests/ut_pycode/test_clean_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ 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)
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.assertNotEmpty(res)

def test_clean_file_cr_nefg_pattern(self):
fLOG(
__file__,
Expand Down Expand Up @@ -58,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__":
Expand Down
25 changes: 22 additions & 3 deletions src/pyquickhelper/pycode/clean_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ 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

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):
Expand All @@ -67,11 +69,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
Expand Down