# Path: zoe_api/web/request_handler.py
# class JinjaApp:
#     """A Jinja2-capable Tornado application."""
#     def __init__(self, application, jinja_options=None):
#         self.application = application
#         self.jinja_options = jinja_options
#         self.init_app(application, jinja_options)
# 
#     @classmethod
#     def init_app(cls, application, jinja_options=None):
#         """Init the application."""
#         app_settings = application.settings
# 
#         _loader = FileSystemLoader(
#             app_settings.get('template_path', 'templates')
#         )
# 
#         _jinja_config = {
#             'extensions': ['jinja2.ext.autoescape', 'jinja2.ext.with_'],
#             'auto_reload': app_settings.get('autoreload', False),
#             'loader': _loader,
#             'cache_size': 50 if app_settings.get('compiled_template_cache', True) else 0,
#             'autoescape': app_settings.get('autoescape', 'xhtml_escape') == "xhtml_escape"
#         }
# 
#         _jinja_config.update(**(jinja_options or {}))
#         environment = Environment(**_jinja_config)
# 
#         application.jinja_environment = environment
#         app_settings['jinja_environment'] = environment
#         environment.filters.update(tojson=tojson_filter, xhtml_escape=xhtml_escape, url_escape=url_escape, squeeze=squeeze, linkify=linkify)
# 
#         return environment


import logging
import os
import zoe_lib.config
import zoe_lib.state
import zoe_api.api_endpoint
import zoe_api.rest_api
import zoe_api.master_api
import zoe_api.web
import zoe_api.auth.ldap
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop, PeriodicCallback
from tornado.web import Application
from zoe_api.web.request_handler import JinjaApp
    """
    zoe_lib.config.load_configuration(test_conf)
    args = zoe_lib.config.get_conf()

    log_args = {
        'level': logging.DEBUG if args.debug else logging.INFO,
        'format': LOG_FORMAT
    }
    if args.log_file != "stderr":
        log_args['filename'] = args.log_file
    logging.basicConfig(**log_args)
    logging.getLogger("MARKDOWN").setLevel(logging.WARNING)
    logging.getLogger("asyncio").setLevel(logging.INFO)
    logging.getLogger("tornado").setLevel(logging.WARNING)

    sql_manager = zoe_lib.state.SQLManager(zoe_lib.config.get_conf())
    sql_manager.init_db()

    master_api = zoe_api.master_api.APIManager()
    api_endpoint = zoe_api.api_endpoint.APIEndpoint(master_api, sql_manager)

    app_settings = {
        'static_path': os.path.join(os.path.dirname(__file__), "web", "static"),
        'static_url_prefix': args.reverse_proxy_path + '/static/',
        'template_path': os.path.join(os.path.dirname(__file__), "web", "templates"),
        'cookie_secret': zoe_lib.config.get_conf().cookie_secret,
        'login_url': '/login',
        'debug': args.debug
    }
    app = Application(zoe_api.web.web_init(api_endpoint) + zoe_api.rest_api.api_init(api_endpoint), **app_settings)

# Path: zoe_api/web/request_handler.py
# class JinjaApp:
#     """A Jinja2-capable Tornado application."""
#     def __init__(self, application, jinja_options=None):
#         self.application = application
#         self.jinja_options = jinja_options
#         self.init_app(application, jinja_options)
# 
#     @classmethod
#     def init_app(cls, application, jinja_options=None):
#         """Init the application."""
#         app_settings = application.settings
# 
#         _loader = FileSystemLoader(
#             app_settings.get('template_path', 'templates')
#         )
# 
#         _jinja_config = {
#             'extensions': ['jinja2.ext.autoescape', 'jinja2.ext.with_'],
#             'auto_reload': app_settings.get('autoreload', False),
#             'loader': _loader,
#             'cache_size': 50 if app_settings.get('compiled_template_cache', True) else 0,
#             'autoescape': app_settings.get('autoescape', 'xhtml_escape') == "xhtml_escape"
#         }
# 
#         _jinja_config.update(**(jinja_options or {}))
#         environment = Environment(**_jinja_config)
# 
#         application.jinja_environment = environment
#         app_settings['jinja_environment'] = environment
#         environment.filters.update(tojson=tojson_filter, xhtml_escape=xhtml_escape, url_escape=url_escape, squeeze=squeeze, linkify=linkify)
# 
#         return environment


# Path: zoe_api/entrypoint.py
import logging
import os
import zoe_lib.config
import zoe_lib.state
import zoe_api.api_endpoint
import zoe_api.rest_api
import zoe_api.master_api
import zoe_api.web
import zoe_api.auth.ldap
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop, PeriodicCallback
from tornado.web import Application
from zoe_api.web.request_handler import JinjaApp

    """
    zoe_lib.config.load_configuration(test_conf)
    args = zoe_lib.config.get_conf()

    log_args = {
        'level': logging.DEBUG if args.debug else logging.INFO,
        'format': LOG_FORMAT
    }
    if args.log_file != "stderr":
        log_args['filename'] = args.log_file
    logging.basicConfig(**log_args)
    logging.getLogger("MARKDOWN").setLevel(logging.WARNING)
    logging.getLogger("asyncio").setLevel(logging.INFO)
    logging.getLogger("tornado").setLevel(logging.WARNING)

    sql_manager = zoe_lib.state.SQLManager(zoe_lib.config.get_conf())
    sql_manager.init_db()

    master_api = zoe_api.master_api.APIManager()
    api_endpoint = zoe_api.api_endpoint.APIEndpoint(master_api, sql_manager)

    app_settings = {
        'static_path': os.path.join(os.path.dirname(__file__), "web", "static"),
        'static_url_prefix': args.reverse_proxy_path + '/static/',
        'template_path': os.path.join(os.path.dirname(__file__), "web", "templates"),
        'cookie_secret': zoe_lib.config.get_conf().cookie_secret,
        'login_url': '/login',
        'debug': args.debug
    }
    app = Application(zoe_api.web.web_init(api_endpoint) + zoe_api.rest_api.api_init(api_endpoint), **app_settings)

    JinjaApp.init_app(app)