<commit_before>from morepath import generic
from .request import Request, Response
from reg import PredicateMatcher, Predicate
import json


class View(object):
    def __init__(self, func, render, permission):
        self.func = func
        self.render = render
        self.permission = permission

    def __call__(self, request, model):
        return self.func(request, model)


# XXX what happens if predicates is None for one registration
# but filled for another?
def register_view(registry, model, view, render=None, permission=None,
                  predicates=None):
    if permission is not None:
        # instantiate permission class so it can be looked up using reg
        permission = permission()
    registration = View(view, render, permission)
    if predicates is not None:
        matcher = registry.exact(generic.view, (Request, model))
        if matcher is None:
            predicate_info = registry.exact('predicate_info', ())
            predicate_info.sort()
            matcher = PredicateMatcher(
                [predicate for (order, predicate) in predicate_info])
        matcher.register(predicates, registration)
        registration = matcher
    registry.register(generic.view, (Request, model), registration)


def register_predicate(registry, name, order, default, index, calc):
    predicate_info = registry.exact('predicate_info', ())
    if predicate_info is None:
        predicate_info = []
        registry.register('predicate_info', (), predicate_info)
    predicate_info.append((order, Predicate(name, index, calc, default)))


def render_json(content):
    """Take dict/list/string/number content and return application/json response.
    """
    response = Response(json.dumps(content))
    response.content_type = 'application/json'
    return response


def render_html(content):
    """Take string and return text/html response.
    """
    response = Response(content)
    response.content_type = 'text/html'
    return response
<commit_msg>Factor out getting predicate specific registration into its own function.<commit_after>from morepath import generic
from .request import Request, Response
from reg import PredicateMatcher, Predicate
import json


class View(object):
    def __init__(self, func, render, permission):
        self.func = func
        self.render = render
        self.permission = permission

    def __call__(self, request, model):
        return self.func(request, model)


# XXX what happens if predicates is None for one registration
# but filled for another?
def register_view(registry, model, view, render=None, permission=None,
                  predicates=None):
    if permission is not None:
        # instantiate permission class so it can be looked up using reg
        permission = permission()
    registration = View(view, render, permission)
    if predicates is not None:
        registration = get_predicate_registration(registry, model,
                                                  predicates, registration)
    registry.register(generic.view, (Request, model), registration)


def get_predicate_registration(registry, model, predicates, registration):
    matcher = registry.exact(generic.view, (Request, model))
    if matcher is None:
        predicate_info = registry.exact('predicate_info', ())
        predicate_info.sort()
        matcher = PredicateMatcher(
            [predicate for (order, predicate) in predicate_info])
    matcher.register(predicates, registration)
    return matcher

def register_predicate(registry, name, order, default, index, calc):
    predicate_info = registry.exact('predicate_info', ())
    if predicate_info is None:
        predicate_info = []
        registry.register('predicate_info', (), predicate_info)
    predicate_info.append((order, Predicate(name, index, calc, default)))


def render_json(content):
    """Take dict/list/string/number content and return application/json response.
    """
    response = Response(json.dumps(content))
    response.content_type = 'application/json'
    return response


def render_html(content):
    """Take string and return text/html response.
    """
    response = Response(content)
    response.content_type = 'text/html'
    return response

{'fraction_non_alphanumeric': 0.08968609865470852, 'fraction_numerical': 0.0, 'mean_word_length': 3.3333333333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6226217', 'n_tokens_mistral': 1113, 'n_tokens_neox': 1076, 'n_words': 341}