From 3024febec085e38d567a8f14aa98f5d6ee508e2e Mon Sep 17 00:00:00 2001 From: Klaus Zerwes Date: Thu, 17 Nov 2022 14:50:52 +0100 Subject: [PATCH 1/2] issue #12 : introduced general_exclude_blocks (used only for vars: for now) --- fqcn-fixer.py | 152 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 93 insertions(+), 59 deletions(-) diff --git a/fqcn-fixer.py b/fqcn-fixer.py index 9faa0cd..a9dcb28 100755 --- a/fqcn-fixer.py +++ b/fqcn-fixer.py @@ -47,6 +47,13 @@ def checkignoreregex(checkline): return True return False +def checkstartexcludeblock(checkline): + """check if line starts a exclude block""" + for exre in _general_exclude_blocks_regex: + if exre.match(checkline): + return True + return False + class Dumper(yaml.Dumper): # pylint: disable=too-many-ancestors """https://github.com/yaml/pyyaml/issues/234""" def increase_indent(self, flow=False, *dargs, **dkwargs): # pylint: disable=keyword-arg-before-vararg,unused-argument @@ -83,6 +90,11 @@ def increase_indent(self, flow=False, *dargs, **dkwargs): # pylint: disable=keyw re.compile(r'\s*-\srole:\s*\w+'), ] +# list of starting regex to exclude complete subsequent blocks +_general_exclude_blocks_regex = [ + re.compile(r'\s*-*\s*vars:$'), +] + required_fqcnconverter_file_version = '0.1.2' argparser = argparse.ArgumentParser(description=__doc__) @@ -320,79 +332,101 @@ def increase_indent(self, flow=False, *dargs, **dkwargs): # pylint: disable=keyw startingwhitespaces4comments = 0 in_task = False in_task_done = False + in_ignore_block = False + startingwhitespaces_ignore_block = None fqcnregex = _fqcnregex for line in fi: if args.debug: debugmsg( - 'STARTLINE : line: %s in_task: %s in_task_done: %s\n' % + '\n\n\nSTARTLINE : line: %s in_task: %s in_task_done: %s\n' % (line, in_task, in_task_done,) ) if args.printdiff: originallines.append(line) nline = line - taskmatch = _taskstartregex.match(line) - if taskmatch: - in_task_done = False - in_task = False - if in_task_done and not taskmatch: + if checkstartexcludeblock(line): + in_ignore_block = True + startingwhitespaces_ignore_block = re.match(r'\s*-?\s*', line).group() if args.debug: - debugmsg('SKIPLINE! %s\n' % (in_task_done and not in_task)) - print('.', file=sys.stderr, end='', flush=True) - else: - if not in_task: - if args.debug: - debugmsg('TASKMATCH : line: %s taskmatch: %s\n' % (line, taskmatch,)) - if taskmatch: - in_task = True - in_task_done = False - fqcnregex = _fqcnregex - startingwhitespaces = r'\s*-?\s+' - startingwhitespacesaftertask = len(taskmatch.group('white')) - if args.debug: - debugmsg('line: %s taskmatch: %s' % (line, taskmatch,)) - debugmsg( - 'startingwhitespaces "%s" startingwhitespacesaftertask "%s"' % - (startingwhitespaces, startingwhitespacesaftertask,) - ) - fqcnmatch = fqcnregex.match(line) - if args.debug: - debugmsg('FQCNMATCH : line: %s fqcnmatch: %s\n' % (line, fqcnmatch,)) - debugmsg('fqcnregex: %s' % fqcnregex) - if fqcnmatch and not checkignoreregex(line): - in_task_done = True - in_task = False - fqcnmodule = fqcnmatch.group('module') - nline = re.sub( - '^(%s)%s:' % (startingwhitespaces, fqcnmodule), - '\\1%s:' % fqcndict[fqcnmodule][0], - line + debugmsg( + 'start exclude block : startingwhitespaces_ignore_block : "%s"' % + startingwhitespaces_ignore_block ) - if fqcnmodule == fqcndict[fqcnmodule][0]: - print('.', file=sys.stderr, end='', flush=True) - else: - print('*', file=sys.stderr, end='', flush=True) - if len(fqcndict[fqcnmodule]) > 1: - wtxt = ('possible ambiguous replacement: %s : %s' % - (fqcnmodule, ' | '.join(fqcndict[fqcnmodule]))) - warnings.append(wtxt) - if args.writewarnings: - if args.writefiles: - print('%s# %s' % (' '*startingwhitespaces4comments, wtxt)) - if args.printdiff: - changedlines.append( - '%s# %s\n' % (' '*startingwhitespaces4comments, wtxt) - ) + elif in_ignore_block: + if re.match(r'%s\w+' % startingwhitespaces_ignore_block, line): + in_ignore_block = False + startingwhitespaces_ignore_block = None + if args.debug: + debugmsg('end exclude block!') else: + if args.debug: + debugmsg('... in exclude block ... ignore line') + if not in_ignore_block: + taskmatch = _taskstartregex.match(line) + if taskmatch: + if args.debug: + debugmsg('taskmatch: %s' % taskmatch) + in_task_done = False + in_task = False + if in_task_done and not taskmatch: + if args.debug: + debugmsg('SKIPLINE! %s\n' % (in_task_done and not in_task)) print('.', file=sys.stderr, end='', flush=True) - if startingwhitespacesaftertask > 0: - startingwhitespaces = ' ' * startingwhitespacesaftertask - startingwhitespaces4comments = startingwhitespacesaftertask - startingwhitespacesaftertask = 0 - fqcnregex = re.compile('^%s(?P%s):' % - (startingwhitespaces, '|'.join(fqcndict.keys())) - ) + else: + if not in_task: if args.debug: - debugmsg('set STARTINGWHITESPACES to "%s"' % startingwhitespaces) + debugmsg('TASKMATCH : line: %s taskmatch: %s\n' % (line, taskmatch,)) + if taskmatch: + in_task = True + in_task_done = False + fqcnregex = _fqcnregex + startingwhitespaces = r'\s*-?\s+' + startingwhitespacesaftertask = len(taskmatch.group('white')) + if args.debug: + debugmsg('line: %s taskmatch: %s' % (line, taskmatch,)) + debugmsg( + 'startingwhitespaces "%s" startingwhitespacesaftertask "%s"' % + (startingwhitespaces, startingwhitespacesaftertask,) + ) + fqcnmatch = fqcnregex.match(line) + if args.debug: + debugmsg('FQCNMATCH : line: %s fqcnmatch: %s\n' % (line, fqcnmatch,)) + debugmsg('fqcnregex: %s' % fqcnregex) + if fqcnmatch and not checkignoreregex(line): + in_task_done = True + in_task = False + fqcnmodule = fqcnmatch.group('module') + nline = re.sub( + '^(%s)%s:' % (startingwhitespaces, fqcnmodule), + '\\1%s:' % fqcndict[fqcnmodule][0], + line + ) + if fqcnmodule == fqcndict[fqcnmodule][0]: + print('.', file=sys.stderr, end='', flush=True) + else: + print('*', file=sys.stderr, end='', flush=True) + if len(fqcndict[fqcnmodule]) > 1: + wtxt = ('possible ambiguous replacement: %s : %s' % + (fqcnmodule, ' | '.join(fqcndict[fqcnmodule]))) + warnings.append(wtxt) + if args.writewarnings: + if args.writefiles: + print('%s# %s' % (' '*startingwhitespaces4comments, wtxt)) + if args.printdiff: + changedlines.append( + '%s# %s\n' % (' '*startingwhitespaces4comments, wtxt) + ) + else: + print('.', file=sys.stderr, end='', flush=True) + if startingwhitespacesaftertask > 0: + startingwhitespaces = ' ' * startingwhitespacesaftertask + startingwhitespaces4comments = startingwhitespacesaftertask + startingwhitespacesaftertask = 0 + fqcnregex = re.compile('^%s(?P%s):' % + (startingwhitespaces, '|'.join(fqcndict.keys())) + ) + if args.debug: + debugmsg('set STARTINGWHITESPACES to "%s"' % startingwhitespaces) if args.writefiles: print(nline, end='') From 22550339a4c7f2850757a22dd31f1196b8cc4139 Mon Sep 17 00:00:00 2001 From: Klaus Zerwes Date: Thu, 17 Nov 2022 14:54:57 +0100 Subject: [PATCH 2/2] issue #12 : added vars block to the test file --- example.yml | 5 +++++ exampleconverted.yml | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/example.yml b/example.yml index 6007c85..2cb5efc 100644 --- a/example.yml +++ b/example.yml @@ -2,6 +2,11 @@ - name: ansible fqcn test playbook hosts: localhost gather_facts: true + vars: + xyz: abc + num: 1 + deploy: true + domain: example.com tasks: - name: test task 1 command: cat dog diff --git a/exampleconverted.yml b/exampleconverted.yml index 526a226..1a6f04b 100644 --- a/exampleconverted.yml +++ b/exampleconverted.yml @@ -2,6 +2,11 @@ - name: ansible fqcn test playbook hosts: localhost gather_facts: true + vars: + xyz: abc + num: 1 + deploy: true + domain: example.com tasks: - name: test task 1 # possible ambiguous replacement: command : ansible.builtin.command | community.ciscosmb.command | community.routeros.command