# coding: u8

import logging

from tornado.web import StaticFileHandler
import redis

from ius.main import settings, filters
from ius.utils import template

__all__ = ['logger', 'render', 'rds']


def logger(name):
    l = logging.getLogger('ius.sanhaolu.com.' + name)
    l.setLevel(logging.INFO if settings.DEBUG else logging.WARNING)
    return l

_extra = {
    'settings': settings,
    'static_url': lambda path: StaticFileHandler.make_static_url(
        settings.TORNADO_SETTINGS, path)
}
# add python builtins functions to jinja2 template
_extra.update(__builtins__)

render = template.Render(
    auto_reload=True,
    autoescape=True,
    extensions=[
        'jinja2.ext.do',
        'jinja2.ext.loopcontrols',
        'pyjade.ext.jinja.PyJadeExtension',
    ],
    extra=_extra,
    variable_start_string='{{ ',  # confict with angularjs
    variable_end_string=' }}',  # {{no_space}} should be used in angularjs
    template_path=settings.TEMPLATE_ROOT,
    undefined=template.UndefinedSilently,
)

RDS = settings.REDIS
_redis_kw = {
    'host': RDS.HOST,
    'port': RDS.PORT,
    'db': RDS.DB
}
render.add_filters(filters.filters)
rds = redis.StrictRedis(**_redis_kw)
