Skip to content

Commit f40d2e8

Browse files
committed
Merge branch 'py3-code-migration' of https://github.com/splunk/splunk-sdk-python into py3-code-migration
2 parents d8d7da4 + 21323ea commit f40d2e8

File tree

7 files changed

+72
-53
lines changed

7 files changed

+72
-53
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Version 2.0.0-beta
44

55
### Feature updates
6-
* `ensure_binary`, `ensure_str`, `ensure_text` and `assert_regex` utility methods have been migrated from `six.py` to `splunklib/__init__.py`
6+
* `ensure_binary`, `ensure_str`, `ensure_text` and `assert_regex` utility methods have been migrated from `six.py` to `splunklib/utils.py`
77

88
### Major changes
99
* Removed Code specific to Python2

splunklib/__init__.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,49 +30,5 @@ def setup_logging(level, log_format=DEFAULT_LOG_FORMAT, date_format=DEFAULT_DATE
3030
datefmt=date_format)
3131

3232

33-
def ensure_binary(s, encoding='utf-8', errors='strict'):
34-
"""
35-
- `str` -> encoded to `bytes`
36-
- `bytes` -> `bytes`
37-
"""
38-
if isinstance(s, str):
39-
return s.encode(encoding, errors)
40-
41-
if isinstance(s, bytes):
42-
return s
43-
44-
raise TypeError(f"not expecting type '{type(s)}'")
45-
46-
47-
def ensure_str(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-
55-
if isinstance(s, str):
56-
return s
57-
58-
raise TypeError(f"not expecting type '{type(s)}'")
59-
60-
61-
def ensure_text(s, encoding='utf-8', errors='strict'):
62-
"""
63-
- `str` -> `str`
64-
- `bytes` -> decoded to `str`
65-
"""
66-
if isinstance(s, bytes):
67-
return s.decode(encoding, errors)
68-
if isinstance(s, str):
69-
return s
70-
raise TypeError(f"not expecting type '{type(s)}'")
71-
72-
73-
def assertRegex(self, *args, **kwargs):
74-
return getattr(self, "assertRegex")(*args, **kwargs)
75-
76-
7733
__version_info__ = (2, 0, 0)
7834
__version__ = ".".join(map(str, __version_info__))

splunklib/modularinput/event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from io import TextIOBase
1616
import xml.etree.ElementTree as ET
1717

18-
from splunklib import ensure_text
18+
from splunklib.utils import ensure_text
1919

2020

2121
class Event:

splunklib/modularinput/event_writer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import sys
1616

17-
from splunklib import ensure_str
17+
from splunklib.utils import ensure_str
1818
from .event import ET
1919

2020

splunklib/searchcommands/search_command.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
from urllib.parse import urlsplit
3535
from warnings import warn
3636
from xml.etree import ElementTree
37+
from splunklib.utils import ensure_str
38+
3739

3840
# Relative imports
3941
import splunklib
@@ -888,7 +890,7 @@ def _read_chunk(istream):
888890
if not header:
889891
return None
890892

891-
match = SearchCommand._header.match(splunklib.ensure_str(header))
893+
match = SearchCommand._header.match(ensure_str(header))
892894

893895
if match is None:
894896
raise RuntimeError(f'Failed to parse transport header: {header}')
@@ -905,7 +907,7 @@ def _read_chunk(istream):
905907
decoder = MetadataDecoder()
906908

907909
try:
908-
metadata = decoder.decode(splunklib.ensure_str(metadata))
910+
metadata = decoder.decode(ensure_str(metadata))
909911
except Exception as error:
910912
raise RuntimeError(f'Failed to parse metadata of length {metadata_length}: {error}')
911913

@@ -919,7 +921,7 @@ def _read_chunk(istream):
919921
except Exception as error:
920922
raise RuntimeError(f'Failed to read body of length {body_length}: {error}')
921923

922-
return metadata, splunklib.ensure_str(body,errors="replace")
924+
return metadata, ensure_str(body,errors="replace")
923925

924926
_header = re.compile(r'chunked\s+1.0\s*,\s*(\d+)\s*,\s*(\d+)\s*\n')
925927

splunklib/utils.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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)

tests/searchcommands/test_search_command.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@
3232
from splunklib.searchcommands.decorators import ConfigurationSetting, Option
3333
from splunklib.searchcommands.search_command import SearchCommand
3434
from splunklib.client import Service
35+
from splunklib.utils import ensure_binary
3536

3637
from io import StringIO, BytesIO
3738

3839

3940
def build_command_input(getinfo_metadata, execute_metadata, execute_body):
40-
input = (f'chunked 1.0,{len(splunklib.ensure_binary(getinfo_metadata))},0\n{getinfo_metadata}' +
41-
f'chunked 1.0,{len(splunklib.ensure_binary(execute_metadata))},{len(splunklib.ensure_binary(execute_body))}\n{execute_metadata}{execute_body}')
41+
input = (f'chunked 1.0,{len(ensure_binary(getinfo_metadata))},0\n{getinfo_metadata}' +
42+
f'chunked 1.0,{len(ensure_binary(execute_metadata))},{len(ensure_binary(execute_body))}\n{execute_metadata}{execute_body}')
4243

43-
ifile = BytesIO(splunklib.ensure_binary(input))
44+
ifile = BytesIO(ensure_binary(input))
4445

4546
ifile = TextIOWrapper(ifile)
4647

0 commit comments

Comments
 (0)