# Path: yithlibraryserver/db.py
# def get_db(request):
#     return request.registry.settings['mongodb'].get_database()
# 
# Path: yithlibraryserver/locale.py
# class DatesFormatter(object):
# 
#     def __init__(self, locale_name):
#         self.locale_name = locale_name
# 
#     def date(self, date_value):
#         if HAS_BABEL:
#             return format_date(date_value, locale=self.locale_name)
#         else:
#             return date_value.strftime('%c')
# 
#     def datetime(self, datetime_value):
#         if HAS_BABEL:
#             return format_datetime(datetime_value, locale=self.locale_name)
#         else:
#             return datetime_value.strftime('%c')


from pyramid.events import BeforeRender, NewRequest
from pyramid.i18n import get_locale_name
from pyramid.renderers import get_renderer
from yithlibraryserver.db import get_db
from yithlibraryserver.locale import DatesFormatter


def add_compress_response_callback(event):

    def gzip_response(request, response):
        response.encode_content('gzip')
        return response

    accepted = event.request.accept_encoding.best_match(('identity', 'gzip'))
    if 'gzip' == accepted:
        event.request.add_response_callback(gzip_response)


def add_base_templates(event):

    def get_template(name):
        renderer = get_renderer('yithlibraryserver:templates/%s.pt' % name)
        return renderer.implementation()

    event.update({
            'base': get_template('base'),
            'profile': get_template('profile'),
            })


def add_custom_functions(event):

    locale_name = get_locale_name(event['request'])

    event.update({

# Path: yithlibraryserver/db.py
# def get_db(request):
#     return request.registry.settings['mongodb'].get_database()
# 
# Path: yithlibraryserver/locale.py
# class DatesFormatter(object):
# 
#     def __init__(self, locale_name):
#         self.locale_name = locale_name
# 
#     def date(self, date_value):
#         if HAS_BABEL:
#             return format_date(date_value, locale=self.locale_name)
#         else:
#             return date_value.strftime('%c')
# 
#     def datetime(self, datetime_value):
#         if HAS_BABEL:
#             return format_datetime(datetime_value, locale=self.locale_name)
#         else:
#             return datetime_value.strftime('%c')


# Path: yithlibraryserver/subscribers.py
from pyramid.events import BeforeRender, NewRequest
from pyramid.i18n import get_locale_name
from pyramid.renderers import get_renderer
from yithlibraryserver.db import get_db
from yithlibraryserver.locale import DatesFormatter



def add_compress_response_callback(event):

    def gzip_response(request, response):
        response.encode_content('gzip')
        return response

    accepted = event.request.accept_encoding.best_match(('identity', 'gzip'))
    if 'gzip' == accepted:
        event.request.add_response_callback(gzip_response)


def add_base_templates(event):

    def get_template(name):
        renderer = get_renderer('yithlibraryserver:templates/%s.pt' % name)
        return renderer.implementation()

    event.update({
            'base': get_template('base'),
            'profile': get_template('profile'),
            })


def add_custom_functions(event):

    locale_name = get_locale_name(event['request'])

    event.update({

            'dates_formatter': DatesFormatter(locale_name),