"""
open/DurusWorks/qp/pub/hit.py

"""
from qp.http.request import HTTPRequest
from qp.http.response import HTTPResponse
from qp.lib.spec import add_getters, spec, specify, datetime_with_tz
from qp.pub.common import get_publisher, site_now
from qp.pub.session import Session


class Hit (object):

    request_is = HTTPRequest
    response_is = HTTPResponse
    session_is = (Session, None)
    time_is = spec(
        datetime_with_tz,
        "time of the creation of the Hit.")
    info_is = spec(
        dict,
        "A place for application code to squirrel away things as needed "
        "while processing the request.")

    def __init__(self, input_stream, cgi_environment):
        self.request = self.request_is(input_stream, cgi_environment)
        self.init_response()
        self.time = site_now()
        self.info = {}
        self.session = None

    def init_response(self):
        self.response = self.response_is()
        self.response.set_compress(self.request.accepts_gzip_encoding())
        self.response.set_range(self.request.get_range())

    def set_session(self, session):
        specify(self, session=session)

    def get_session(self):
        if self.session is None:
            self.set_session(get_publisher().create_session())
        return self.session

add_getters(Hit)


