# 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_backend():
#     return _current_weblab()._backend
# 
# def _current_timestamp():
#     return float(_to_timestamp(datetime.datetime.now()))
# 
# def _current_weblab():
#     if 'weblab' not in current_app.extensions:
#         raise WebLabNotInitializedError("App not initialized with weblab.init_app()")
#     return current_app.extensions['weblab']
# 
# def _current_session_id():
#     return _current_weblab()._session_id()
# 
# Path: weblablib/tasks.py
# class _TaskWrapper(object):
# class WebLabTask(object):
# class _TaskRunner(threading.Thread):
#     def __init__(self, weblab, func, unique):
#     def func(self):
#     def unique(self):
#     def __call__(self, *args, **kwargs):
#     def delay(self, *args, **kwargs):
#     def run_sync(self, *args, **kwargs):
#     def __init__(self, weblab, task_id):
#     def join(self, timeout=None, error_on_timeout=True):
#     def task_id(self):
#     def session_id(self):
#     def name(self):
#     def _is_current_task(self):
#     def data(self):
#     def data(self, new_data):
#     def update_data(self, new_data):
#     def store(self):
#     def retrieve(self):
#     def stop(self):
#     def status(self):
#     def done(self):
#     def running(self):
#     def submitted(self):
#     def failed(self):
#     def finished(self):
#     def stopping(self):
#     def result(self):
#     def error(self):
#     def __repr__(self):
#     def __lt__(self, other):
#     def __cmp__(self, other):
#     def __eq__(self, other):
#     def __hash__(self):
#     def __init__(self, number, weblab, app):
#     def stop(self):
#     def run(self):
# def _current_task():
# def _current_task_stopping():
#     _STEPS_WAITING = 20


import abc
import json
import zlib
import warnings
import six
from werkzeug.datastructures import ImmutableDict
from werkzeug.local import LocalProxy
from flask import g, current_app
from weblablib.utils import create_token, _current_backend, _current_timestamp, \
     _current_weblab, _current_session_id
from weblablib.tasks import current_task
from __future__ import unicode_literals, print_function, division






def get_weblab_user(cached=True):
    """
    Get the current user. If ``cached=True`` it will store it and return the same each time.
    If you need to get always a different one get it with ``cached=False``. In long tasks, for
    example, it's normal to call it with ``cached=False`` so it gets updated with whatever
    information comes from other threads.

    Two shortcuts exist to this function:
     * :data:`weblab_user`: it is equivalent to ``get_weblab_user(cached=True)``
     * :data:`socket_weblab_user`: it is equivalent to ``get_weblab_user(cached=False)``

    Given that the function always returns a :class:`CurrentUser` or :class:`ExpiredUser` or :class:`AnonymousUser`, it's safe to do things like::

       if not weblab_user.anonymous:
           print(weblab_user.username)
           print(weblab_user.username_unique)

    :param cached: if this method is called twice in the same thread, it will return the same object.
    """
    if cached and hasattr(g, 'weblab_user'):
        return g.weblab_user

    # Cached: then use Redis

# 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_backend():
#     return _current_weblab()._backend
# 
# def _current_timestamp():
#     return float(_to_timestamp(datetime.datetime.now()))
# 
# def _current_weblab():
#     if 'weblab' not in current_app.extensions:
#         raise WebLabNotInitializedError("App not initialized with weblab.init_app()")
#     return current_app.extensions['weblab']
# 
# def _current_session_id():
#     return _current_weblab()._session_id()
# 
# Path: weblablib/tasks.py
# class _TaskWrapper(object):
# class WebLabTask(object):
# class _TaskRunner(threading.Thread):
#     def __init__(self, weblab, func, unique):
#     def func(self):
#     def unique(self):
#     def __call__(self, *args, **kwargs):
#     def delay(self, *args, **kwargs):
#     def run_sync(self, *args, **kwargs):
#     def __init__(self, weblab, task_id):
#     def join(self, timeout=None, error_on_timeout=True):
#     def task_id(self):
#     def session_id(self):
#     def name(self):
#     def _is_current_task(self):
#     def data(self):
#     def data(self, new_data):
#     def update_data(self, new_data):
#     def store(self):
#     def retrieve(self):
#     def stop(self):
#     def status(self):
#     def done(self):
#     def running(self):
#     def submitted(self):
#     def failed(self):
#     def finished(self):
#     def stopping(self):
#     def result(self):
#     def error(self):
#     def __repr__(self):
#     def __lt__(self, other):
#     def __cmp__(self, other):
#     def __eq__(self, other):
#     def __hash__(self):
#     def __init__(self, number, weblab, app):
#     def stop(self):
#     def run(self):
# def _current_task():
# def _current_task_stopping():
#     _STEPS_WAITING = 20


# Path: weblablib/users.py
import abc
import json
import zlib
import warnings
import six
from werkzeug.datastructures import ImmutableDict
from werkzeug.local import LocalProxy
from flask import g, current_app
from weblablib.utils import create_token, _current_backend, _current_timestamp, \
     _current_weblab, _current_session_id
from weblablib.tasks import current_task

from __future__ import unicode_literals, print_function, division






def get_weblab_user(cached=True):
    """
    Get the current user. If ``cached=True`` it will store it and return the same each time.
    If you need to get always a different one get it with ``cached=False``. In long tasks, for
    example, it's normal to call it with ``cached=False`` so it gets updated with whatever
    information comes from other threads.

    Two shortcuts exist to this function:
     * :data:`weblab_user`: it is equivalent to ``get_weblab_user(cached=True)``
     * :data:`socket_weblab_user`: it is equivalent to ``get_weblab_user(cached=False)``

    Given that the function always returns a :class:`CurrentUser` or :class:`ExpiredUser` or :class:`AnonymousUser`, it's safe to do things like::

       if not weblab_user.anonymous:
           print(weblab_user.username)
           print(weblab_user.username_unique)

    :param cached: if this method is called twice in the same thread, it will return the same object.
    """
    if cached and hasattr(g, 'weblab_user'):
        return g.weblab_user

    # Cached: then use Redis

    session_id = _current_session_id()