# 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
# Copyright 2017 onwards LabsLand Experimentia S.L.
# This software is licensed under the GNU AGPL v3:
# GNU Affero General Public License version 3 (see the file LICENSE)
# Read in the documentation about the license

from __future__ import unicode_literals, print_function, division








class _TaskWrapper(object):
    def __init__(self, weblab, func, unique):
        self._func = func
        self._unique = unique
        self._name = func.__name__

# 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

# Copyright 2017 onwards LabsLand Experimentia S.L.
# This software is licensed under the GNU AGPL v3:
# GNU Affero General Public License version 3 (see the file LICENSE)
# Read in the documentation about the license

from __future__ import unicode_literals, print_function, division








class _TaskWrapper(object):
    def __init__(self, weblab, func, unique):
        self._func = func
        self._unique = unique
        self._name = func.__name__

        if len(self._name) == len(create_token()):