mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 01:52:56 +01:00
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:
parent
a4b447375a
commit
586f31d06c
1 changed files with 35 additions and 2 deletions
|
|
@ -76,6 +76,19 @@ class Cmd(jwutils.Cmd):
|
||||||
return data
|
return data
|
||||||
return r
|
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
|
@staticmethod
|
||||||
def _replace_cpp_symbol(data, src, target, context=None):
|
def _replace_cpp_symbol(data, src, target, context=None):
|
||||||
stopc = "^a-zA-Z0-9_"
|
stopc = "^a-zA-Z0-9_"
|
||||||
|
|
@ -313,6 +326,7 @@ class CmdIndentMakefileEquals(Cmd):
|
||||||
return p
|
return p
|
||||||
|
|
||||||
def process(self, args, files):
|
def process(self, args, files):
|
||||||
|
slog(NOTICE, "Beautifying", len(files), "makefiles:")
|
||||||
context = dict()
|
context = dict()
|
||||||
right_align_match = 2
|
right_align_match = 2
|
||||||
context["indent"] = args.equal_pos - right_align_match
|
context["indent"] = args.equal_pos - right_align_match
|
||||||
|
|
@ -323,11 +337,30 @@ class CmdIndentMakefileEquals(Cmd):
|
||||||
context["skip-short"] = args.skip_short
|
context["skip-short"] = args.skip_short
|
||||||
context["min-assignments"] = args.min_assignments
|
context["min-assignments"] = args.min_assignments
|
||||||
replacements = {"blah": "blub"} # just a dummy to use _replace_in_file, TODO: obviate the need
|
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:
|
for dir, name in files:
|
||||||
path = dir + '/' + name
|
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):
|
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):
|
class CmdAddCppNamespace(Cmd):
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue