<commit_before>import re
import os

from django.test import TestCase

from .utils import get_template_dirs

re_url = re.compile(r'\shref="(?P<url>(?!https?:|mailto:|\?|{|#)[^"]*)"')

class TestTemplates(TestCase):
    def assertValidURLs(self, filename):
        with open(filename) as f:
            urls = [m.group('url') for m in re_url.finditer(f.read())]

        self.failIf(urls, "%s contains hardcoded URLs: %r" % (
            filename,
            urls,
        ))

    idx = 0
    for x in get_template_dirs():
        for root, _, filenames in os.walk(x):
            for y in filenames:
                def wrapper(self, filename=os.path.join(root, y)):
                    self.assertValidURLs(filename)
                idx += 1
                locals()['test_template_idx_%04d' % idx] = wrapper<commit_msg>Use a metaclass instead of dirty dict()-mangling.<commit_after>import re
import os

from django.test import TestCase
from django.utils.text import slugify

from .utils import get_template_dirs

re_url = re.compile(r'\shref="(?P<url>(?!https?:|mailto:|\?|{|#)[^"]*)"')

class TestTemplatesMeta(type):
    def __new__(cls, name, bases, attrs):
        def generate(template):
            def fn(self):
                self.assertValidURLs(template)
            return fn

        for x in get_template_dirs():
            for root, _, templates in os.walk(x):
                for y in templates:
                    template = os.path.join(root, y)

                    attrs['test_%s' % slugify(template)] = generate(template)

        return super(TestTemplatesMeta, cls).__new__(cls, name, bases, attrs)

class TestTemplates(TestCase):
    __metaclass__ = TestTemplatesMeta

    def assertValidURLs(self, template):
        with open(template) as f:
            urls = [m.group('url') for m in re_url.finditer(f.read())]

        self.failIf(urls, "%s contains hardcoded URLs: %r" % (
            template,
            urls,
        ))
{'fraction_non_alphanumeric': 0.12876852324987226, 'fraction_numerical': 0.002043944813490036, 'mean_word_length': 2.3758620689655174, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3708227', 'n_tokens_mistral': 630, 'n_tokens_neox': 606, 'n_words': 149}