|
| 1 | +# Copyright © 2011-2023 Splunk, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +"""The **splunklib.utils** File for utility functions. |
| 16 | +""" |
| 17 | + |
| 18 | + |
| 19 | +def ensure_binary(s, encoding='utf-8', errors='strict'): |
| 20 | + """ |
| 21 | + - `str` -> encoded to `bytes` |
| 22 | + - `bytes` -> `bytes` |
| 23 | + """ |
| 24 | + if isinstance(s, str): |
| 25 | + return s.encode(encoding, errors) |
| 26 | + |
| 27 | + if isinstance(s, bytes): |
| 28 | + return s |
| 29 | + |
| 30 | + raise TypeError(f"not expecting type '{type(s)}'") |
| 31 | + |
| 32 | + |
| 33 | +def ensure_str(s, encoding='utf-8', errors='strict'): |
| 34 | + """ |
| 35 | + - `str` -> `str` |
| 36 | + - `bytes` -> decoded to `str` |
| 37 | + """ |
| 38 | + if isinstance(s, bytes): |
| 39 | + return s.decode(encoding, errors) |
| 40 | + |
| 41 | + if isinstance(s, str): |
| 42 | + return s |
| 43 | + |
| 44 | + raise TypeError(f"not expecting type '{type(s)}'") |
| 45 | + |
| 46 | + |
| 47 | +def ensure_text(s, encoding='utf-8', errors='strict'): |
| 48 | + """ |
| 49 | + - `str` -> `str` |
| 50 | + - `bytes` -> decoded to `str` |
| 51 | + """ |
| 52 | + if isinstance(s, bytes): |
| 53 | + return s.decode(encoding, errors) |
| 54 | + if isinstance(s, str): |
| 55 | + return s |
| 56 | + raise TypeError(f"not expecting type '{type(s)}'") |
| 57 | + |
| 58 | + |
| 59 | +def assertRegex(self, *args, **kwargs): |
| 60 | + return getattr(self, "assertRegex")(*args, **kwargs) |
0 commit comments