Debian Codegraph @

lib/ansible/module_utils/json_utils.py

raw
1 # This code is part of Ansible, but is an independent component.
2 # This particular file snippet, and this file snippet only, is BSD licensed.
3 # Modules you write using this snippet, which is embedded dynamically by Ansible
4 # still belong to the author of the module, and may assign their own license
5 # to the complete work.
6 #
7 # Redistribution and use in source and binary forms, with or without modification,
8 # are permitted provided that the following conditions are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
24 # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #
26
27 from __future__ import annotations
28
29 import json # pylint: disable=unused-import
30
31
32 # NB: a copy of this function exists in ../../modules/core/async_wrapper.py. Ensure any
33 # changes are propagated there.
34 def _filter_non_json_lines(data, objects_only=False):
35 """
36 Used to filter unrelated output around module JSON output, like messages from
37 tcagetattr, or where dropbear spews MOTD on every single command (which is nuts).
38
39 Filters leading lines before first line-starting occurrence of '{' or '[', and filter all
40 trailing lines after matching close character (working from the bottom of output).
41 """
42 warnings = []
43
44 # Filter initial junk
45 lines = data.splitlines()
46
47 for start, line in enumerate(lines):
48 line = line.strip()
49 if line.startswith(u'{'):
50 endchar = u'}'
51 break
52 elif not objects_only and line.startswith(u'['):
53 endchar = u']'
54 break
55 else:
56 raise ValueError('No start of json char found')
57
58 # Filter trailing junk
59 lines = lines[start:]
60
61 for reverse_end_offset, line in enumerate(reversed(lines)):
62 if line.strip().endswith(endchar):
63 break
64 else:
65 raise ValueError('No end of json char found')
66
67 if reverse_end_offset > 0:
68 # Trailing junk is uncommon and can point to things the user might
69 # want to change. So print a warning if we find any
70 trailing_junk = lines[len(lines) - reverse_end_offset:]
71 for line in trailing_junk:
72 if line.strip():
73 warnings.append('Module invocation had junk after the JSON data: %s' % '\n'.join(trailing_junk))
74 break
75
76 lines = lines[:(len(lines) - reverse_end_offset)]
77
78 return ('\n'.join(lines), warnings)