# Path: gunstar/session.py
# class Session(object):
# 
#     salt = 'cookie-session'
# 
#     def __init__(self, app, request, response):
#         self.app = app
#         self.request = request
#         self.response = response
#         self.modified = False
#         self.cleared = False
#         self.secret_key = self.app.config.get('SECRET_KEY')
#         self.session_cookie_name = self.app.config.get('SESSION_COOKIE_NAME')
#         self.session_cookie_domain = self.app.config.get(
#             'SESSION_COOKIE_DOMAIN'
#         )
#         self.session_cookie_path = self.app.config.get('SESSION_COOKIE_PATH')
#         self.session_cookie_httponly = self.app.config.get(
#             'SESSION_COOKIE_HTTPONLY'
#         )
#         self.session_cookie_secure = self.app.config.get(
#             'SESSION_COOKIE_SECURE'
#         )
#         self.max_age = self.app.config.get('PERMANENT_SESSION_LIFETIME')
#         self.data = {}
# 
#         # init session
#         self.get_session()
# 
#     def get_max_age(self):
#         return self.max_age.seconds + (self.max_age.days * 24 * 3600)
# 
#     def get_cookie_domain(self):
#         cookie_domain = self.session_cookie_domain
#         if not cookie_domain:
#             cookie_domain = self.request.host.rsplit(':', 1)[0]
#         return cookie_domain
# 
#     def get_serializer(self):
#         return URLSafeTimedSerializer(self.secret_key, salt=self.salt)
# 
#     def get_session(self):
#         if not self.secret_key:
#             return None
# 
#         s = self.get_serializer()
#         cookie_value = self.request.cookies.get(self.session_cookie_name)
# 
#         if not cookie_value:
#             return None
# 
#         try:
#             self.data = s.loads(cookie_value, max_age=self.get_max_age())
#         except BadSignature:
#             return None
# 
#     def get(self, key, default=None):
#         return self.data.get(key, default)
# 
#     def set(self, key, value):
#         self.data[key] = value
#         self.modified = True
# 
#     def delete(self, key):
#         self.data.pop(key, None)
#         self.modified = True
# 
#     def clear(self):
#         self.data.clear()
#         self.cleared = True
# 
#     def save(self):
#         if not self.secret_key:
#             return None
# 
#         if self.cleared:
#             self.response.delete_cookie(
#                 self.session_cookie_name,
#                 path=self.session_cookie_path,
#                 domain=self.get_cookie_domain()
#             )
# 
#         if self.modified:
#             s = self.get_serializer()
#             cookie_value = s.dumps(dict(self.data))
#             self.response.set_cookie(
#                 self.session_cookie_name,
#                 cookie_value,
#                 max_age=self.max_age,
#                 path=self.session_cookie_path,
#                 domain=self.get_cookie_domain(),
#                 secure=self.session_cookie_secure,
#                 httponly=self.session_cookie_httponly
#             )
#         return None
# 
# Path: gunstar/template.py
# @evalcontextfilter
# def linebreaks(eval_ctx, value):
#     value = re.sub(r'\r\n|\r|\n', '\n', value)  # normalize newlines
#     paras = re.split('\n{2,}', value)
#     paras = [u'<p>%s</p>' % p.replace('\n', '<br />') for p in paras]
#     paras = u'\n\n'.join(paras)
#     return Markup(paras)
# 
# @evalcontextfilter
# def linebreaksbr(eval_ctx, value):
#     value = re.sub(r'\r\n|\r|\n', '\n', value)  # normalize newlines
#     paras = re.split('\n{2,}', value)
#     paras = [u'%s' % p.replace('\n', '<br />') for p in paras]
#     paras = u'\n\n'.join(paras)
#     return Markup(paras)
# 
# Path: gunstar/signals.py



from jinja2 import Environment, FileSystemLoader, TemplateNotFound
from webob import Request as WebObRequest, Response as WebObResponse
from webob.static import DirectoryApp as WebObDirectoryApp
from gunstar.session import Session
from gunstar.template import linebreaks, linebreaksbr
from gunstar.signals import template_rendered_signal
import traceback
import os.path
        if self.app.config.get('TEMPLATE_PATH', None):
            template_dirs.append(self.app.config['TEMPLATE_PATH'])
        template_dirs.append(GUNSTAR_TEMPLATE_PATH)
        template_env = Environment(
            loader=FileSystemLoader(template_dirs),
            extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_'],
            autoescape=True
        )
        template_env.globals.update(self.get_template_globals())
        template_env.filters.update(self.get_template_filters())
        return template_env

    def render_template(self, template_name, **kwargs):
        try:
            template = self.template_env.get_template(template_name)
        except TemplateNotFound:
            raise TemplateNotFound(template_name)
        template_rendered = template.render(kwargs)
        template_rendered_signal.send(
            self.app, handler=self, template=template_rendered, context=kwargs
        )
        self.response.text = template_rendered

    def reverse_route(self, name, *args):
        url = ''
        route = self.app.router.find_route_by_name(name)
        if route:
            reverse = route.reverse_route(*args)
            if reverse:
                url = reverse

# Path: gunstar/session.py
# class Session(object):
# 
#     salt = 'cookie-session'
# 
#     def __init__(self, app, request, response):
#         self.app = app
#         self.request = request
#         self.response = response
#         self.modified = False
#         self.cleared = False
#         self.secret_key = self.app.config.get('SECRET_KEY')
#         self.session_cookie_name = self.app.config.get('SESSION_COOKIE_NAME')
#         self.session_cookie_domain = self.app.config.get(
#             'SESSION_COOKIE_DOMAIN'
#         )
#         self.session_cookie_path = self.app.config.get('SESSION_COOKIE_PATH')
#         self.session_cookie_httponly = self.app.config.get(
#             'SESSION_COOKIE_HTTPONLY'
#         )
#         self.session_cookie_secure = self.app.config.get(
#             'SESSION_COOKIE_SECURE'
#         )
#         self.max_age = self.app.config.get('PERMANENT_SESSION_LIFETIME')
#         self.data = {}
# 
#         # init session
#         self.get_session()
# 
#     def get_max_age(self):
#         return self.max_age.seconds + (self.max_age.days * 24 * 3600)
# 
#     def get_cookie_domain(self):
#         cookie_domain = self.session_cookie_domain
#         if not cookie_domain:
#             cookie_domain = self.request.host.rsplit(':', 1)[0]
#         return cookie_domain
# 
#     def get_serializer(self):
#         return URLSafeTimedSerializer(self.secret_key, salt=self.salt)
# 
#     def get_session(self):
#         if not self.secret_key:
#             return None
# 
#         s = self.get_serializer()
#         cookie_value = self.request.cookies.get(self.session_cookie_name)
# 
#         if not cookie_value:
#             return None
# 
#         try:
#             self.data = s.loads(cookie_value, max_age=self.get_max_age())
#         except BadSignature:
#             return None
# 
#     def get(self, key, default=None):
#         return self.data.get(key, default)
# 
#     def set(self, key, value):
#         self.data[key] = value
#         self.modified = True
# 
#     def delete(self, key):
#         self.data.pop(key, None)
#         self.modified = True
# 
#     def clear(self):
#         self.data.clear()
#         self.cleared = True
# 
#     def save(self):
#         if not self.secret_key:
#             return None
# 
#         if self.cleared:
#             self.response.delete_cookie(
#                 self.session_cookie_name,
#                 path=self.session_cookie_path,
#                 domain=self.get_cookie_domain()
#             )
# 
#         if self.modified:
#             s = self.get_serializer()
#             cookie_value = s.dumps(dict(self.data))
#             self.response.set_cookie(
#                 self.session_cookie_name,
#                 cookie_value,
#                 max_age=self.max_age,
#                 path=self.session_cookie_path,
#                 domain=self.get_cookie_domain(),
#                 secure=self.session_cookie_secure,
#                 httponly=self.session_cookie_httponly
#             )
#         return None
# 
# Path: gunstar/template.py
# @evalcontextfilter
# def linebreaks(eval_ctx, value):
#     value = re.sub(r'\r\n|\r|\n', '\n', value)  # normalize newlines
#     paras = re.split('\n{2,}', value)
#     paras = [u'<p>%s</p>' % p.replace('\n', '<br />') for p in paras]
#     paras = u'\n\n'.join(paras)
#     return Markup(paras)
# 
# @evalcontextfilter
# def linebreaksbr(eval_ctx, value):
#     value = re.sub(r'\r\n|\r|\n', '\n', value)  # normalize newlines
#     paras = re.split('\n{2,}', value)
#     paras = [u'%s' % p.replace('\n', '<br />') for p in paras]
#     paras = u'\n\n'.join(paras)
#     return Markup(paras)
# 
# Path: gunstar/signals.py



# Path: gunstar/http.py
from jinja2 import Environment, FileSystemLoader, TemplateNotFound
from webob import Request as WebObRequest, Response as WebObResponse
from webob.static import DirectoryApp as WebObDirectoryApp
from gunstar.session import Session
from gunstar.template import linebreaks, linebreaksbr
from gunstar.signals import template_rendered_signal
import traceback
import os.path

        if self.app.config.get('TEMPLATE_PATH', None):
            template_dirs.append(self.app.config['TEMPLATE_PATH'])
        template_dirs.append(GUNSTAR_TEMPLATE_PATH)
        template_env = Environment(
            loader=FileSystemLoader(template_dirs),
            extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_'],
            autoescape=True
        )
        template_env.globals.update(self.get_template_globals())
        template_env.filters.update(self.get_template_filters())
        return template_env

    def render_template(self, template_name, **kwargs):
        try:
            template = self.template_env.get_template(template_name)
        except TemplateNotFound:
            raise TemplateNotFound(template_name)
        template_rendered = template.render(kwargs)
        template_rendered_signal.send(
            self.app, handler=self, template=template_rendered, context=kwargs
        )
        self.response.text = template_rendered

    def reverse_route(self, name, *args):
        url = ''
        route = self.app.router.find_route_by_name(name)
        if route:
            reverse = route.reverse_route(*args)
            if reverse:
                url = reverse

        return url