mirror of
https://codeberg.org/privacy1st/py-regex-replace
synced 2024-12-23 02:36:05 +01:00
add py-replace
This commit is contained in:
parent
682949c1cc
commit
d14fdcc75e
@ -3,7 +3,7 @@
|
||||
|
||||
[metadata]
|
||||
name = py-regex-replace
|
||||
version = 0.0.1
|
||||
version = 0.0.2
|
||||
author = Daniel Langbein
|
||||
author_email = daniel@systemli.org
|
||||
description = perform regex-replacement on stdin and output result to stdout
|
||||
@ -33,3 +33,4 @@ where = src
|
||||
; https://setuptools.readthedocs.io/en/latest/userguide/entry_point.html
|
||||
console_scripts=
|
||||
py-regex-replace = py_regex_replace.main:main
|
||||
py-replace = py_replace.main:main
|
||||
|
0
src/py_replace/__init__.py
Normal file
0
src/py_replace/__init__.py
Normal file
43
src/py_replace/main.py
Executable file
43
src/py_replace/main.py
Executable file
@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
text, repl, string, count = parse_args()
|
||||
|
||||
occurrences = string.count(text)
|
||||
if occurrences != count:
|
||||
print(f'[ERROR] Expected {count} occurrences of text in string, but got {occurrences}.')
|
||||
exit(1)
|
||||
|
||||
string = string.replace(text, repl)
|
||||
|
||||
print(string, end='')
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(prog='py-regex-replace')
|
||||
|
||||
parser.add_argument('--text', '-t',
|
||||
help='text to replace',
|
||||
required=True, type=str)
|
||||
parser.add_argument('--repl', '-r',
|
||||
help='replacement',
|
||||
required=True, type=str)
|
||||
parser.add_argument('--count', '-c',
|
||||
help='expected count of text in string',
|
||||
default=1, type=int)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
string = sys.stdin.read()
|
||||
if len(string) == 0:
|
||||
print('[WARNING] The given string is empty.', file=sys.stderr)
|
||||
|
||||
return args.text, args.repl, string, args.count
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
12
test/test_multiline_pattern.sh
Normal file
12
test/test_multiline_pattern.sh
Normal file
@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
string='alpha = a
|
||||
beta = b
|
||||
Exec = foo bar baz
|
||||
end = EOF'
|
||||
pattern='= b\nExec ='
|
||||
repl='='
|
||||
|
||||
string="$(printf '%s' "${string}" | ../src/py_regex_replace/main.py -p "${pattern}" -r "${repl}" -c 1)"
|
||||
|
||||
printf 'RESULT>>>\n%s\n<<<RESULT\n' "${string}"
|
12
test/test_multiline_replacement.sh
Normal file
12
test/test_multiline_replacement.sh
Normal file
@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
string='alpha = a
|
||||
beta = b
|
||||
Exec = foo bar baz
|
||||
end = EOF'
|
||||
pattern='foo bar baz'
|
||||
repl='foo bar baz\n\n o.O\n'
|
||||
|
||||
string="$(printf '%s' "${string}" | ../src/py_regex_replace/main.py -p "${pattern}" -r "${repl}" -c 1)"
|
||||
|
||||
printf 'RESULT>>>\n%s\n<<<RESULT\n' "${string}"
|
Loading…
Reference in New Issue
Block a user