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

import filters
import parser.FoostacheParser as FoostacheParser
import parser.FoostacheParserVisitor as FoostacheParserVisitor


class PathError(BaseException):
    pass


class Visitor(FoostacheParserVisitor.FoostacheParserVisitor):
    TYPES = {
        "string": (str, unicode),
        "array": (list,),
        "object": (dict,),
        "number": (int, float),
        "boolean": (bool,),
        "null": (type(None),)
    }

    @staticmethod
    def _is_type(value, type_string):
        assert type_string in Visitor.TYPES
        return type(value) in Visitor.TYPES[type_string]

    FILTERMAP = {
        "html": filters.html5,
        "uri": filters.uri_component,
        "jss": filters.js_string
    }

    def __init__(self, context):
        self._contexts = list()
        self._contexts.append(context)
        self._filters = list()
        self._iterates = list()

    # Visit a parse tree produced by FoostacheParser#template.
    def visitTemplate(self, ctx):
        return self.visit(ctx.statements())

    # Visit a parse tree produced by FoostacheParser#statements.
    def visitStatements(self, ctx):
        results = list()
        for statement in ctx.statement():
            results.append(self.visit(statement))
        return u''.join(results)

    # Visit a parse tree produced by FoostacheParser#statement.
    def visitStatement(self, ctx):
        return self.visitChildren(ctx)

    # Visit a parse tree produced by FoostacheParser#rawText.
    def visitRawText(self, ctx):
        return ctx.getText()

    # Visit a parse tree produced by FoostacheParser#literalText.
    def visitLiteral(self, ctx):
        return ctx.literalText().getText()

    def applyFilters(self, s):
        for f in reversed(self._filters):
            if f:
                s = f(s)
        return s

    # Visit a parse tree produced by FoostacheParser#stringField.
    def visitStringField(self, ctx):
        value = self.visit(ctx.path())
        for f in ctx.inlineFilter():
            g = Visitor.FILTERMAP.get(self.visit(f), None)
            if g:
                value = g(value)
        value = self.applyFilters(value)
        return value

    # Visit a parse tree produced by FoostacheParser#numberField.
    def visitNumberField(self, ctx):
        value = self.visit(ctx.path())
        value = self.visit(ctx.numberFormat()).format(value)
        for f in ctx.inlineFilter():
            g = Visitor.FILTERMAP.get(self.visit(f), None)
            if g:
                value = g(value)
        value = self.applyFilters(value)
        return unicode(value)

    def visitNumberFormat(self, ctx):
        # PERCENT (flags=ZERO)? (width=PINTEGERN)? (DOTN precision=PINTEGERN)? specifier=NUMBER_SPECIFIER
        if ctx.specifier.text == u"d":
            if ctx.width:
                if ctx.flags:
                    return "{{:0{}d}}".format(ctx.width.text)
                else:<commit_msg>pep8 cleanup renamed applyFunction to apply_function<commit_after># -*- coding: utf-8 -*-

import filters
import parser.FoostacheParser as FoostacheParser
import parser.FoostacheParserVisitor as FoostacheParserVisitor


class PathError(BaseException):
    pass


class Visitor(FoostacheParserVisitor.FoostacheParserVisitor):
    TYPES = {
        "string": (str, unicode),
        "array": (list,),
        "object": (dict,),
        "number": (int, float),
        "boolean": (bool,),
        "null": (type(None),)
    }

    @staticmethod
    def _is_type(value, type_string):
        assert type_string in Visitor.TYPES
        return type(value) in Visitor.TYPES[type_string]

    FILTERMAP = {
        "html": filters.html5,
        "uri": filters.uri_component,
        "jss": filters.js_string
    }

    def __init__(self, context):
        self._contexts = list()
        self._contexts.append(context)
        self._filters = list()
        self._iterates = list()

    # Visit a parse tree produced by FoostacheParser#template.
    def visitTemplate(self, ctx):
        return self.visit(ctx.statements())

    # Visit a parse tree produced by FoostacheParser#statements.
    def visitStatements(self, ctx):
        results = list()
        for statement in ctx.statement():
            results.append(self.visit(statement))
        return u''.join(results)

    # Visit a parse tree produced by FoostacheParser#statement.
    def visitStatement(self, ctx):
        return self.visitChildren(ctx)

    # Visit a parse tree produced by FoostacheParser#rawText.
    def visitRawText(self, ctx):
        return ctx.getText()

    # Visit a parse tree produced by FoostacheParser#literalText.
    def visitLiteral(self, ctx):
        return ctx.literalText().getText()

    def apply_filters(self, s):
        for f in reversed(self._filters):
            if f:
                s = f(s)
        return s

    # Visit a parse tree produced by FoostacheParser#stringField.
    def visitStringField(self, ctx):
        value = self.visit(ctx.path())
        for f in ctx.inlineFilter():
            g = Visitor.FILTERMAP.get(self.visit(f), None)
            if g:
                value = g(value)
        value = self.apply_filters(value)
        return value

    # Visit a parse tree produced by FoostacheParser#numberField.
    def visitNumberField(self, ctx):
        value = self.visit(ctx.path())
        value = self.visit(ctx.numberFormat()).format(value)
        for f in ctx.inlineFilter():
            g = Visitor.FILTERMAP.get(self.visit(f), None)
            if g:
                value = g(value)
        value = self.apply_filters(value)
        return unicode(value)

    def visitNumberFormat(self, ctx):
        # PERCENT (flags=ZERO)? (width=PINTEGERN)? (DOTN precision=PINTEGERN)? specifier=NUMBER_SPECIFIER
        if ctx.specifier.text == u"d":
            if ctx.width:
                if ctx.flags:
                    return "{{:0{}d}}".format(ctx.width.text)
                else:
{'fraction_non_alphanumeric': 0.10839745734359317, 'fraction_numerical': 0.00117096018735363, 'mean_word_length': 2.3703494926719277, 'pattern_counts': {'":': 20, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2089131', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1714, 'n_words': 438}