<commit_before>from __future__ import absolute_import

import collections
import os

import yaml
import markdown
import html2text
import six

from django.conf import settings
from django.core.mail import EmailMessage, EmailMultiAlternatives
from django.template import Template, Context, TemplateDoesNotExist
from django.utils.functional import cached_property

from .loaders import get_template_source

VALID_KEYS = set(['to', 'cc', 'bcc', 'subject', 'attachments',
                  'from', 'headers', 'content-type'])
REQUIRED_KEYS = set(['to', 'subject', 'content-type'])

TYPE_PLAIN = 'plain'
TYPE_MARKDOWN = 'markdown'
TYPE_HTML = 'html'

VALID_CONTENT_TYPE = set([TYPE_PLAIN, TYPE_MARKDOWN, TYPE_HTML])


class TemplateYAMLLoader(yaml.SafeLoader):
    """
    Dirty hack to ignore curly braces when parsing yaml
    """

    def fetch_flow_mapping_start(self):
        """
        { has been encountered
        """
        return self.fetch_plain()

    def fetch_flow_mapping_end(self):
        """
        } has been encountered
        """
        return self.fetch_plain()


class OgmiosError(Exception):
    pass


class EmailTemplateError(OgmiosError):
    pass


class EmailSender(object):

    def __init__(self, filename, context, template_loader=None, attachments=None):
        self.filename = filename
        self.context = context
        self.template_loader = template_loader
        self.attachments = attachments or []

    def get_from(self):
        from_addr = self.data.get('from', settings.DEFAULT_FROM_EMAIL)
        return self.render_string(from_addr)

    def render_string(self, string):
        return Template(string).render(Context(self.context))

    def get_template_source(self):
        return loaders.get_template_source(self)

    @cached_property
    def content(self):
        source = self.get_template_source()

        if source is None:
            raise TemplateDoesNotExist(self.filename)

        return source.split('\n---\n')

    @cached_property
    def data(self):
        data = yaml.load(self.content[0], Loader=TemplateYAMLLoader)

        if not (set(data.keys()) <= VALID_KEYS):
            invalid_keys = set(data.keys()) - VALID_KEYS
            raise EmailTemplateError("Unknown keys: {}".format(', '.join(invalid_keys)))<commit_msg>there is no sense in allowing running code when loading yaml<commit_after>from __future__ import absolute_import

import collections
import os

import yaml
import markdown
import html2text
import six

from django.conf import settings
from django.core.mail import EmailMessage, EmailMultiAlternatives
from django.template import Template, Context, TemplateDoesNotExist
from django.utils.functional import cached_property

from .loaders import get_template_source

VALID_KEYS = set(['to', 'cc', 'bcc', 'subject', 'attachments',
                  'from', 'headers', 'content-type'])
REQUIRED_KEYS = set(['to', 'subject', 'content-type'])

TYPE_PLAIN = 'plain'
TYPE_MARKDOWN = 'markdown'
TYPE_HTML = 'html'

VALID_CONTENT_TYPE = set([TYPE_PLAIN, TYPE_MARKDOWN, TYPE_HTML])


class TemplateYAMLLoader(yaml.SafeLoader):
    """
    Dirty hack to ignore curly braces when parsing yaml
    """

    def fetch_flow_mapping_start(self):
        """
        { has been encountered
        """
        return self.fetch_plain()

    def fetch_flow_mapping_end(self):
        """
        } has been encountered
        """
        return self.fetch_plain()


class OgmiosError(Exception):
    pass


class EmailTemplateError(OgmiosError):
    pass


class EmailSender(object):

    def __init__(self, filename, context, template_loader=None, attachments=None):
        self.filename = filename
        self.context = context
        self.template_loader = template_loader
        self.attachments = attachments or []

    def get_from(self):
        from_addr = self.data.get('from', settings.DEFAULT_FROM_EMAIL)
        return self.render_string(from_addr)

    def render_string(self, string):
        return Template(string).render(Context(self.context))

    def get_template_source(self):
        return loaders.get_template_source(self)

    @cached_property
    def content(self):
        source = self.get_template_source()

        if source is None:
            raise TemplateDoesNotExist(self.filename)

        return source.split('\n---\n')

    @cached_property
    def data(self):
        data = yaml.safe_load(self.content[0], Loader=TemplateYAMLLoader)

        if not (set(data.keys()) <= VALID_KEYS):
            invalid_keys = set(data.keys()) - VALID_KEYS
            raise EmailTemplateError("Unknown keys: {}".format(', '.join(invalid_keys)))
{'fraction_non_alphanumeric': 0.10189165950128977, 'fraction_numerical': 0.0008598452278589854, 'mean_word_length': 3.369014084507042, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1633235', 'n_tokens_mistral': 1368, 'n_tokens_neox': 1320, 'n_words': 323}