aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--qtip/ansible_library/plugins/__init__.py0
-rw-r--r--qtip/ansible_library/plugins/action/__init__.py0
-rw-r--r--qtip/ansible_library/plugins/action/collect.py45
-rw-r--r--tests/unit/ansible_library/plugins/action/collect_test.py36
4 files changed, 81 insertions, 0 deletions
diff --git a/qtip/ansible_library/plugins/__init__.py b/qtip/ansible_library/plugins/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/qtip/ansible_library/plugins/__init__.py
diff --git a/qtip/ansible_library/plugins/action/__init__.py b/qtip/ansible_library/plugins/action/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/qtip/ansible_library/plugins/action/__init__.py
diff --git a/qtip/ansible_library/plugins/action/collect.py b/qtip/ansible_library/plugins/action/collect.py
new file mode 100644
index 00000000..88ad0e35
--- /dev/null
+++ b/qtip/ansible_library/plugins/action/collect.py
@@ -0,0 +1,45 @@
+#!/usr/bin/python
+
+###############################################################
+# Copyright (c) 2017 ZTE Corporation
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+from collections import defaultdict
+import re
+
+from ansible.plugins.action import ActionBase
+
+
+class ActionModule(ActionBase):
+
+ def run(self, tmp=None, task_vars=None):
+ result = super(ActionModule, self).run(tmp, task_vars)
+
+ if result.get('skipped', False):
+ return result
+
+ string = self._task.args.get('string')
+ patterns = self._task.args.get('patterns')
+
+ return collect(patterns, string)
+
+
+def collect(patterns, string):
+ """collect all named subgroups of the match into a list keyed by subgroup name
+ """
+ captured = defaultdict(list)
+
+ if not isinstance(patterns, list):
+ patterns = [patterns]
+
+ for p in patterns:
+ for match_obj in re.finditer(p, string, re.MULTILINE):
+ for (key, value) in match_obj.groupdict().items():
+ captured[key].append(value)
+
+ return captured
diff --git a/tests/unit/ansible_library/plugins/action/collect_test.py b/tests/unit/ansible_library/plugins/action/collect_test.py
new file mode 100644
index 00000000..5a9fc8f5
--- /dev/null
+++ b/tests/unit/ansible_library/plugins/action/collect_test.py
@@ -0,0 +1,36 @@
+##############################################################################
+# Copyright (c) 2017 ZTE Corp and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+import pytest
+
+from qtip.ansible_library.plugins.action import collect
+
+
+@pytest.fixture
+def string():
+ return """Lorem ipsum dolor sit amet,
+consectetur adipiscing elit,
+sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
+
+Ut enim ad minim veniam,
+quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
+
+Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
+Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+"""
+
+
+@pytest.mark.parametrize("patterns,expected", [
+ ('not exist', {}),
+ ('Lorem (\S+)', {}),
+ ('nisi ut (?P<name>\S+)', {'name': ['aliquip']}),
+ ('in\s(?P<in>\w+)', {'in': ['reprehenderit', 'voluptate', 'culpa']})
+])
+def test_collect(patterns, string, expected):
+ assert collect.collect(patterns, string) == expected