2014-01-28 16:41:19 -08:00
|
|
|
import indexreader
|
|
|
|
import utils
|
|
|
|
import wbrequestresponse
|
|
|
|
import wbexceptions
|
2014-01-28 20:18:47 -08:00
|
|
|
import time
|
2014-01-28 16:41:19 -08:00
|
|
|
|
2014-01-28 22:03:01 -08:00
|
|
|
from os import path
|
2014-01-28 16:41:19 -08:00
|
|
|
from itertools import imap
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
|
|
|
|
|
|
|
#=================================================================
|
2014-01-31 10:04:21 -08:00
|
|
|
class StaticTextView:
|
|
|
|
def __init__(self, text):
|
|
|
|
self.text = text
|
|
|
|
|
|
|
|
def render_to_string(self, **kwargs):
|
|
|
|
return self.text
|
|
|
|
|
|
|
|
def render_response(self, **kwargs):
|
|
|
|
return wbrequestresponse.WbResponse.text_stream(self.text)
|
2014-01-28 16:41:19 -08:00
|
|
|
|
|
|
|
#=================================================================
|
2014-01-31 10:04:21 -08:00
|
|
|
class J2TemplateView:
|
|
|
|
def __init__(self, filename):
|
2014-01-28 22:03:01 -08:00
|
|
|
template_dir, template_file = path.split(filename)
|
|
|
|
|
2014-01-28 16:41:19 -08:00
|
|
|
self.template_file = template_file
|
|
|
|
|
2014-01-31 10:04:21 -08:00
|
|
|
self.jinja_env = self.make_jinja_env(template_dir)
|
|
|
|
|
2014-01-28 16:41:19 -08:00
|
|
|
|
2014-01-31 10:04:21 -08:00
|
|
|
def make_jinja_env(self, template_dir):
|
|
|
|
jinja_env = Environment(loader = FileSystemLoader(template_dir), trim_blocks = True)
|
|
|
|
jinja_env.filters['format_ts'] = J2TemplateView.format_ts
|
|
|
|
return jinja_env
|
2014-01-28 16:41:19 -08:00
|
|
|
|
2014-01-31 10:04:21 -08:00
|
|
|
def render_to_string(self, **kwargs):
|
2014-01-28 16:41:19 -08:00
|
|
|
template = self.jinja_env.get_template(self.template_file)
|
|
|
|
|
2014-01-31 10:04:21 -08:00
|
|
|
template_result = template.render(**kwargs)
|
2014-01-28 16:41:19 -08:00
|
|
|
|
2014-01-31 10:04:21 -08:00
|
|
|
return template_result
|
2014-01-28 16:41:19 -08:00
|
|
|
|
2014-01-31 10:04:21 -08:00
|
|
|
def render_response(self, **kwargs):
|
|
|
|
template_result = self.render_to_string(**kwargs)
|
|
|
|
return wbrequestresponse.WbResponse.text_response(str(template_result), content_type = 'text/html; charset=utf-8')
|
2014-01-28 16:41:19 -08:00
|
|
|
|
|
|
|
|
2014-01-31 10:04:21 -08:00
|
|
|
# Filters
|
|
|
|
@staticmethod
|
|
|
|
def format_ts(value, format='%a, %b %d %Y %H:%M:%S'):
|
|
|
|
value = utils.timestamp_to_datetime(value)
|
|
|
|
return time.strftime(format, value)
|
2014-01-28 20:18:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-31 10:04:21 -08:00
|
|
|
# cdx index view
|
2014-01-28 20:18:47 -08:00
|
|
|
|
2014-01-31 10:04:21 -08:00
|
|
|
#=================================================================
|
|
|
|
# html captures 'calendar' view
|
|
|
|
#=================================================================
|
|
|
|
class J2HtmlCapturesView(J2TemplateView):
|
|
|
|
def render_response(self, wbrequest, cdx_lines):
|
|
|
|
return J2TemplateView.render_response(self,
|
|
|
|
cdx_lines = list(cdx_lines),
|
|
|
|
url = wbrequest.wb_url.url,
|
|
|
|
prefix = wbrequest.wb_prefix)
|
2014-01-28 20:18:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
#=================================================================
|
2014-01-31 10:04:21 -08:00
|
|
|
# stream raw cdx text
|
|
|
|
#=================================================================
|
|
|
|
class TextCapturesView:
|
|
|
|
def render_response(self, wbrequest, cdx_lines):
|
|
|
|
cdx_lines = imap(lambda x: str(x) + '\n', cdx_lines)
|
|
|
|
return wbrequestresponse.WbResponse.text_stream(cdx_lines)
|
|
|
|
|
|
|
|
|
|
|
|
|