# Path: django_test_tools/templatetags/dtt_filters.py
# @register.filter(name='to_snake_case')
# def to_snake_case(name):
#     s1 = first_cap_re.sub(r'\1_\2', name)
#     return all_cap_re.sub(r'\1_\2', s1).lower()


from django.template.loader import render_to_string
from jinja2 import Environment, PackageLoader, select_autoescape
from ..templatetags.dtt_filters import to_snake_case

class SerializerTestGenerator(object):

    def __init__(self):
        self.env = Environment(
            loader=PackageLoader('django_test_tools', 'templates'),
            autoescape=select_autoescape(['html', 'j2'])
        )
        self.env.filters['to_snake_case'] = to_snake_case

        self.template_name = 'django_test_tools/test_serializers.py.j2'
        self.template = self.env.get_template(self.template_name)

    def print(self, serializer_info, filename):
        rendered = self.template.render(serializer_info)
        with open(filename, 'w', encoding='utf-8') as file:
            file.write(rendered)


class GenericTemplateWriter(object):

    def __init__(self, template_name, **kwargs):
        trim_blocks = kwargs.get('trim_blocks', False)
        lstrip_blocks = kwargs.get('lstrip_blocks', False)
        self.env = Environment(
            loader=PackageLoader('django_test_tools', 'templates'),
            autoescape=select_autoescape(['html', ],),
            trim_blocks=trim_blocks,
            lstrip_blocks=lstrip_blocks
        )

# Path: django_test_tools/templatetags/dtt_filters.py
# @register.filter(name='to_snake_case')
# def to_snake_case(name):
#     s1 = first_cap_re.sub(r'\1_\2', name)
#     return all_cap_re.sub(r'\1_\2', s1).lower()


# Path: django_test_tools/generators/crud_generator.py
from django.template.loader import render_to_string
from jinja2 import Environment, PackageLoader, select_autoescape
from ..templatetags.dtt_filters import to_snake_case


class SerializerTestGenerator(object):

    def __init__(self):
        self.env = Environment(
            loader=PackageLoader('django_test_tools', 'templates'),
            autoescape=select_autoescape(['html', 'j2'])
        )
        self.env.filters['to_snake_case'] = to_snake_case

        self.template_name = 'django_test_tools/test_serializers.py.j2'
        self.template = self.env.get_template(self.template_name)

    def print(self, serializer_info, filename):
        rendered = self.template.render(serializer_info)
        with open(filename, 'w', encoding='utf-8') as file:
            file.write(rendered)


class GenericTemplateWriter(object):

    def __init__(self, template_name, **kwargs):
        trim_blocks = kwargs.get('trim_blocks', False)
        lstrip_blocks = kwargs.get('lstrip_blocks', False)
        self.env = Environment(
            loader=PackageLoader('django_test_tools', 'templates'),
            autoescape=select_autoescape(['html', ],),
            trim_blocks=trim_blocks,
            lstrip_blocks=lstrip_blocks
        )

        self.env.filters['to_snake_case'] = to_snake_case