"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/qp/pub/common.py $
$Id: common.py 28673 2006-08-30 00:58:21Z dbinger $

Code for maintaining the global Publisher, and convenience functions
for operations that go through the publisher.
"""
from threading import _get_ident

_publisher = {}  #  { int : Publisher }

def set_publisher(value):
    global _publisher
    assert _get_ident() not in _publisher
    _publisher[_get_ident()] = value

def clear_publisher():
    # Used in test scripts only.
    global _publisher
    if _get_ident() in _publisher:
        del _publisher[_get_ident()]

def get_publisher():
    return _publisher.get(_get_ident())

# All of the remaining functions here work through get_publisher().

def get_hit():
    return get_publisher().get_hit()

def get_request():
    return get_hit().get_request()

def get_response():
    return get_hit().get_response()

def get_site():
    return get_publisher().get_site()

def get_path(n=0):
    return get_request().get_path(n=n)

def redirect(location, permanent=False):
    get_publisher().redirect(location, permanent=permanent)

def not_found(body=None):
    get_publisher().not_found(body=body)

def header(title, *args, **kwargs):
    return get_publisher().header(title, *args, **kwargs)

def footer(*args, **kwargs):
    return get_publisher().footer(*args, **kwargs)

def page(title, *content, **kwargs):
    return get_publisher().page(title, *content, **kwargs)

def respond(title, *content, **kwargs):
    return get_publisher().respond(title, *content, **kwargs)

def site_now():
    return get_publisher().get_time_zone().now()

# The remaining are for sites using DurusPublisher.

def get_session():
    return get_hit().get_session()

def get_user():
    return get_session().get_effective_user()

def ensure_signed_in(**kwargs):
    return get_publisher().ensure_signed_in(**kwargs)
