<commit_before># -*- coding: utf-8 -*-

import os
from inspect import getargspec

from werkzeug.wrappers import Response

from tableutils import Table

_CUR_PATH = os.path.dirname(os.path.abspath(__file__))
_CSS_PATH = _CUR_PATH + '/../_clastic_assets/common.css'
try:
    _STYLE_CONTENT = open(_CSS_PATH).read()
except:
    import pdb;pdb.set_trace()
    _STYLE_CONTENT = ''


class TabularRender(object):
    _html_doctype = '<!doctype html>'
    _html_wrapper, _html_wrapper_close = '<html>', '</html>'
    _html_table_tag = '<table class="clastic-atr-table">'
    _html_style_content = _STYLE_CONTENT

    def __init__(self, max_depth=4, orientation='auto'):
        self.max_depth = max_depth
        self.orientation = orientation

    def _html_format_ep(self, route):
        # TODO: callable object endpoints?
        module_name = route.endpoint.__module__
        try:
            func_name = route.endpoint.func_name
        except:
            func_name = repr(route.endpoint)
        func_name = func_name.replace('<', '(').replace('>', ')')
        args, _, _, _ = getargspec(route.endpoint)
        argstr = ', '.join(args)
        title = ('<h2><small><sub>%s</sub></small><br/>%s(%s)</h2>'
                 % (module_name, func_name, argstr))
        return title

    def __call__(self, context, _route):
        content_parts = [self._html_wrapper]
        if self._html_style_content:
            content_parts.extend(['<head><style type="text/css">',
                                  self._html_style_content,
                                  '</style></head>'])
        content_parts.append('<body>')
        title = self._html_format_ep(_route)
        content_parts.append(title)
        table = Table.from_data(context, max_depth=self.max_depth)
        table._html_table_tag = self._html_table_tag
        content = table.to_html(max_depth=self.max_depth,
                                orientation=self.orientation)
        content_parts.append(content)
        content_parts.append('</body>')
        content_parts.append(self._html_wrapper_close)<commit_msg>add docstring rendering to render_basic's html branch. handy for little descriptions. there's no truncation, so if the docstring of a function endpoint is too long, just wrap it!<commit_after># -*- coding: utf-8 -*-

import os
import cgi
from inspect import getargspec

from werkzeug.wrappers import Response

from tableutils import Table

_CUR_PATH = os.path.dirname(os.path.abspath(__file__))
_CSS_PATH = _CUR_PATH + '/../_clastic_assets/common.css'
try:
    _STYLE_CONTENT = open(_CSS_PATH).read()
except:
    _STYLE_CONTENT = ''


def escape_html(text):
    return cgi.escape(text, True)


class TabularRender(object):
    _html_doctype = '<!doctype html>'
    _html_wrapper, _html_wrapper_close = '<html>', '</html>'
    _html_table_tag = '<table class="clastic-atr-table">'
    _html_style_content = _STYLE_CONTENT

    def __init__(self, max_depth=4, orientation='auto'):
        self.max_depth = max_depth
        self.orientation = orientation

    def _html_format_ep(self, route):
        # TODO: callable object endpoints?
        module_name = route.endpoint.__module__
        try:
            func_name = route.endpoint.func_name
        except:
            func_name = repr(route.endpoint)
        func_name = func_name.replace('<', '(').replace('>', ')')
        args, _, _, _ = getargspec(route.endpoint)
        argstr = ', '.join(args)

        func_doc = getattr(route.endpoint, '__doc__', '')
        if func_doc:
            escaped_doc = escape_html(func_doc.lstrip())
            html_doc = '<p style="white-space: pre;">%s</p>' % escaped_doc
        else:
            html_doc = '<!-- add a docstring to display a message here! -->'

        title = ('<h2><small><sub>%s</sub></small><br/>%s(%s)</h2>%s'
                 % (module_name, func_name, argstr, html_doc))
        return title

    def __call__(self, context, _route):
        content_parts = [self._html_wrapper]
        if self._html_style_content:
            content_parts.extend(['<head><style type="text/css">',
                                  self._html_style_content,
                                  '</style></head>'])
        content_parts.append('<body>')
        title = self._html_format_ep(_route)
        content_parts.append(title)
        table = Table.from_data(context, max_depth=self.max_depth)
        table._html_table_tag = self._html_table_tag
        content = table.to_html(max_depth=self.max_depth,
                                orientation=self.orientation)
        content_parts.append(content)
        content_parts.append('</body>')
        content_parts.append(self._html_wrapper_close)
{'fraction_non_alphanumeric': 0.12441514249255636, 'fraction_numerical': 0.0017014036580178648, 'mean_word_length': 2.6513975155279503, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6506807', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1433, 'n_words': 268}