From a6e6bc4c710b67f8333c7359b5167067751a7cea Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 8 Nov 2018 01:39:19 +0000 Subject: [PATCH] issue #426: to_text filter. --- tests/ansible/ansible.cfg | 1 + tests/ansible/lib/filters/mitogen_tests.py | 27 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/ansible/lib/filters/mitogen_tests.py diff --git a/tests/ansible/ansible.cfg b/tests/ansible/ansible.cfg index 9c25732c..1406d2c4 100644 --- a/tests/ansible/ansible.cfg +++ b/tests/ansible/ansible.cfg @@ -7,6 +7,7 @@ callback_plugins = lib/callback stdout_callback = nice_stdout vars_plugins = lib/vars library = lib/modules +filter_plugins = lib/filters module_utils = lib/module_utils retry_files_enabled = False display_args_to_stdout = True diff --git a/tests/ansible/lib/filters/mitogen_tests.py b/tests/ansible/lib/filters/mitogen_tests.py new file mode 100644 index 00000000..e7614658 --- /dev/null +++ b/tests/ansible/lib/filters/mitogen_tests.py @@ -0,0 +1,27 @@ + +from ansible.module_utils._text import to_text + + +try: + Unicode = unicode +except: + Unicode = str + + +def to_text(s): + """ + Ensure the str or unicode `s` is unicode, and strip away any subclass. Also + works on lists. + """ + if isinstance(s, list): + return [to_text(ss) for ss in s] + if not isinstance(s, Unicode): + s = to_text(s) + return Unicode(s) + + +class FilterModule(object): + def filters(self): + return { + 'to_text': to_text, + }