| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import base64 |
| 14 |
import sys |
| 15 |
|
| 16 |
import futurist |
| 17 |
|
| 18 |
bytes_types = (bytes, bytearray) |
| 19 |
|
| 20 |
|
| 21 |
def _encode(data): |
| 22 |
"""Encode the given data using base-64 |
| 23 |
|
| 24 |
:param data: |
| 25 |
:return: base-64 encoded string |
| 26 |
""" |
| 27 |
if not isinstance(data, bytes_types): |
| 28 |
data = str(data).encode("latin-1") |
| 29 |
return base64.b64encode(data).decode("utf-8") |
| 30 |
|
| 31 |
|
| 32 |
def _decode(data): |
| 33 |
"""Decode the base-64 encoded string |
| 34 |
|
| 35 |
:param data: |
| 36 |
:return: decoded data |
| 37 |
""" |
| 38 |
if not isinstance(data, bytes_types): |
| 39 |
data = str(data).encode("latin-1") |
| 40 |
return base64.b64decode(data.decode("utf-8")) |
| 41 |
|
| 42 |
|
| 43 |
def _increment_last_byte(data): |
| 44 |
"""Get the last byte in the array and increment it |
| 45 |
|
| 46 |
:param bytes_string: |
| 47 |
:return: |
| 48 |
""" |
| 49 |
if not isinstance(data, bytes_types): |
| 50 |
if isinstance(data, str): |
| 51 |
data = data.encode('utf-8') |
| 52 |
else: |
| 53 |
data = str(data).encode("latin-1") |
| 54 |
s = bytearray(data) |
| 55 |
s[-1] = s[-1] + 1 |
| 56 |
return bytes(s) |
| 57 |
|
| 58 |
|
| 59 |
DEFAULT_TIMEOUT = 30 |
| 60 |
LOCK_PREFIX = '/locks/' |
| 61 |
|
| 62 |
|
| 63 |
def _import_module(import_str): |
| 64 |
"""Import a module.""" |
| 65 |
__import__(import_str) |
| 66 |
return sys.modules[import_str] |
| 67 |
|
| 68 |
|
| 69 |
def _try_import(import_str, default=None): |
| 70 |
"""Try to import a module and if it fails return default.""" |
| 71 |
try: |
| 72 |
return _import_module(import_str) |
| 73 |
except ImportError: |
| 74 |
return default |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
_eventlet = _try_import('eventlet') |
| 79 |
_patcher = _try_import('eventlet.patcher') |
| 80 |
|
| 81 |
|
| 82 |
def _get_threadpool_executor(): |
| 83 |
if all((_eventlet, _patcher)) and _patcher.is_monkey_patched('thread'): |
| 84 |
return futurist.GreenThreadPoolExecutor |
| 85 |
return futurist.ThreadPoolExecutor |