summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYujun Zhang <zhang.yujunz@zte.com.cn>2017-04-25 14:08:01 +0800
committerYujun Zhang <zhang.yujunz@zte.com.cn>2017-04-25 16:58:31 +0800
commit2189e3bfdf1d250fa1201f89249745d4d6aab7f0 (patch)
tree3a5ab98cdd7632bd80c9181001a635c72a252eef
parent6463b6635a8094a0ddcf83605cd231ba46104f72 (diff)
Rewrite `export_to_file` as decorator
Change-Id: Ibf852038fac986801a3fc2c608a0ddb1572abbde Signed-off-by: Yujun Zhang <zhang.yujunz@zte.com.cn>
-rw-r--r--qtip/ansible_library/plugins/action/calculate.py10
-rw-r--r--qtip/ansible_library/plugins/action/collect.py10
-rw-r--r--qtip/util/export_to.py11
3 files changed, 16 insertions, 15 deletions
diff --git a/qtip/ansible_library/plugins/action/calculate.py b/qtip/ansible_library/plugins/action/calculate.py
index 01c80a02..ee3b8e3a 100644
--- a/qtip/ansible_library/plugins/action/calculate.py
+++ b/qtip/ansible_library/plugins/action/calculate.py
@@ -38,12 +38,13 @@ class ActionModule(ActionBase):
spec = yaml.safe_load(stream)
metrics = self._task.args.get('metrics')
- export_to = self._task.args.get('export_to')
+ dest = self._task.args.get('dest')
- return calc_qpi(spec, metrics, export_to)
+ return calc_qpi(spec, metrics, dest=dest)
-def calc_qpi(qpi_spec, metrics, dest=None):
+@export_to_file
+def calc_qpi(qpi_spec, metrics):
display.vv("calculate QPI {}".format(qpi_spec['name']))
display.vvv("spec: {}".format(qpi_spec))
@@ -63,9 +64,6 @@ def calc_qpi(qpi_spec, metrics, dest=None):
'metrics': metrics
}
- if dest is not None:
- export_to_file(results, dest)
-
return results
diff --git a/qtip/ansible_library/plugins/action/collect.py b/qtip/ansible_library/plugins/action/collect.py
index c54cbe7d..9d3b2a73 100644
--- a/qtip/ansible_library/plugins/action/collect.py
+++ b/qtip/ansible_library/plugins/action/collect.py
@@ -27,12 +27,13 @@ class ActionModule(ActionBase):
string = self._task.args.get('string')
patterns = self._task.args.get('patterns')
- export_to = self._task.args.get('export_to')
+ dest = self._task.args.get('dest')
- return collect(patterns, string, export_to)
+ return collect(patterns, string, dest=dest)
-def collect(patterns, string, dest=None):
+@export_to_file
+def collect(patterns, string):
"""collect all named subgroups of the match into a list keyed by subgroup name
"""
captured = defaultdict(list)
@@ -45,7 +46,4 @@ def collect(patterns, string, dest=None):
for (key, value) in match_obj.groupdict().items():
captured[key].append(value)
- if dest is not None:
- export_to_file(captured, dest)
-
return captured
diff --git a/qtip/util/export_to.py b/qtip/util/export_to.py
index 98c39b5f..17adae7c 100644
--- a/qtip/util/export_to.py
+++ b/qtip/util/export_to.py
@@ -10,6 +10,11 @@
import json
-def export_to_file(content, filename):
- with open(filename, 'w+') as f:
- f.write(json.dumps(content, indent=2))
+def export_to_file(func):
+ def func_wrapper(spec, metrics, dest=None):
+ content = func(spec, metrics)
+ if dest is not None:
+ with open(dest, 'w+') as f:
+ f.write(json.dumps(content, indent=2))
+ return content
+ return func_wrapper