utils.py
Common utilities.
__all__ = ['handle_teardown_signals', 'datetime_to_seconds', 'is_true', 'is_false', 'retry', 'extract_http_scheme_host_port', 'remove_http_proxy_env_vars']
module-attribute
¶
datetime_to_seconds(dt)
¶
Convert UTC datetime to seconds since epoch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dt |
datetime.datetime
|
Date time. |
required |
Returns:
Type | Description |
---|---|
float
|
Seconds since epoch. |
Source code in solnlib/utils.py
82 83 84 85 86 87 88 89 90 91 92 93 |
|
extract_http_scheme_host_port(http_url)
¶
Extract scheme, host and port from a HTTP URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
http_url |
str
|
HTTP URL to extract. |
required |
Returns:
Type | Description |
---|---|
Tuple
|
A tuple of scheme, host and port |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in solnlib/utils.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
handle_teardown_signals(callback)
¶
Register handler for SIGTERM/SIGINT/SIGBREAK signal.
Catch SIGTERM/SIGINT/SIGBREAK signals, and invoke callback Note: this should be called in main thread since Python only catches signals in main thread.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable
|
Callback for tear down signals. |
required |
Source code in solnlib/utils.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
is_false(val)
¶
Decide if val
is false.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
Union[str, int]
|
Value to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True or False. |
Source code in solnlib/utils.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
is_true(val)
¶
Decide if val
is true.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
Union[str, int]
|
Value to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True or False. |
Source code in solnlib/utils.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
remove_http_proxy_env_vars()
¶
Removes HTTP(s) proxies from environment variables.
Removes the following environment variables
- http_proxy
- https_proxy
- HTTP_PROXY
- HTTPS_PROXY
This function can be used in Splunk modular inputs code before starting the ingestion to ensure that no proxy is going to be used when doing requests. In case of proxy is needed, it can be defined in the modular inputs code.
Source code in solnlib/utils.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
retry(retries=3, reraise=True, default_return=None, exceptions=None)
¶
A decorator to run function with max retries
times if there is
exception.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
retries |
int
|
(optional) Max retries times, default is 3. |
3
|
reraise |
bool
|
Whether exception should be reraised, default is True. |
True
|
default_return |
Any
|
(optional) Default return value for function run after max retries and reraise is False. |
None
|
exceptions |
List
|
(optional) List of exceptions that should retry. |
None
|
Source code in solnlib/utils.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
|