From 0a91e568c8b8762b15d67a6a452c0ce7eb218a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Giraud?= Date: Fri, 27 Oct 2023 17:58:50 +0200 Subject: [PATCH 1/6] comment --- bin/rbt | 62 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/bin/rbt b/bin/rbt index 8f3b475..5859936 100755 --- a/bin/rbt +++ b/bin/rbt @@ -1,4 +1,5 @@ #!/usr/bin/python3 -u +# -*- coding: utf-8 -*- import getopt import os @@ -150,7 +151,7 @@ def readfile(filename, fdw): for line in fd_in: fdw(line) -def apply_on_file(simulate, begin_tag, end_tag, delete_tags, replace, fileinfo): +def apply_on_file(config, fileinfo): (use_stdout_ori, filename) = fileinfo use_stdout = use_stdout_ori move = False @@ -167,8 +168,8 @@ def apply_on_file(simulate, begin_tag, end_tag, delete_tags, replace, fileinfo): rest = chomp(line) first = eol(line) while rest: - index_begin = rest.find(begin_tag) - index_end = rest.find(end_tag) + index_begin = rest.find(config.begin_tag) + index_end = rest.find(config.end_tag) if index_begin == -1 and index_end == -1: if depth == 0: fd_out.write(rest) @@ -176,12 +177,12 @@ def apply_on_file(simulate, begin_tag, end_tag, delete_tags, replace, fileinfo): first = '' elif index_begin >= 0 and index_end == -1: if depth == 0: - if not delete_tags: - fd_out.write(rest[:index_begin+len(begin_tag)]) + if not config.delete_tags: + fd_out.write(rest[:index_begin+len(config.begin_tag)]) else: fd_out.write(rest[:index_begin]) depth = depth + 1 - rest = rest[index_begin+len(begin_tag):] + rest = rest[index_begin+len(config.begin_tag):] if rest == '': fd_out.write(eol(line)) first = '' @@ -190,34 +191,35 @@ def apply_on_file(simulate, begin_tag, end_tag, delete_tags, replace, fileinfo): if depth < 0: error("reach end tag before start tag") if depth == 0: - replace(fd_out.write) + if config.replace: + config.replace(fd_out.write) fd_out.write(first) - if not delete_tags: - fd_out.write(rest[index_end:index_end+len(end_tag)]) + if not config.delete_tags: + fd_out.write(rest[index_end:index_end+len(config.end_tag)]) else: fd_out.write(rest[index_end:index_end]) - rest = rest[index_end+len(end_tag):] + rest = rest[index_end+len(config.end_tag):] first = '' elif index_begin >= 0 and index_end >= 0: if index_begin < index_end: if depth == 0: - if not delete_tags: - fd_out.write(rest[:index_begin+len(begin_tag)]) + if not config.delete_tags: + fd_out.write(rest[:index_begin+len(config.begin_tag)]) else: fd_out.write(rest[:index_begin]) depth = depth + 1 - rest = rest[index_begin+len(begin_tag):] + rest = rest[index_begin+len(config.begin_tag):] else: depth = depth - 1 if depth < 0: error("reach end tag before start tag") if depth == 0: - replace(fd_out.write) - fd_out.write('hello') + if config.replace: + config.replace(fd_out.write) fd_out.write(first) - if not delete_tags: - fd_out.write(end_tag) - rest = rest[index_end+len(end_tag):] + if not config.delete_tags: + fd_out.write(config.end_tag) + rest = rest[index_end+len(config.end_tag):] first = '' else: if depth == 0: @@ -228,9 +230,11 @@ def apply_on_file(simulate, begin_tag, end_tag, delete_tags, replace, fileinfo): print('Processed: %s' % (filename,), file=sys.stderr) -def apply_replacements(simulate, begin_tag, end_tag, delete_tags, replace, files): + + +def apply_replacements(config, files): for afile in files: - apply_on_file(simulate, begin_tag, end_tag, delete_tags, replace, afile) + apply_on_file(config, afile) class Config: @@ -240,15 +244,17 @@ class Config: self.end_tag = None self.replace = None self.simulate = False + self.comment = False + self.uncomment = False self.delete_tags = False self.files = [] def parse(self, arguments): opts, args = [], [] try: - opts, args = getopt.getopt(arguments, "hvsdb:e:r:R:", + opts, args = getopt.getopt(arguments, "hvsdb:e:r:R:cu", ["help", "version", "simulate", "delete-tags", "begin-tag=", "end-tag=", "replace=", - "replace-file="]) + "replace-file=", "comment", "uncomment"]) except getopt.GetoptError as e: error(e) @@ -260,6 +266,10 @@ class Config: usage() if o in ("-v", "--version"): version() + if o in ("-c", "--comment"): + self.comment = True + if o in ("-u", "--uncomment"): + self.uncomment = True if o in ("-b", "--begin-tag"): self.begin_tag = a if o in ("-e", "--end-tag"): @@ -286,7 +296,11 @@ class Config: error("option --begin-tag is required") if not self.end_tag: error("option --end-tag is required") - if not self.replace: + if self.comment and self.uncomment: + error("options --comment and --uncomment are exclusives") + if (self.comment or self.uncomment) and self.replace: + error("options (--comment or --uncomment) and (--replace or --replace-file) are exclusives") + if not self.replace and (not self.comment and not self.uncomment): error("option --replace or --replace-file is required") @@ -326,6 +340,6 @@ if __name__ == '__main__': config.validate() paths = as_real_files(config.get_files()) - apply_replacements(config.simulate, config.begin_tag, config.end_tag, config.delete_tags, config.replace, paths) + apply_replacements(config, paths) close_files(paths) From fc595e85b8804c630047259503bce803a15255d7 Mon Sep 17 00:00:00 2001 From: jfgiraud Date: Sun, 5 Nov 2023 19:30:48 +0100 Subject: [PATCH 2/6] Rewrite --- bin/rbt | 94 +++++++++++++++++++++++++-------------------------------- 1 file changed, 41 insertions(+), 53 deletions(-) diff --git a/bin/rbt b/bin/rbt index 5859936..b181338 100755 --- a/bin/rbt +++ b/bin/rbt @@ -151,6 +151,16 @@ def readfile(filename, fdw): for line in fd_in: fdw(line) +def find_next(line, tags): + index_min = len(line) + found = None + for tag in tags: + index = line.find(tag) + if index != -1 and index < index_min: + index_min = index + found = tag + return (found, index_min) if found else (None, -1) + def apply_on_file(config, fileinfo): (use_stdout_ori, filename) = fileinfo use_stdout = use_stdout_ori @@ -163,67 +173,45 @@ def apply_on_file(config, fileinfo): (fno, temporary_file) = tempfile.mkstemp() fd_out = open(fno, 'wt') move = True + tags = { config.begin_tag, config.end_tag } + debug = False with fd_out: for line in fd_in: - rest = chomp(line) - first = eol(line) + rest = line + if debug: + print("rest=", rest, file=sys.stderr) while rest: - index_begin = rest.find(config.begin_tag) - index_end = rest.find(config.end_tag) - if index_begin == -1 and index_end == -1: - if depth == 0: + (next_tag, next_index) = find_next(rest, tags) + if debug: + print("rest=", rest, file=sys.stderr) + print("nt=", next_tag, "i=", next_index, file=sys.stderr) + print("----", file=sys.stderr) + if depth == 0: + if not next_tag: fd_out.write(rest) - rest = None - first = '' - elif index_begin >= 0 and index_end == -1: - if depth == 0: - if not config.delete_tags: - fd_out.write(rest[:index_begin+len(config.begin_tag)]) - else: - fd_out.write(rest[:index_begin]) - depth = depth + 1 - rest = rest[index_begin+len(config.begin_tag):] - if rest == '': - fd_out.write(eol(line)) - first = '' - elif index_begin == -1 and index_end >= 0: - depth = depth - 1 - if depth < 0: + break + if next_tag == config.end_tag: error("reach end tag before start tag") - if depth == 0: - if config.replace: - config.replace(fd_out.write) - fd_out.write(first) + if next_tag == config.begin_tag: + fd_out.write(rest[:next_index]) if not config.delete_tags: - fd_out.write(rest[index_end:index_end+len(config.end_tag)]) - else: - fd_out.write(rest[index_end:index_end]) - rest = rest[index_end+len(config.end_tag):] - first = '' - elif index_begin >= 0 and index_end >= 0: - if index_begin < index_end: - if depth == 0: - if not config.delete_tags: - fd_out.write(rest[:index_begin+len(config.begin_tag)]) - else: - fd_out.write(rest[:index_begin]) + fd_out.write(next_tag) + rest = rest[next_index+len(next_tag):] depth = depth + 1 - rest = rest[index_begin+len(config.begin_tag):] - else: + else: + if not next_tag: + fd_out.write(eol(rest)) + break + if next_tag == config.begin_tag: + error("reach begin tag before end tag") + if next_tag == config.end_tag: + config.replace(fd_out.write) + if not config.delete_tags: + fd_out.write(next_tag) + rest = rest[next_index+len(next_tag):] depth = depth - 1 - if depth < 0: - error("reach end tag before start tag") - if depth == 0: - if config.replace: - config.replace(fd_out.write) - fd_out.write(first) - if not config.delete_tags: - fd_out.write(config.end_tag) - rest = rest[index_end+len(config.end_tag):] - first = '' - else: - if depth == 0: - fd_out.write(eol(line)) + +#printf 'hello\ntiti*BEGIN*tutu*END*toto*BEGIN*entre\net puis la*END*fin' | ./bin/rbt -b '*BEGIN*' -e '*END*' --replace ppp if not simulate and move: shutil.move(temporary_file, filename) From b268ade0f69384c4eed8c29aaea6316ebb5d93eb Mon Sep 17 00:00:00 2001 From: jfgiraud Date: Sun, 5 Nov 2023 20:06:10 +0100 Subject: [PATCH 3/6] Rewrite --- bin/rbt | 22 ++++++++++---- tests/rbt_tests.sh | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 5 deletions(-) diff --git a/bin/rbt b/bin/rbt index b181338..0e7c8d8 100755 --- a/bin/rbt +++ b/bin/rbt @@ -141,9 +141,9 @@ def chomp(x): def eol(x): - if x.endswith("\r\n"): return x[-2:] - if x.endswith("\n") or x.endswith("\r"): return x[-1:] - return '' + if x.endswith("\r\n"): return (x[:-2], x[-2:]) + if x.endswith("\n") or x.endswith("\r"): return (x[:-1], x[-1:]) + return (x, None) def readfile(filename, fdw): @@ -184,6 +184,7 @@ def apply_on_file(config, fileinfo): (next_tag, next_index) = find_next(rest, tags) if debug: print("rest=", rest, file=sys.stderr) + print("depth=", depth, file=sys.stderr) print("nt=", next_tag, "i=", next_index, file=sys.stderr) print("----", file=sys.stderr) if depth == 0: @@ -198,14 +199,25 @@ def apply_on_file(config, fileinfo): fd_out.write(next_tag) rest = rest[next_index+len(next_tag):] depth = depth + 1 + written = False + (c, e) = eol(rest) + if not e: + config.replace(fd_out.write) + written = True else: if not next_tag: - fd_out.write(eol(rest)) + (c, e) = eol(rest) + if not written: + config.replace(fd_out.write) + fd_out.write(e) + written = True break if next_tag == config.begin_tag: error("reach begin tag before end tag") if next_tag == config.end_tag: - config.replace(fd_out.write) + if not written: + config.replace(fd_out.write) + written = True if not config.delete_tags: fd_out.write(next_tag) rest = rest[next_index+len(next_tag):] diff --git a/tests/rbt_tests.sh b/tests/rbt_tests.sh index 2a64bab..886736d 100755 --- a/tests/rbt_tests.sh +++ b/tests/rbt_tests.sh @@ -30,6 +30,57 @@ function assert_exec_equals() { } +## No tags, no change + +cat > /tmp/lorem_ipsum <<'EOF' +Lorem ipsum dolor sit amet. +EOF + +read -r -d '' EXPECTED <<'EOF' +Lorem ipsum dolor sit amet. +EOF + +assert_exec_equals \ + "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ + "${EXPECTED}" + +rm -f /tmp/lorem_ipsum + +## Tags, monoline + +cat > /tmp/lorem_ipsum <<'EOF' +Lorem *BEGIN*ipsum dolor *END*sit amet. +EOF + +read -r -d '' EXPECTED <<'EOF' +Lorem *BEGIN*new text here*END*sit amet. +EOF + +assert_exec_equals \ + "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ + "${EXPECTED}" + +rm -f /tmp/lorem_ipsum + +## Tags, monoline + +cat > /tmp/lorem_ipsum <<'EOF' +Lorem *BEGIN*ipsum dolor *END*sit *BEGIN*amet*END*. +EOF + +read -r -d '' EXPECTED <<'EOF' +Lorem *BEGIN*new text here*END*sit *BEGIN*new text here*END*. +EOF + +assert_exec_equals \ + "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ + "${EXPECTED}" + +rm -f /tmp/lorem_ipsum + + +## Tag multiline + cat > /tmp/lorem_ipsum <<'EOF' Lorem ipsum dolor sit amet, consectetur adipiscing elit. *BEGIN*Pellentesque maximus faucibus lectus, in ultricies lorem volutpat in. Sed rutrum risus et vehicula rhoncus. Nunc sed est et eros mollis vehicula. Pellentesque semper dignissim maximus. @@ -48,6 +99,30 @@ assert_exec_equals \ "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ "${EXPECTED}" +## Tag multiline + +cat > /tmp/lorem_ipsum <<'EOF' +Lorem ipsum dolor sit amet, consectetur adipiscing elit. *BEGIN* +Pellentesque maximus faucibus lectus, in ultricies lorem volutpat in. +Sed rutrum risus et vehicula rhoncus. Nunc sed est et eros mollis vehicula. Pellentesque semper dignissim maximus. +Praesent in justo et ante faucibus eleifend in ac est. Donec orci magna, pellentesque id libero nec, faucibus porta purus. +Pellentesque luctus sollicitudin tortor sit amet accumsan. Nullam mauris felis, egestas in faucibus in, feugiat vel arcu. +*END* +Cras rhoncus aliquam tristique. +EOF + + +read -r -d '' EXPECTED <<'EOF' +Lorem ipsum dolor sit amet, consectetur adipiscing elit. *BEGIN* +new text here*END* +Cras rhoncus aliquam tristique. +EOF + +assert_exec_equals \ + "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ + "${EXPECTED}" + + echo "${ok}/${total} (${ko} errors)" #rm -f /tmp/lorem_ipsum From 281178aee6307ad152e814f4017a9866121a6700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Giraud?= Date: Fri, 24 Nov 2023 15:45:15 +0100 Subject: [PATCH 4/6] comment --- .editorconfig | 1 + bin/rbt | 198 +++++++++++++++++++++-------- doc/rbt.adoc | 16 ++- tests/rbt_tests.sh | 309 ++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 442 insertions(+), 82 deletions(-) diff --git a/.editorconfig b/.editorconfig index f3deef6..b6e6b29 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,6 +8,7 @@ indent_size=4 [*.sh] indent_style=space indent_size=2 +trim_trailing_whitespace=false [{*.yml,*.yaml}] indent_style=space diff --git a/bin/rbt b/bin/rbt index 0e7c8d8..f540e61 100755 --- a/bin/rbt +++ b/bin/rbt @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import getopt +import io import os import os.path import re @@ -9,9 +10,6 @@ import sys import tempfile import shutil import stat -import difflib -import shlex -import subprocess simulate = False @@ -108,6 +106,52 @@ replace-between-tags 0.0.13 #END_DO_NOT_MODIFY:make update-doc ''' +CRLF = '\r\n' +LF = '\n' +CR = '\r' +LINE_ENDING = LF + + +def block_comment(prefix, suffix): + return lambda fd, buf: fd.write(prefix + buf.getvalue().strip() + suffix) + + +def block_clean(prefix, suffix, s): + if s.startswith(prefix): + s = s[len(prefix):] + if s.endswith(suffix): + s = s[:-len(suffix)] + return s + + +def block_uncomment(prefix, suffix): + return lambda fd, buf: fd.write(block_clean(prefix, suffix, buf.getvalue().strip())) + + +def line_comment(prefix): + return lambda fd, buf: fd.write(LINE_ENDING.join([prefix + l.strip() for l in buf.getvalue().strip().split(LINE_ENDING)])) + + +def line_uncomment(prefix): + return lambda fd, buf: fd.write(LINE_ENDING.join([l.strip() if not l.startswith(prefix) else l[len(prefix):] for l in buf.getvalue().strip().split(LINE_ENDING)])) + + +def block(prefix, suffix): + return block_comment(prefix, suffix), block_uncomment(prefix, suffix) + + +def line(prefix): + return line_comment(prefix), line_uncomment(prefix) + +styles = { + 'java-block': block('/* ', ' */'), + 'java-line': line('// '), + 'bash': line('# '), + 'sql': line('-- '), + 'xml': block('') +} + + def usage(): lines = _USAGE.split('\n') display = False @@ -120,30 +164,28 @@ def usage(): print(line) sys.exit(0) + def version(): lines = _USAGE.split('\n') for i, line in enumerate(lines): if line.startswith("#END_DO_NOT_MODIFY:make update-doc"): - previous_line = lines[i-1] + previous_line = lines[i - 1] print(previous_line.split()[1]) break sys.exit(0) + def error(message): print(message, file=sys.stderr) sys.exit(1) def chomp(x): - if x.endswith("\r\n"): return x[:-2] - if x.endswith("\n") or x.endswith("\r"): return x[:-1] - return x - - -def eol(x): - if x.endswith("\r\n"): return (x[:-2], x[-2:]) - if x.endswith("\n") or x.endswith("\r"): return (x[:-1], x[-1:]) - return (x, None) + if x.endswith(CRLF): + return x[:-2], x[-2:] + if x.endswith(LF) or x.endswith(CR): + return x[:-1], x[-1:] + return x, None def readfile(filename, fdw): @@ -151,7 +193,8 @@ def readfile(filename, fdw): for line in fd_in: fdw(line) -def find_next(line, tags): + +def find_next(line, tags, use_re=False): index_min = len(line) found = None for tag in tags: @@ -161,11 +204,13 @@ def find_next(line, tags): found = tag return (found, index_min) if found else (None, -1) + def apply_on_file(config, fileinfo): (use_stdout_ori, filename) = fileinfo use_stdout = use_stdout_ori move = False with open(filename, 'rt') as fd_in: + text_inside = io.StringIO() depth = 0 if use_stdout or simulate: fd_out = open(sys.stdout.fileno(), 'w', closefd=False) @@ -173,65 +218,85 @@ def apply_on_file(config, fileinfo): (fno, temporary_file) = tempfile.mkstemp() fd_out = open(fno, 'wt') move = True - tags = { config.begin_tag, config.end_tag } - debug = False + tags = {config.begin_tag, config.end_tag} with fd_out: for line in fd_in: - rest = line - if debug: - print("rest=", rest, file=sys.stderr) + begin_of_line = True + rest, le = chomp(line) while rest: (next_tag, next_index) = find_next(rest, tags) - if debug: - print("rest=", rest, file=sys.stderr) - print("depth=", depth, file=sys.stderr) - print("nt=", next_tag, "i=", next_index, file=sys.stderr) - print("----", file=sys.stderr) + # if (config.comment or config.uncomment) and next_index != 0: + # text_inside.write(LINE_ENDING) + if depth > 0 and begin_of_line and (config.comment or config.uncomment): + text_inside.write(LINE_ENDING) + if next_index != 0: + begin_of_line = False if depth == 0: if not next_tag: fd_out.write(rest) break - if next_tag == config.end_tag: + if config.begin_tag != config.end_tag and next_tag == config.end_tag: error("reach end tag before start tag") if next_tag == config.begin_tag: fd_out.write(rest[:next_index]) if not config.delete_tags: fd_out.write(next_tag) - rest = rest[next_index+len(next_tag):] + rest = rest[next_index + len(next_tag):] + # if the opening tag is followed by crlf, insert it + if len(rest) == 0 and le is not None: + fd_out.write(LINE_ENDING) depth = depth + 1 written = False - (c, e) = eol(rest) - if not e: - config.replace(fd_out.write) - written = True else: - if not next_tag: - (c, e) = eol(rest) - if not written: - config.replace(fd_out.write) - fd_out.write(e) - written = True + if next_tag: + if config.begin_tag != config.end_tag and next_tag == config.begin_tag: + depth = depth + 1 + if config.comment or config.uncomment: + text_inside.write("xx"+rest[:next_index]) + rest = rest[next_index + len(next_tag):] + elif next_tag == config.end_tag: + depth = depth - 1 + if config.comment or config.uncomment: + text_inside.write(rest[:next_index]) + rest = rest[next_index + len(next_tag):] + if depth < 0: + error("reach end tag without corresponding begin tag") + if depth == 0: + if not written: + # if begin_of_line: #config.new_line_before_end_tag: + # fd_out.write(config.line_ending) + #print("-- %s" % text_inside.getvalue(), file=sys.stderr) + if config.comment: + styles[config.style][0](fd_out, text_inside) + elif config.uncomment: + styles[config.style][1](fd_out, text_inside) + else: + config.replace(fd_out.write) + # if config.comment or config.uncomment: + # text_inside.write(LINE_ENDING) + if begin_of_line: + fd_out.write(LINE_ENDING) + if not config.delete_tags: + fd_out.write(next_tag) + written = True + text_inside = io.StringIO() + else: + error("case not supported for tag '%s'" % next_tag) + else: + if config.comment or config.uncomment: + text_inside.write(rest) break - if next_tag == config.begin_tag: - error("reach begin tag before end tag") - if next_tag == config.end_tag: - if not written: - config.replace(fd_out.write) - written = True - if not config.delete_tags: - fd_out.write(next_tag) - rest = rest[next_index+len(next_tag):] - depth = depth - 1 + begin_of_line = False + if depth == 0 or written: + fd_out.write(LINE_ENDING) -#printf 'hello\ntiti*BEGIN*tutu*END*toto*BEGIN*entre\net puis la*END*fin' | ./bin/rbt -b '*BEGIN*' -e '*END*' --replace ppp + # printf 'hello\ntiti*BEGIN*tutu*END*toto*BEGIN*entre\net puis la*END*fin' | ./bin/rbt -b '*BEGIN*' -e '*END*' --replace ppp if not simulate and move: shutil.move(temporary_file, filename) print('Processed: %s' % (filename,), file=sys.stderr) - - def apply_replacements(config, files): for afile in files: apply_on_file(config, afile) @@ -252,15 +317,20 @@ class Config: def parse(self, arguments): opts, args = [], [] try: - opts, args = getopt.getopt(arguments, "hvsdb:e:r:R:cu", - ["help", "version", "simulate", "delete-tags", "begin-tag=", "end-tag=", "replace=", - "replace-file=", "comment", "uncomment"]) + opts, args = getopt.getopt(arguments, "hvsdb:B:e:E:r:R:c:u:DU", + ["help", "version", "simulate", "delete-tags", "begin-tag=", "begin-regexp-tag=", "end-tag=", + "end-regexp-tag=", + "replace=", + "replace-file=", "comment=", "uncomment=", "dos", + "unix"]) except getopt.GetoptError as e: error(e) if len(opts) == 0: usage() + global LINE_ENDING + for o, a in opts: if o in ("-h", "--help"): usage() @@ -268,16 +338,30 @@ class Config: version() if o in ("-c", "--comment"): self.comment = True + self.style = a if o in ("-u", "--uncomment"): self.uncomment = True + self.style = a if o in ("-b", "--begin-tag"): self.begin_tag = a + self.use_re = False + if o in ("-B", "--begin-regexp-tag"): + self.begin_tag = a + self.use_re = True if o in ("-e", "--end-tag"): self.end_tag = a + self.use_re = False + if o in ("-E", "--end-regexp-tag"): + self.end_tag = a + self.use_re = True if o in ("-s", "--simulate"): self.simulate = True if o in ("-d", "--delete-tags"): self.delete_tags = True + if o in ("-D", "--dos"): + LINE_ENDING = CRLF + if o in ("-U", "--unix"): + LINE_ENDING = LF if o in ("-r", "--replace"): # if -s in last position in the command line, a='' # https://stackoverflow.com/questions/21053988/lambda-function-accessing-outside-variable @@ -293,15 +377,18 @@ class Config: def validate(self): if not self.begin_tag: - error("option --begin-tag is required") + error("option --begin-tag or --begin-regexp-tag is required") if not self.end_tag: - error("option --end-tag is required") + error("option --end-tag or --end-regexp-tag is required") if self.comment and self.uncomment: error("options --comment and --uncomment are exclusives") if (self.comment or self.uncomment) and self.replace: error("options (--comment or --uncomment) and (--replace or --replace-file) are exclusives") if not self.replace and (not self.comment and not self.uncomment): - error("option --replace or --replace-file is required") + error("option (--comment or --uncomment) or (--replace or --replace-file) is required") + if self.comment or self.uncomment: + if self.style not in styles: + error("style not known") def create_tmp_and_init(fd_in): @@ -334,7 +421,6 @@ def close_files(files): if __name__ == '__main__': - config = Config() config.parse(sys.argv[1:]) config.validate() diff --git a/doc/rbt.adoc b/doc/rbt.adoc index abab4c4..1994178 100644 --- a/doc/rbt.adoc +++ b/doc/rbt.adoc @@ -27,9 +27,18 @@ With no FILE, or when FILE is `-`, read standard input. *-b* _string_, *--begin-tag* _string_:: The begin tag to search. +*-d*, *--delete*:: +Delete begin/end tags after replacing. + +*-D*, *--dos*:: +Use Dos/Windows line ending characters. + *-e* _string_, *--end-tag* _string_:: The end tag to search. +*-h*:: +Display help. + *-r* _string_, *--replace* _string_:: The string used to replace. @@ -39,11 +48,8 @@ The file used to replace. *-s*, *--simulate*:: Force output to STDOUT. -*-d*, *--delete*:: -Delete begin/end tags after replacing. - -*-h*:: -Display help. +*-U*, *--unix*:: +Use Unix line ending character. *-v*:: Display version. diff --git a/tests/rbt_tests.sh b/tests/rbt_tests.sh index 886736d..2d24d62 100755 --- a/tests/rbt_tests.sh +++ b/tests/rbt_tests.sh @@ -10,13 +10,15 @@ function assert_equals() { total=$((total+1)) expected="$1" actual="$2" - message="$3" + command="$3" + message="$4" if [[ "$expected" == "$actual" ]]; then ok=$((ok+1)) echo "OK: ${message}" >&2 else ko=$((ko+1)) echo "KO: ${message}" >&2 + echo " command: ${command}" >&2 echo " expects: ${expected}" >&2 echo " but receives: ${actual}" >&2 fi @@ -26,11 +28,12 @@ function assert_exec_equals() { command="${1}" actual=$(eval "${command}") expected="${2}" - assert_equals "${expected}" "${actual}" "${command}" + message="${3}" + assert_equals "${expected}" "${actual}" "${command}" "${message}" } -## No tags, no change +################################################################################################################ cat > /tmp/lorem_ipsum <<'EOF' Lorem ipsum dolor sit amet. @@ -42,11 +45,12 @@ EOF assert_exec_equals \ "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ - "${EXPECTED}" + "${EXPECTED}" \ + "#1: No block, no change" rm -f /tmp/lorem_ipsum -## Tags, monoline +################################################################################################################ cat > /tmp/lorem_ipsum <<'EOF' Lorem *BEGIN*ipsum dolor *END*sit amet. @@ -58,11 +62,12 @@ EOF assert_exec_equals \ "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ - "${EXPECTED}" + "${EXPECTED}" \ + "#2: Block on one line" rm -f /tmp/lorem_ipsum -## Tags, monoline +################################################################################################################ cat > /tmp/lorem_ipsum <<'EOF' Lorem *BEGIN*ipsum dolor *END*sit *BEGIN*amet*END*. @@ -74,32 +79,146 @@ EOF assert_exec_equals \ "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ - "${EXPECTED}" + "${EXPECTED}" \ + "#3: Two blocks on the same line" rm -f /tmp/lorem_ipsum +################################################################################################################ -## Tag multiline +cat > /tmp/lorem_ipsum <<'EOF' +Lorem *BEGIN*ipsum dolor *END*sit amet, consectetur +*BEGIN*adipiscing elit*END*. +EOF + + +read -r -d '' EXPECTED <<'EOF' +Lorem *BEGIN*new text here*END*sit amet, consectetur +*BEGIN*new text here*END*. +EOF + +assert_exec_equals \ + "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ + "${EXPECTED}" \ + "#4: Two blocks on consecutive lines" + + +################################################################################################################ cat > /tmp/lorem_ipsum <<'EOF' -Lorem ipsum dolor sit amet, consectetur adipiscing elit. *BEGIN*Pellentesque maximus faucibus lectus, in ultricies lorem volutpat in. -Sed rutrum risus et vehicula rhoncus. Nunc sed est et eros mollis vehicula. Pellentesque semper dignissim maximus. -Praesent in justo et ante faucibus eleifend in ac est. Donec orci magna, pellentesque id libero nec, faucibus porta purus. -Pellentesque luctus sollicitudin tortor sit amet accumsan. Nullam mauris felis, egestas in faucibus in, feugiat vel arcu. -*END*Cras rhoncus aliquam tristique. +Lorem *BEGIN*ipsum dolor *END*sit +amet, +consectetur *BEGIN*adipiscing elit*END*. +EOF + + +read -r -d '' EXPECTED <<'EOF' +Lorem *BEGIN*new text here*END*sit +amet, +consectetur *BEGIN*new text here*END*. +EOF + +assert_exec_equals \ + "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ + "${EXPECTED}" \ + "#5: Two blocks on non consecutive lines" + +################################################################################################################ + +cat > /tmp/lorem_ipsum <<'EOF' +Lorem *BEGIN*ipsum dolor *END*sit +amet, +consectetur *BEGIN* +adipiscing elit*END*. +EOF + + +read -r -d '' EXPECTED <<'EOF' +Lorem *BEGIN*new text here*END*sit +amet, +consectetur *BEGIN* +new text here*END*. +EOF + +assert_exec_equals \ + "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ + "${EXPECTED}" \ + "#6: Two blocks on non consecutive lines. The second block is on two lines (force LF after begin)." + + +################################################################################################################ + +cat > /tmp/lorem_ipsum <<'EOF' +Lorem *BEGIN*ipsum dolor *END*sit +amet, +consectetur *BEGIN* +adipiscing +elit +*END*. EOF read -r -d '' EXPECTED <<'EOF' -Lorem ipsum dolor sit amet, consectetur adipiscing elit. *BEGIN*new text here -*END*Cras rhoncus aliquam tristique. +Lorem *BEGIN*new text here*END*sit +amet, +consectetur *BEGIN* +new text here +*END*. EOF assert_exec_equals \ "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ - "${EXPECTED}" + "${EXPECTED}" \ + "#7: Two blocks on non consecutive lines. The second block is on two lines (force LF after begin and before end)." -## Tag multiline + +################################################################################################################ + +cat > /tmp/lorem_ipsum <<'EOF' +Lorem *BEGIN*ipsum dolor *END*sit +amet, consectetur +*BEGIN* +adipiscing +elit *END*. +EOF + + +read -r -d '' EXPECTED <<'EOF' +Lorem *BEGIN*new text here*END*sit +amet, consectetur +*BEGIN* +new text here*END*. +EOF + +assert_exec_equals \ + "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ + "${EXPECTED}" \ + "#8: Two blocks on non consecutive lines. The second block is on two lines (force LF after begin)." + + +################################################################################################################ + +cat > /tmp/lorem_ipsum <<'EOF' +Lorem *BEGIN*ipsum dolor +*END*sit amet, consectetur *BEGIN* +adipiscing +elit *END*. +EOF + + +read -r -d '' EXPECTED <<'EOF' +Lorem *BEGIN*new text here +*END*sit amet, consectetur *BEGIN* +new text here*END*. +EOF + +assert_exec_equals \ + "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ + "${EXPECTED}" \ + "#9: Two blocks with end of the first block/begin of the second block are on the same line." + + +################################################################################################################ cat > /tmp/lorem_ipsum <<'EOF' Lorem ipsum dolor sit amet, consectetur adipiscing elit. *BEGIN* @@ -111,17 +230,165 @@ Pellentesque luctus sollicitudin tortor sit amet accumsan. Nullam mauris felis, Cras rhoncus aliquam tristique. EOF - read -r -d '' EXPECTED <<'EOF' Lorem ipsum dolor sit amet, consectetur adipiscing elit. *BEGIN* -new text here*END* +new text here +*END* Cras rhoncus aliquam tristique. EOF assert_exec_equals \ "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ - "${EXPECTED}" + "${EXPECTED}" \ + "#10: One block on several lines" + +################################################################################################################ + +cat > /tmp/lorem_ipsum <<'EOF' +Lorem ~ipsum dolor~ sit amet. +EOF + +read -r -d '' EXPECTED <<'EOF' +Lorem ~new text here~ sit amet. +EOF + +assert_exec_equals \ + "cd .. ; ./bin/rbt -b '~' -e '~' -r 'new text here' < /tmp/lorem_ipsum" \ + "${EXPECTED}" \ + "#11: Block on one line (begin and end strings are equal)" +rm -f /tmp/lorem_ipsum + +################################################################################################################ + +cat > /tmp/lorem_ipsum <<'EOF' +Lorem ipsum dolor sit amet, consectetur adipiscing elit. ~ +Pellentesque maximus faucibus lectus, in ultricies lorem volutpat in. +Sed rutrum risus et vehicula rhoncus. Nunc sed est et eros mollis vehicula. Pellentesque semper dignissim maximus. +Praesent in justo et ante faucibus eleifend in ac est. Donec orci magna, pellentesque id libero nec, faucibus porta purus. +Pellentesque luctus sollicitudin tortor sit amet accumsan. Nullam mauris felis, egestas in faucibus in, feugiat vel arcu. +~ +Cras rhoncus aliquam tristique. +EOF + +read -r -d '' EXPECTED <<'EOF' +Lorem ipsum dolor sit amet, consectetur adipiscing elit. ~ +new text here +~ +Cras rhoncus aliquam tristique. +EOF + +assert_exec_equals \ + "cd .. ; ./bin/rbt -b '~' -e '~' -r 'new text here' < /tmp/lorem_ipsum" \ + "${EXPECTED}" \ + "#12: Block on same lines (begin and end strings are equal)" + +rm -f /tmp/lorem_ipsum + +################################################################################################################ + +cat > /tmp/lorem_ipsum <<'EOF' +Lorem ipsum dolor sit amet, consectetur adipiscing elit. +~Pellentesque maximus faucibus lectus, in ultricies lorem volutpat in.~ Sed rutrum risus et vehicula rhoncus. Nunc sed est et eros mollis vehicula. Pellentesque semper dignissim maximus. ~Praesent in justo et ante faucibus eleifend in ac est. Donec orci magna, pellentesque id libero nec, faucibus porta purus. +Pellentesque luctus sollicitudin tortor sit amet accumsan. Nullam mauris felis, egestas in faucibus in, feugiat vel arcu.~ +Cras rhoncus aliquam tristique. +EOF + +read -r -d '' EXPECTED <<'EOF' +Lorem ipsum dolor sit amet, consectetur adipiscing elit. +~new text here~ Sed rutrum risus et vehicula rhoncus. Nunc sed est et eros mollis vehicula. Pellentesque semper dignissim maximus. ~new text here~ +Cras rhoncus aliquam tristique. +EOF + +assert_exec_equals \ + "cd .. ; ./bin/rbt -b '~' -e '~' -r 'new text here' < /tmp/lorem_ipsum" \ + "${EXPECTED}" \ + "#13: Block on different lines (begin and end strings are equal)" + +rm -f /tmp/lorem_ipsum + +################################################################################################################ + +cat > /tmp/lorem_ipsum <<'EOF' +texte sans rien +-- +texte avec un ~tag~ en plein milieu +-- +~tag~ au début +-- +la fin se termine par ~tag~ +-- +~tag~ +-- +la fin se termine par ~tag~ +-- +la fin se termine par ~tag +sur deux lignes~ +-- +la fin se termine par ~tag +sur deux lignes~ avec texte après +-- +~tag +sur deux lignes~ +-- +~tag +sur deux lignes~ avec texte après +-- +deux ~tag1~ et ~tag2~ +-- +deux ~tag1~ et ~tag sur +ligne suivante~ +-- +entre +~ +texte à +changer +~ +EOF + +read -r -d '' EXPECTED <<'EOF' +texte sans rien +-- +texte avec un ~/* tag */~ en plein milieu +-- +~/* tag */~ au début +-- +la fin se termine par ~/* tag */~ +-- +~/* tag */~ +-- +la fin se termine par ~/* tag */~ +-- +la fin se termine par ~/* tag +sur deux lignes */~ +-- +la fin se termine par ~/* tag +sur deux lignes */~ avec texte après +-- +~/* tag +sur deux lignes */~ +-- +~/* tag +sur deux lignes */~ avec texte après +-- +deux ~/* tag1 */~ et ~/* tag2 */~ +-- +deux ~/* tag1 */~ et ~/* tag sur +ligne suivante */~ +-- +entre +~ +/* texte à +changer */ +~ +EOF + +assert_exec_equals \ + "cd .. ; ./bin/rbt -b '~' -e '~' --comment java-block < /tmp/lorem_ipsum" \ + "${EXPECTED}" \ + "#14: Several tests with comment /* */ (begin and end strings are equal)" + +rm -f /tmp/lorem_ipsum echo "${ok}/${total} (${ko} errors)" From 903f89e0269300c6dffc85f38fe0d5ee448b3cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Giraud?= Date: Tue, 5 Dec 2023 14:27:25 +0100 Subject: [PATCH 5/6] comment --- bin/rbt | 17 ++------- tests/rbt_tests.sh | 91 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 91 insertions(+), 17 deletions(-) diff --git a/bin/rbt b/bin/rbt index f540e61..17e02b6 100755 --- a/bin/rbt +++ b/bin/rbt @@ -194,10 +194,10 @@ def readfile(filename, fdw): fdw(line) -def find_next(line, tags, use_re=False): +def find_next(line, begin_tag, end_tag): index_min = len(line) found = None - for tag in tags: + for tag in {begin_tag, end_tag}: index = line.find(tag) if index != -1 and index < index_min: index_min = index @@ -218,13 +218,12 @@ def apply_on_file(config, fileinfo): (fno, temporary_file) = tempfile.mkstemp() fd_out = open(fno, 'wt') move = True - tags = {config.begin_tag, config.end_tag} with fd_out: for line in fd_in: begin_of_line = True rest, le = chomp(line) while rest: - (next_tag, next_index) = find_next(rest, tags) + (next_tag, next_index) = find_next(rest, config.begin_tag, config.end_tag) # if (config.comment or config.uncomment) and next_index != 0: # text_inside.write(LINE_ENDING) if depth > 0 and begin_of_line and (config.comment or config.uncomment): @@ -252,7 +251,7 @@ def apply_on_file(config, fileinfo): if config.begin_tag != config.end_tag and next_tag == config.begin_tag: depth = depth + 1 if config.comment or config.uncomment: - text_inside.write("xx"+rest[:next_index]) + text_inside.write(rest[:next_index]) rest = rest[next_index + len(next_tag):] elif next_tag == config.end_tag: depth = depth - 1 @@ -344,16 +343,8 @@ class Config: self.style = a if o in ("-b", "--begin-tag"): self.begin_tag = a - self.use_re = False - if o in ("-B", "--begin-regexp-tag"): - self.begin_tag = a - self.use_re = True if o in ("-e", "--end-tag"): self.end_tag = a - self.use_re = False - if o in ("-E", "--end-regexp-tag"): - self.end_tag = a - self.use_re = True if o in ("-s", "--simulate"): self.simulate = True if o in ("-d", "--delete-tags"): diff --git a/tests/rbt_tests.sh b/tests/rbt_tests.sh index 2d24d62..8f6586a 100755 --- a/tests/rbt_tests.sh +++ b/tests/rbt_tests.sh @@ -143,7 +143,7 @@ EOF assert_exec_equals \ "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ "${EXPECTED}" \ - "#6: Two blocks on non consecutive lines. The second block is on two lines (force LF after begin)." + "#6: Two blocks on non consecutive lines. The second block is on two lines (force LF after begin)" ################################################################################################################ @@ -169,7 +169,7 @@ EOF assert_exec_equals \ "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ "${EXPECTED}" \ - "#7: Two blocks on non consecutive lines. The second block is on two lines (force LF after begin and before end)." + "#7: Two blocks on non consecutive lines. The second block is on two lines (force LF after begin and before end)" ################################################################################################################ @@ -193,7 +193,7 @@ EOF assert_exec_equals \ "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ "${EXPECTED}" \ - "#8: Two blocks on non consecutive lines. The second block is on two lines (force LF after begin)." + "#8: Two blocks on non consecutive lines. The second block is on two lines (force LF after begin)" ################################################################################################################ @@ -215,7 +215,7 @@ EOF assert_exec_equals \ "cd .. ; ./bin/rbt -b '*BEGIN*' -e '*END*' -r 'new text here' < /tmp/lorem_ipsum" \ "${EXPECTED}" \ - "#9: Two blocks with end of the first block/begin of the second block are on the same line." + "#9: Two blocks with end of the first block/begin of the second block are on the same line" ################################################################################################################ @@ -390,6 +390,89 @@ assert_exec_equals \ rm -f /tmp/lorem_ipsum +################################################################################################################ + +cat > /tmp/lorem_ipsum <<'EOF' +texte sans rien +-- +texte avec un ~tag~ en plein milieu +-- +~tag~ au début +-- +la fin se termine par ~tag~ +-- +~tag~ +-- +la fin se termine par ~tag~ +-- +la fin se termine par ~tag +sur deux lignes~ +-- +la fin se termine par ~tag +sur deux lignes~ avec texte après +-- +~tag +sur deux lignes~ +-- +~tag +sur deux lignes~ avec texte après +-- +deux ~tag1~ et ~tag2~ +-- +deux ~tag1~ et ~tag sur +ligne suivante~ +-- +entre +~ +texte à +changer +~ +EOF + +read -r -d '' EXPECTED <<'EOF' +texte sans rien +-- +texte avec un /* tag */ en plein milieu +-- +/* tag */ au début +-- +la fin se termine par /* tag */ +-- +/* tag */ +-- +la fin se termine par /* tag */ +-- +la fin se termine par /* tag +sur deux lignes */ +-- +la fin se termine par /* tag +sur deux lignes */ avec texte après +-- +/* tag +sur deux lignes */ +-- +/* tag +sur deux lignes */ avec texte après +-- +deux /* tag1 */ et /* tag2 */ +-- +deux /* tag1 */ et /* tag sur +ligne suivante */ +-- +entre + +/* texte à +changer */ + +EOF + +assert_exec_equals \ + "cd .. ; ./bin/rbt -b '~' -e '~' --comment java-block -d < /tmp/lorem_ipsum" \ + "${EXPECTED}" \ + "#15: Several tests with comment /* */ (begin and end strings are equal), begin/end strings are deleted" + +rm -f /tmp/lorem_ipsum + echo "${ok}/${total} (${ko} errors)" #rm -f /tmp/lorem_ipsum From bd72c1319503ca75d26be6449f30ce4001e769b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Giraud?= Date: Wed, 6 Dec 2023 16:02:29 +0100 Subject: [PATCH 6/6] comment --- bin/rbt | 68 ++++++++++++++++++------------------- doc/generated/md/rbt.md | 4 +-- doc/generated/txt/rbt.1.txt | 4 +-- doc/rbt.adoc | 14 ++++---- tests/rbt_tests.sh | 2 ++ 5 files changed, 47 insertions(+), 45 deletions(-) diff --git a/bin/rbt b/bin/rbt index 17e02b6..2819890 100755 --- a/bin/rbt +++ b/bin/rbt @@ -28,10 +28,10 @@ DESCRIPTION With no FILE, or when FILE is -, read standard input. OPTIONS - -b string, --begin-tag string + -b string, --begin-string string The begin tag to search. - -e string, --end-tag string + -e string, --end-string string The end tag to search. -r string, --replace string @@ -194,15 +194,15 @@ def readfile(filename, fdw): fdw(line) -def find_next(line, begin_tag, end_tag): +def find_next(line, begin_string, end_string): index_min = len(line) found = None - for tag in {begin_tag, end_tag}: - index = line.find(tag) + for pattern in {begin_string, end_string}: + index = line.find(pattern) if index != -1 and index < index_min: index_min = index - found = tag - return (found, index_min) if found else (None, -1) + found = pattern + return (found, index_min, found) if found else (None, -1, None) def apply_on_file(config, fileinfo): @@ -223,7 +223,8 @@ def apply_on_file(config, fileinfo): begin_of_line = True rest, le = chomp(line) while rest: - (next_tag, next_index) = find_next(rest, config.begin_tag, config.end_tag) + (next_matching_string, next_index, matching_pattern) = find_next(rest, config.begin_string, config.end_string) + # print("->%s", str((next_matching_string, next_index, matching_pattern)), file=sys.stderr) # if (config.comment or config.uncomment) and next_index != 0: # text_inside.write(LINE_ENDING) if depth > 0 and begin_of_line and (config.comment or config.uncomment): @@ -231,38 +232,38 @@ def apply_on_file(config, fileinfo): if next_index != 0: begin_of_line = False if depth == 0: - if not next_tag: + if not next_matching_string: fd_out.write(rest) break - if config.begin_tag != config.end_tag and next_tag == config.end_tag: + if config.begin_string != config.end_string and matching_pattern == config.end_string: error("reach end tag before start tag") - if next_tag == config.begin_tag: + if matching_pattern == config.begin_string: fd_out.write(rest[:next_index]) if not config.delete_tags: - fd_out.write(next_tag) - rest = rest[next_index + len(next_tag):] + fd_out.write(next_matching_string) + rest = rest[next_index + len(next_matching_string):] # if the opening tag is followed by crlf, insert it if len(rest) == 0 and le is not None: fd_out.write(LINE_ENDING) depth = depth + 1 written = False else: - if next_tag: - if config.begin_tag != config.end_tag and next_tag == config.begin_tag: + if next_matching_string: + if config.begin_string != config.end_string and matching_pattern == config.begin_string: depth = depth + 1 if config.comment or config.uncomment: text_inside.write(rest[:next_index]) - rest = rest[next_index + len(next_tag):] - elif next_tag == config.end_tag: + rest = rest[next_index + len(next_matching_string):] + elif matching_pattern == config.end_string: depth = depth - 1 if config.comment or config.uncomment: text_inside.write(rest[:next_index]) - rest = rest[next_index + len(next_tag):] + rest = rest[next_index + len(next_matching_string):] if depth < 0: error("reach end tag without corresponding begin tag") if depth == 0: if not written: - # if begin_of_line: #config.new_line_before_end_tag: + # if begin_of_line: #config.new_line_before_end_string: # fd_out.write(config.line_ending) #print("-- %s" % text_inside.getvalue(), file=sys.stderr) if config.comment: @@ -276,11 +277,11 @@ def apply_on_file(config, fileinfo): if begin_of_line: fd_out.write(LINE_ENDING) if not config.delete_tags: - fd_out.write(next_tag) + fd_out.write(next_matching_string) written = True text_inside = io.StringIO() else: - error("case not supported for tag '%s'" % next_tag) + error("case not supported for tag '%s'" % next_matching_string) else: if config.comment or config.uncomment: text_inside.write(rest) @@ -304,8 +305,8 @@ def apply_replacements(config, files): class Config: def __init__(self): - self.begin_tag = None - self.end_tag = None + self.begin_string = None + self.end_string = None self.replace = None self.simulate = False self.comment = False @@ -316,9 +317,8 @@ class Config: def parse(self, arguments): opts, args = [], [] try: - opts, args = getopt.getopt(arguments, "hvsdb:B:e:E:r:R:c:u:DU", - ["help", "version", "simulate", "delete-tags", "begin-tag=", "begin-regexp-tag=", "end-tag=", - "end-regexp-tag=", + opts, args = getopt.getopt(arguments, "hvsdb:e:r:R:c:u:DU", + ["help", "version", "simulate", "delete-tags", "begin-string=", "end-string=", "replace=", "replace-file=", "comment=", "uncomment=", "dos", "unix"]) @@ -341,10 +341,10 @@ class Config: if o in ("-u", "--uncomment"): self.uncomment = True self.style = a - if o in ("-b", "--begin-tag"): - self.begin_tag = a - if o in ("-e", "--end-tag"): - self.end_tag = a + if o in ("-b", "--begin-string"): + self.begin_string = a + if o in ("-e", "--end-string"): + self.end_string = a if o in ("-s", "--simulate"): self.simulate = True if o in ("-d", "--delete-tags"): @@ -367,10 +367,10 @@ class Config: return self.files def validate(self): - if not self.begin_tag: - error("option --begin-tag or --begin-regexp-tag is required") - if not self.end_tag: - error("option --end-tag or --end-regexp-tag is required") + if not self.begin_string: + error("option --begin-string is required") + if not self.end_string: + error("option --end-string is required") if self.comment and self.uncomment: error("options --comment and --uncomment are exclusives") if (self.comment or self.uncomment) and self.replace: diff --git a/doc/generated/md/rbt.md b/doc/generated/md/rbt.md index 6c352e8..9d6df89 100644 --- a/doc/generated/md/rbt.md +++ b/doc/generated/md/rbt.md @@ -19,10 +19,10 @@ With no FILE, or when FILE is `-`, read standard input. OPTIONS ======= -**-b** *string*, **--begin-tag** *string* +**-b** *string*, **--begin-string** *string* The begin tag to search. -**-e** *string*, **--end-tag** *string* +**-e** *string*, **--end-string** *string* The end tag to search. **-r** *string*, **--replace** *string* diff --git a/doc/generated/txt/rbt.1.txt b/doc/generated/txt/rbt.1.txt index 73b2f3d..19ae05e 100644 --- a/doc/generated/txt/rbt.1.txt +++ b/doc/generated/txt/rbt.1.txt @@ -12,10 +12,10 @@ DESCRIPTION With no FILE, or when FILE is -, read standard input. OPTIONS - -b string, --begin-tag string + -b string, --begin-string string The begin tag to search. - -e string, --end-tag string + -e string, --end-string string The end tag to search. -r string, --replace string diff --git a/doc/rbt.adoc b/doc/rbt.adoc index 1994178..0882be0 100644 --- a/doc/rbt.adoc +++ b/doc/rbt.adoc @@ -24,17 +24,17 @@ With no FILE, or when FILE is `-`, read standard input. == OPTIONS // tag::options[] -*-b* _string_, *--begin-tag* _string_:: -The begin tag to search. +*-b* _string_, *--begin-string* _string_:: +The begin string to search. *-d*, *--delete*:: -Delete begin/end tags after replacing. +Delete begin/end strings after replacing. *-D*, *--dos*:: Use Dos/Windows line ending characters. -*-e* _string_, *--end-tag* _string_:: -The end tag to search. +*-e* _string_, *--end-string* _string_:: +The end string to search. *-h*:: Display help. @@ -58,7 +58,7 @@ Display version. == EXAMPLES // tag::examples[] -.Replace between tags using stdout (`-s` option) +.Replace between strings using stdout (`-s` option) [source,shell] ---- $ cat /tmp/lorem_ipsum @@ -72,7 +72,7 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit. *BEGIN*new text here *END*Cras rhoncus aliquam tristique. ---- -.Replace infile between tags and delete begin/end tags (`-d` option) +.Replace infile between strings and delete begin/end strings (`-d` option) [source,shell] ---- $ cat /tmp/lorem_ipsum diff --git a/tests/rbt_tests.sh b/tests/rbt_tests.sh index 8f6586a..a122037 100755 --- a/tests/rbt_tests.sh +++ b/tests/rbt_tests.sh @@ -473,6 +473,8 @@ assert_exec_equals \ rm -f /tmp/lorem_ipsum +################################################################################################################ + echo "${ok}/${total} (${ko} errors)" #rm -f /tmp/lorem_ipsum