mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 01:52:56 +01:00
misc: Add multi_regex_edit()
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
1ffdb8728a
commit
8642b73ba1
1 changed files with 32 additions and 0 deletions
|
|
@ -114,6 +114,38 @@ def commit_tmpfile(tmp: str, path: str) -> None: # export
|
||||||
log.slog(log.NOTICE, "saving {}".format(path), caller=caller)
|
log.slog(log.NOTICE, "saving {}".format(path), caller=caller)
|
||||||
os.rename(path + '.tmp', path)
|
os.rename(path + '.tmp', path)
|
||||||
|
|
||||||
|
def multi_regex_edit(spec, strings): # export
|
||||||
|
for cmd in spec:
|
||||||
|
if len(cmd) < 2:
|
||||||
|
raise Exception('Invalid command in multi_regex_edit(): {}'.format(str(cmd)))
|
||||||
|
if cmd[0] == 'sub':
|
||||||
|
rx = re.compile(cmd[1])
|
||||||
|
replacement = cmd[2]
|
||||||
|
r = []
|
||||||
|
for l in strings:
|
||||||
|
r.append(re.sub(rx, replacement, l))
|
||||||
|
strings = r
|
||||||
|
continue
|
||||||
|
if cmd[0] == 'del':
|
||||||
|
rx = re.compile(cmd[1])
|
||||||
|
r = []
|
||||||
|
for l in strings:
|
||||||
|
if rx.search(l) is not None:
|
||||||
|
continue
|
||||||
|
r.append(l)
|
||||||
|
strings = r
|
||||||
|
continue
|
||||||
|
if cmd[0] == 'match':
|
||||||
|
rx = re.compile(cmd[1])
|
||||||
|
r = []
|
||||||
|
for l in strings:
|
||||||
|
if rx.search(l) is not None:
|
||||||
|
r.append(l)
|
||||||
|
strings = r
|
||||||
|
continue
|
||||||
|
raise Exception('Invalid command in multi_regex_edit(): {}'.format(str(cmd)))
|
||||||
|
return strings
|
||||||
|
|
||||||
def dump(prio: int, objects: Iterable, *args, **kwargs) -> None: # export
|
def dump(prio: int, objects: Iterable, *args, **kwargs) -> None: # export
|
||||||
caller = log.get_caller_pos(kwargs=kwargs)
|
caller = log.get_caller_pos(kwargs=kwargs)
|
||||||
log.slog(prio, ",---------- {}".format(' '.join(args)), caller=caller)
|
log.slog(prio, ",---------- {}".format(' '.join(args)), caller=caller)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue