<commit_before>"""models Fastly API concepts

No dependencies on Terraform should be in this module"""

from collections import namedtuple
import re
import os

class FastlyVCL:
    @classmethod
    def from_string(cls, content):
        return cls(content.splitlines())

    def __init__(self, lines):
        self._lines = lines

    def __eq__(self, another):
        return self._lines == another._lines

    def __str__(self):
        return "\n".join(self._lines)

    def __repr__(self):
        return "FastlyVCL(%s)" % repr(self._lines)

    def insert(self, section, statement):
        section_start = self._find_section_start(section)
        lines = list(self._lines)
        lines.insert(section_start + 1, '')
        lines.insert(
            section_start + 1,
            '  %s' % statement
        )
        return FastlyVCL(lines)

    def _find_section_start(self, section):
        lookup = r"^( *)sub vcl_%s {" % section
        section_start = None
        for i, line in enumerate(self._lines):
            m = re.match(lookup, line)
            if m:
                section_start = i
                break
        if section_start is None:
            raise FastlyCustomVCLGenerationError("Cannot match %s into main VCL template:\n\n%s" % (lookup, str(self)))
        return section_start

class FastlyVCLSnippet(namedtuple('FastlyVCLSnippet', ['name', 'content', 'type'])):
    """VCL snippets that can be used to augment the default VCL

    Due to Terraform limitations we are unable to pass these directly to the Fastly API, and have to build a whole VCL ourselves.

    Terminology for fields comes from https://docs.fastly.com/api/config#snippet"""

    def insert_include(self, main_vcl):
        # TODO: pass more lines in, and add a comment on where this is coming from
        return main_vcl.insert(self.type, 'include "%s"' % self.name)

class FastlyCustomVCLGenerationError(Exception):
    pass

_directory = os.path.join(os.path.dirname(__file__), 'fastly', 'vcl')
def _read_vcl_file(name):
    with open(os.path.join(_directory, name)) as fp:
        return fp.read()

# taken from https://docs.fastly.com/guides/vcl/mixing-and-matching-fastly-vcl-with-custom-vcl#fastlys-vcl-boilerplate
# Fastly expands #FASTLY macros into generated VCL
MAIN_VCL_TEMPLATE = FastlyVCL.from_string(_read_vcl_file('main.vcl'))

VCL_SNIPPETS = {<commit_msg>Support multiple lines in FastlyVCL.insert<commit_after>"""models Fastly API concepts

No dependencies on Terraform should be in this module"""

from collections import namedtuple
import re
import os

class FastlyVCL:
    @classmethod
    def from_string(cls, content):
        return cls(content.splitlines())

    def __init__(self, lines):
        self._lines = lines

    def __eq__(self, another):
        return self._lines == another._lines

    def __str__(self):
        return "\n".join(self._lines)

    def __repr__(self):
        return "FastlyVCL(%s)" % repr(self._lines)

    def insert(self, section, statements):
        section_start = self._find_section_start(section)
        lines = list(self._lines)
        lines.insert(section_start + 1, '')
        lines[section_start+1:section_start+1] = ['  %s' % s for s in statements]
        return FastlyVCL(lines)

    def _find_section_start(self, section):
        lookup = r"^( *)sub vcl_%s {" % section
        section_start = None
        for i, line in enumerate(self._lines):
            m = re.match(lookup, line)
            if m:
                section_start = i
                break
        if section_start is None:
            raise FastlyCustomVCLGenerationError("Cannot match %s into main VCL template:\n\n%s" % (lookup, str(self)))
        return section_start

class FastlyVCLSnippet(namedtuple('FastlyVCLSnippet', ['name', 'content', 'type'])):
    """VCL snippets that can be used to augment the default VCL

    Due to Terraform limitations we are unable to pass these directly to the Fastly API, and have to build a whole VCL ourselves.

    Terminology for fields comes from https://docs.fastly.com/api/config#snippet"""

    def insert_include(self, main_vcl):
        return main_vcl.insert(self.type, ['include "%s"' % self.name])

class FastlyCustomVCLGenerationError(Exception):
    pass

_directory = os.path.join(os.path.dirname(__file__), 'fastly', 'vcl')
def _read_vcl_file(name):
    with open(os.path.join(_directory, name)) as fp:
        return fp.read()

# taken from https://docs.fastly.com/guides/vcl/mixing-and-matching-fastly-vcl-with-custom-vcl#fastlys-vcl-boilerplate
# Fastly expands #FASTLY macros into generated VCL
MAIN_VCL_TEMPLATE = FastlyVCL.from_string(_read_vcl_file('main.vcl'))

VCL_SNIPPETS = {
{'fraction_non_alphanumeric': 0.10585729499467518, 'fraction_numerical': 0.0010649627263045794, 'mean_word_length': 3.1120840630472855, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '2101325', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1434, 'n_words': 395}