Mini Shell
"""backwards compat for rads.context"""
# pylint: disable=wrong-import-position,wrong-import-order
import __main__ as main_obj
import time
import warnings
from contextlib import contextmanager
import socket
import hashlib
import pymysql
from ._locking import LockError as S_LockError
from .compat import RadsDeprecation
@contextmanager
def db_cur(_autocommit=False, _yield_conn=False, **kwargs):
"""deprecated"""
warnings.warn(
'rads.context.db_cur is deprecated. Use pymysql.connect',
RadsDeprecation,
stacklevel=3,
)
kwargs['autocommit'] = _autocommit
if 'db' in kwargs:
warnings.warn(
'The db= kwarg is deprecated in PyMySQL. Please use database=',
RadsDeprecation,
stacklevel=3,
)
kwargs['database'] = kwargs.pop('db')
with pymysql.connect(**kwargs) as conn:
with conn.cursor() as cur:
if _yield_conn:
yield conn, cur
else:
yield cur
@contextmanager
def s_lock(name=None):
"""deprecated"""
warnings.warn(
'rads.context.s_lock is deprecated. see rads.lock',
RadsDeprecation,
stacklevel=3,
)
if name is None:
# caveat: this will crash if run in an interactive python shell
name = main_obj.__file__
name = name.encode('utf-8')
lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
try:
lock_socket.bind('\0%s' % hashlib.sha256(name).hexdigest())
except socket.error as exc:
raise S_LockError(name) from exc
try:
yield
finally:
lock_socket.close()
@contextmanager
def wait_lock(name=None, sleep=0.5, max_sleep=None):
"""deprecated"""
warnings.warn(
'rads.context.wait_lock is deprecated. see rads.wait_lock',
RadsDeprecation,
stacklevel=3,
)
if name is None:
# caveat: this will crash if run in an interactive python shell
name = main_obj.__file__
slept = 0.0
while True:
try:
with s_lock(name):
yield
return
except S_LockError:
time.sleep(sleep)
slept += sleep
if max_sleep is not None and slept >= max_sleep:
raise
Zerion Mini Shell 1.0