add py-replace

This commit is contained in:
Daniel Langbein 2022-10-17 22:02:36 +02:00
parent 682949c1cc
commit d14fdcc75e
7 changed files with 70 additions and 2 deletions

View File

@ -22,7 +22,7 @@ python3 -m build
``` ```
5) Upload the distribution packages with twine. For the username, use `__token__`. For the password, use a 5) Upload the distribution packages with twine. For the username, use `__token__`. For the password, use a
[test.pypi.org API token](https://test.pypi.org/manage/account/#api-tokens): [test.pypi.org API token](https://test.pypi.org/manage/account/#api-tokens):
```shell ```shell
python3 -m twine upload --repository testpypi dist/* python3 -m twine upload --repository testpypi dist/*

View File

@ -3,7 +3,7 @@
[metadata] [metadata]
name = py-regex-replace name = py-regex-replace
version = 0.0.1 version = 0.0.2
author = Daniel Langbein author = Daniel Langbein
author_email = daniel@systemli.org author_email = daniel@systemli.org
description = perform regex-replacement on stdin and output result to stdout 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 ; https://setuptools.readthedocs.io/en/latest/userguide/entry_point.html
console_scripts= console_scripts=
py-regex-replace = py_regex_replace.main:main py-regex-replace = py_regex_replace.main:main
py-replace = py_replace.main:main

View File

43
src/py_replace/main.py Executable file
View 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()

View 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}"

View 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}"