process-text-files.py: Add command cleanup-spaces

The command removes traling empty spaces from lines and trailing empty lines
from files.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2019-03-04 21:18:13 +01:00
commit 586f31d06c

View file

@ -76,6 +76,19 @@ class Cmd(jwutils.Cmd):
return data
return r
@staticmethod
def _cleanup_spaces(data, src, target, context=None):
lines = data.splitlines()
last = len(lines) - 1
r = ''
while last >= 0 and len(lines[last].strip()) == 0:
last -= 1
i = 0
while i <= last:
r += lines[i].rstrip() + '\n'
i += 1
return r
@staticmethod
def _replace_cpp_symbol(data, src, target, context=None):
stopc = "^a-zA-Z0-9_"
@ -313,6 +326,7 @@ class CmdIndentMakefileEquals(Cmd):
return p
def process(self, args, files):
slog(NOTICE, "Beautifying", len(files), "makefiles:")
context = dict()
right_align_match = 2
context["indent"] = args.equal_pos - right_align_match
@ -323,11 +337,30 @@ class CmdIndentMakefileEquals(Cmd):
context["skip-short"] = args.skip_short
context["min-assignments"] = args.min_assignments
replacements = {"blah": "blub"} # just a dummy to use _replace_in_file, TODO: obviate the need
slog(NOTICE, "indenting equal signs in", len(files), "makefiles:")
for dir, name in files:
path = dir + '/' + name
if self._replace_in_file(path, replacements, func=self._cleanup_spaces):
slog(NOTICE, "+ purged spaces :", path)
if self._replace_in_file(path, replacements, func=self._indent_pattern, context=context):
slog(NOTICE, "+ changed", path)
slog(NOTICE, "+ aligned equals :", path)
class CmdCleanupSpaces(Cmd):
def __init__(self):
super(CmdCleanupSpaces, self).__init__("cleanup-spaces", "Remove trailing empty lines")
def add_parser(self, parsers):
p = super(CmdCleanupSpaces, self).add_parser(parsers)
return p
def process(self, args, files):
slog(NOTICE, "Cleaning up unnecessary space in", len(files), "files:")
context = dict()
replacements = {"blah": "blub"} # just a dummy to use _replace_in_file, TODO: obviate the need
for dir, name in files:
path = dir + '/' + name
if self._replace_in_file(path, replacements, func=self._cleanup_spaces, context=context):
slog(NOTICE, "+ purged spaces :", path)
class CmdAddCppNamespace(Cmd):