# Path: weblablib/utils.py
# def create_token(size=None):
#     if size is None:
#         size = 32
#     tok = os.urandom(size)
#     safe_token = base64.urlsafe_b64encode(tok).strip().replace(b'=', b'').replace(b'-', b'_')
#     safe_token = safe_token.decode('utf8')
#     return safe_token
# 
# def _current_session_id():
#     return _current_weblab()._session_id()
# 
# def _current_weblab():
#     if 'weblab' not in current_app.extensions:
#         raise WebLabNotInitializedError("App not initialized with weblab.init_app()")
#     return current_app.extensions['weblab']
# 
# Path: weblablib/exc.py
# class AlreadyRunningError(WebLabError):
#     """When creating a task (:meth:`WebLab.task`) with ``unique='global'`` or ``unique='user'``, the second thread/process attempting to run the same method will obtain this error"""
#     pass
# 
# class TimeoutError(WebLabError):
#     """When joining (:meth:`WebLabTask.join`) a task with a timeout, this error may arise"""
#     pass


import sys
import time
import threading
import traceback
import six
import redis
from werkzeug.local import LocalProxy
from werkzeug.datastructures import ImmutableDict
from flask import g
from weblablib.utils import create_token, _current_session_id, _current_weblab
from weblablib.exc import AlreadyRunningError, TimeoutError
    def run(self):
        _TaskRunner._instances.append(self)

        while not self._stopping:
            try:
                with self.app.app_context():
                    self.weblab.run_tasks()
            except redis.ConnectionError:
                # In the case of a redis ConnectionError, let's wait a bit more to see if
                # this happens again. It can be that we are just restarting the server
                # and Redis died before, or a Redis upgrade or so.
                traceback.print_exc()
                time.sleep(5)
            except Exception:
                traceback.print_exc()
                continue

            for _ in six.moves.range(_TaskRunner._STEPS_WAITING):
                time.sleep(0.05)
                if self._stopping:
                    break


def _current_task():
    task_id = getattr(g, '_weblab_task_id', None)
    if task_id is None:
        return None

    weblab_task = getattr(g, '_weblab_task', None)
    if weblab_task is None:

# Path: weblablib/utils.py
# def create_token(size=None):
#     if size is None:
#         size = 32
#     tok = os.urandom(size)
#     safe_token = base64.urlsafe_b64encode(tok).strip().replace(b'=', b'').replace(b'-', b'_')
#     safe_token = safe_token.decode('utf8')
#     return safe_token
# 
# def _current_session_id():
#     return _current_weblab()._session_id()
# 
# def _current_weblab():
#     if 'weblab' not in current_app.extensions:
#         raise WebLabNotInitializedError("App not initialized with weblab.init_app()")
#     return current_app.extensions['weblab']
# 
# Path: weblablib/exc.py
# class AlreadyRunningError(WebLabError):
#     """When creating a task (:meth:`WebLab.task`) with ``unique='global'`` or ``unique='user'``, the second thread/process attempting to run the same method will obtain this error"""
#     pass
# 
# class TimeoutError(WebLabError):
#     """When joining (:meth:`WebLabTask.join`) a task with a timeout, this error may arise"""
#     pass


# Path: weblablib/tasks.py
import sys
import time
import threading
import traceback
import six
import redis
from werkzeug.local import LocalProxy
from werkzeug.datastructures import ImmutableDict
from flask import g
from weblablib.utils import create_token, _current_session_id, _current_weblab
from weblablib.exc import AlreadyRunningError, TimeoutError

    def run(self):
        _TaskRunner._instances.append(self)

        while not self._stopping:
            try:
                with self.app.app_context():
                    self.weblab.run_tasks()
            except redis.ConnectionError:
                # In the case of a redis ConnectionError, let's wait a bit more to see if
                # this happens again. It can be that we are just restarting the server
                # and Redis died before, or a Redis upgrade or so.
                traceback.print_exc()
                time.sleep(5)
            except Exception:
                traceback.print_exc()
                continue

            for _ in six.moves.range(_TaskRunner._STEPS_WAITING):
                time.sleep(0.05)
                if self._stopping:
                    break


def _current_task():
    task_id = getattr(g, '_weblab_task_id', None)
    if task_id is None:
        return None

    weblab_task = getattr(g, '_weblab_task', None)
    if weblab_task is None:

        weblab = _current_weblab()