# Path: weblablib/exc.py
# class NotFoundError(WebLabError, KeyError):
#     pass
# 
# Path: weblablib/utils.py
# 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_backend():
#     return _current_weblab()._backend
# 
# def _current_session_id():
#     return _current_weblab()._session_id()
# 
# Path: weblablib/users.py
# def get_weblab_user(cached=True):
# def _set_weblab_user_cache(user):
#     def active(self):
#     def is_anonymous(self):
#     def __str__(self):
#     def _method(self, *args, **kwargs): # pylint: disable=unused-argument, no-self-use
#     def active(self):
#     def is_anonymous(self):
#     def locale(self):
#     def data(self):
#     def __str__(self):
#     def __init__(self, session_id, back, last_poll, max_date, username, username_unique,
#                  exited, data, locale, full_name, experiment_name, category_name, experiment_id,
#                  request_client_data, request_server_data, start_date):
#     def experiment_name(self):
#     def category_name(self):
#     def experiment_id(self):
#     def full_name(self):
#     def locale(self):
#     def back(self):
#     def last_poll(self):
#     def session_id(self):
#     def max_date(self):
#     def username(self):
#     def username_unique(self):
#     def exited(self):
#     def request_client_data(self):
#     def request_server_data(self):
#     def start_date(self):
#     def add_action(self, session_id, action):
#     def store_action(self, session_id, action_id, action): # pylint: disable=no-self-use
#     def clean_actions(self, session_id): # pylint: disable=no-self-use
#     def __init__(self, user, data, previous_hash=None):
#     def initial_hash(self):
#     def _get_hash(self, data):
#     def store(self):
#     def store_if_modified(self):
#     def is_modified(self):
#     def retrieve(self):
#     def __init__(self, *args, **kwargs):
#     def data(self):
#     def data(self, data):
#     def update_data(self, new_data=_OBJECT):
#     def time_without_polling(self):
#     def time_left(self):
#     def to_expired_user(self):
#     def user(self):
#     def active(self):
#     def is_anonymous(self):
#     def __str__(self):
#     def __init__(self, *args, **kwargs):
#     def data(self):
#     def data(self, value):
#     def update_data(self, value=None):
#     def time_left(self):
#     def active(self):
#     def is_anonymous(self):
#     def __str__(self):
# class WebLabUser(object):
# class AnonymousDataImmutableDict(dict):
# class AnonymousUser(WebLabUser):
# class _CurrentOrExpiredUser(WebLabUser): # pylint: disable=abstract-method
# class DataHolder(dict):
# class CurrentUser(_CurrentOrExpiredUser):
# class ExpiredUser(_CurrentOrExpiredUser):
# _OBJECT = object()


import time
import json
import traceback
from flask import g
from weblablib.exc import NotFoundError
from weblablib.utils import _current_weblab, _current_backend, _current_session_id
from weblablib.users import ExpiredUser, CurrentUser, weblab_user, _set_weblab_user_cache




def status_time(session_id):
    weblab = _current_weblab()
    backend = weblab._backend
    user = backend.get_user(session_id)
    if isinstance(user, ExpiredUser) and user.disposing_resources:
        return 2 # Try again in 2 seconds

    if user.is_anonymous or not isinstance(user, CurrentUser):
        return -1

    if user.exited:
        return -1

    if weblab.timeout and weblab.timeout > 0:
        # If timeout is set to -1, it will never timeout (unless user exited)
        if user.time_without_polling >= weblab.timeout:
            return -1

    if user.time_left <= 0:
        return -1

    return min(weblab.poll_interval, int(user.time_left))

def store_initial_weblab_user_data():
    session_id = _current_session_id()
    if session_id:

# Path: weblablib/exc.py
# class NotFoundError(WebLabError, KeyError):
#     pass
# 
# Path: weblablib/utils.py
# 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_backend():
#     return _current_weblab()._backend
# 
# def _current_session_id():
#     return _current_weblab()._session_id()
# 
# Path: weblablib/users.py
# def get_weblab_user(cached=True):
# def _set_weblab_user_cache(user):
#     def active(self):
#     def is_anonymous(self):
#     def __str__(self):
#     def _method(self, *args, **kwargs): # pylint: disable=unused-argument, no-self-use
#     def active(self):
#     def is_anonymous(self):
#     def locale(self):
#     def data(self):
#     def __str__(self):
#     def __init__(self, session_id, back, last_poll, max_date, username, username_unique,
#                  exited, data, locale, full_name, experiment_name, category_name, experiment_id,
#                  request_client_data, request_server_data, start_date):
#     def experiment_name(self):
#     def category_name(self):
#     def experiment_id(self):
#     def full_name(self):
#     def locale(self):
#     def back(self):
#     def last_poll(self):
#     def session_id(self):
#     def max_date(self):
#     def username(self):
#     def username_unique(self):
#     def exited(self):
#     def request_client_data(self):
#     def request_server_data(self):
#     def start_date(self):
#     def add_action(self, session_id, action):
#     def store_action(self, session_id, action_id, action): # pylint: disable=no-self-use
#     def clean_actions(self, session_id): # pylint: disable=no-self-use
#     def __init__(self, user, data, previous_hash=None):
#     def initial_hash(self):
#     def _get_hash(self, data):
#     def store(self):
#     def store_if_modified(self):
#     def is_modified(self):
#     def retrieve(self):
#     def __init__(self, *args, **kwargs):
#     def data(self):
#     def data(self, data):
#     def update_data(self, new_data=_OBJECT):
#     def time_without_polling(self):
#     def time_left(self):
#     def to_expired_user(self):
#     def user(self):
#     def active(self):
#     def is_anonymous(self):
#     def __str__(self):
#     def __init__(self, *args, **kwargs):
#     def data(self):
#     def data(self, value):
#     def update_data(self, value=None):
#     def time_left(self):
#     def active(self):
#     def is_anonymous(self):
#     def __str__(self):
# class WebLabUser(object):
# class AnonymousDataImmutableDict(dict):
# class AnonymousUser(WebLabUser):
# class _CurrentOrExpiredUser(WebLabUser): # pylint: disable=abstract-method
# class DataHolder(dict):
# class CurrentUser(_CurrentOrExpiredUser):
# class ExpiredUser(_CurrentOrExpiredUser):
# _OBJECT = object()


# Path: weblablib/ops.py
import time
import json
import traceback
from flask import g
from weblablib.exc import NotFoundError
from weblablib.utils import _current_weblab, _current_backend, _current_session_id
from weblablib.users import ExpiredUser, CurrentUser, weblab_user, _set_weblab_user_cache





def status_time(session_id):
    weblab = _current_weblab()
    backend = weblab._backend
    user = backend.get_user(session_id)
    if isinstance(user, ExpiredUser) and user.disposing_resources:
        return 2 # Try again in 2 seconds

    if user.is_anonymous or not isinstance(user, CurrentUser):
        return -1

    if user.exited:
        return -1

    if weblab.timeout and weblab.timeout > 0:
        # If timeout is set to -1, it will never timeout (unless user exited)
        if user.time_without_polling >= weblab.timeout:
            return -1

    if user.time_left <= 0:
        return -1

    return min(weblab.poll_interval, int(user.time_left))

def store_initial_weblab_user_data():
    session_id = _current_session_id()
    if session_id:

        backend = _current_backend()