<commit_before># retrieved from: https://gist.github.com/zyegfryed/918403
# used for creating pdf files to be served using django

# -*- coding: utf-8 -*-
import codecs
import subprocess
from fdfgen import forge_fdf

from django.template import Template, loader
from django.template.loader import find_template, LoaderOrigin


class PdfTemplateError(Exception):
    pass


class PdfTemplate(Template):

    def __init__(self, template_string, origin=None, name='<Unknown Template>'):
        self.origin = origin

    def render(self, context):
        context = context.items()
        output, err = self.fill_form(context, self.origin.name)
        if err:
            raise PdfTemplateError(err)
        return output

    def fill_form(self, fields, src, pdftk_bin=None):
        if pdftk_bin is None:
            from django.conf import settings
            assert hasattr(settings, 'PDFTK_BIN'), "PDF generation requires pdftk (http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/). Edit your PDFTK_BIN settings accordingly."
            assert len(settings.PDFTK_BIN) > 0
            pdftk_bin = settings.PDFTK_BIN

        fdf_stream = forge_fdf(fdf_data_strings=fields)

        cmd = [
            pdftk_bin,
            src,
            'fill_form',
            '-',
            'output',
            '-',
            'flatten',
        ]
        cmd = ' '.join(cmd)

        try:
            process = subprocess.Popen(cmd, stdin=subprocess.PIPE,
                                       stdout=subprocess.PIPE, shell=True)
            return process.communicate(input=fdf_stream)
        except OSError:
            return None


def get_template_from_string(source, origin=None, name=None):
    """
    Returns a compiled Template object for the given template code,
    handling template inheritance recursively.
    """
    if name and name.endswith('.pdf'):
        return PdfTemplate('pdf', origin, name)
    return Template(source, origin, name)
loader.get_template_from_string = get_template_from_string


def make_origin(display_name, loader, name, dirs):
    # Always return an Origin object, because PDFTemplate need it to render
    # the PDF Form file.
    return LoaderOrigin(display_name, loader, name, dirs)
loader.make_origin = make_origin


def get_template(template_name):
    """
    Returns a compiled Template object for the given template name,
    handling template inheritance recursively.
    """

    def strict_errors(exception):
        raise exception

    def fake_strict_errors(exception):
        return (u'', -1)

    # Loading hacks
    # Ignore UnicodeError, due to PDF file read
    codecs.register_error('strict', fake_strict_errors)
    # --//--
    template, origin = find_template(template_name)
    # Loading hacks
    codecs.register_error('strict', strict_errors)
    # --//--
    return template
<commit_msg>fixing import error for pdf<commit_after># retrieved from: https://gist.github.com/zyegfryed/918403
# used for creating pdf files to be served using django

# -*- coding: utf-8 -*-
import codecs
import subprocess
from fdfgen import forge_fdf

from django.template import Template, loader
from django.template.loader import get_template as gtl, LoaderOrigin


class PdfTemplateError(Exception):
    pass


class PdfTemplate(Template):

    def __init__(self, template_string, origin=None, name='<Unknown Template>'):
        self.origin = origin

    def render(self, context):
        context = context.items()
        output, err = self.fill_form(context, self.origin.name)
        if err:
            raise PdfTemplateError(err)
        return output

    def fill_form(self, fields, src, pdftk_bin=None):
        if pdftk_bin is None:
            from django.conf import settings
            assert hasattr(settings, 'PDFTK_BIN'), "PDF generation requires pdftk (http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/). Edit your PDFTK_BIN settings accordingly."
            assert len(settings.PDFTK_BIN) > 0
            pdftk_bin = settings.PDFTK_BIN

        fdf_stream = forge_fdf(fdf_data_strings=fields)

        cmd = [
            pdftk_bin,
            src,
            'fill_form',
            '-',
            'output',
            '-',
            'flatten',
        ]
        cmd = ' '.join(cmd)

        try:
            process = subprocess.Popen(cmd, stdin=subprocess.PIPE,
                                       stdout=subprocess.PIPE, shell=True)
            return process.communicate(input=fdf_stream)
        except OSError:
            return None


def get_template_from_string(source, origin=None, name=None):
    """
    Returns a compiled Template object for the given template code,
    handling template inheritance recursively.
    """
    if name and name.endswith('.pdf'):
        return PdfTemplate('pdf', origin, name)
    return Template(source, origin, name)
loader.get_template_from_string = get_template_from_string


def make_origin(display_name, loader, name, dirs):
    # Always return an Origin object, because PDFTemplate need it to render
    # the PDF Form file.
    return LoaderOrigin(display_name, loader, name, dirs)
loader.make_origin = make_origin


def get_template(template_name):
    """
    Returns a compiled Template object for the given template name,
    handling template inheritance recursively.
    """

    def strict_errors(exception):
        raise exception

    def fake_strict_errors(exception):
        return (u'', -1)

    # Loading hacks
    # Ignore UnicodeError, due to PDF file read
    codecs.register_error('strict', fake_strict_errors)
    # --//--
    template = gtl(template_name)
    # Loading hacks
    codecs.register_error('strict', strict_errors)
    # --//--
    return template

{'fraction_non_alphanumeric': 0.0913377575969263, 'fraction_numerical': 0.0031435557107928748, 'mean_word_length': 2.620101137800253, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2095827', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1623, 'n_words': 443}