<commit_before>"""
Usage:
    mut-redirects <source_path> -o <output>

    -h, --help             List CLI prototype, arguments, and options.
    <source_path>          Path to the file(s) containing redirect rules.
    -o, --output <output>  File path for the output .htaccess file.
"""

# Spec URL: https://docs.google.com/document/d/1oI2boFmtzvbbvt-uQawY9k_gLSLbW7LQO2RjVkvtRgg/edit?ts=57caf48b#

import re
import collections
from typing import List, Optional, Dict
from docopt import docopt

RuleDefinition = collections.namedtuple('RuleDefinition', ('is_temp', 'version', 'old_url', 'new_url', 'is_symlink'))


class RedirectContext:
    def __init__(self) -> None:
        self.rules = []  # type: List[RuleDefinition]
        self.symlinks = []  # type: List[List[str]]
        self.definitions = {}  # type: Dict[str, str]

    def add_definition(self, key: str, value: str) -> None:
        self.definitions[key] = value

    def generate_rule(self, is_temp: bool, version: str, old_url: str, new_url: str, is_symlink: bool = False) -> None:
        context_url = self.definitions['base']

        # if url contains {version} - substitute in the correct version
        old_url_sub = self.rule_substitute(old_url, version)
        new_url_sub = self.rule_substitute(new_url, version)

        # reformatting the old url
        if len(old_url_sub) > 0:
            if old_url_sub[0] != '/':
                old_url_sub = '/' + old_url_sub

            if not is_symlink:
                if old_url_sub[-1] == '/':
                    old_url_sub = old_url_sub[:-1]
                old_url_sub = '/' + context_url + old_url_sub

        new_rule = RuleDefinition(is_temp, version, old_url_sub, new_url_sub, False)

        # check for symlinks
        if len(self.symlinks) > 0:
            for symlink in self.symlinks:
                if version == symlink[1]:
                    self.generate_rule(is_temp, symlink[0], old_url, new_url, True)

        self.rules.append(new_rule)

    def rule_substitute(self, input_string: str, version: str = '') -> str:
        # look for strings between { }
        sub_regex = '{(.*?)}'
        matches = re.findall(sub_regex, input_string, re.DOTALL)
        if (version != ''):
            input_string = input_string.replace('${version}', version)

        input_string = input_string.strip()

        if not matches:
            return input_string

        for match in matches:
            # substitute the definition value for the match
            if match != 'version':
                input_string = input_string.replace('${' + match + '}', self.definitions[match])
        return input_string


def parse_versions(defs: Dict[str, str]) -> List[str]:
    return defs['versions'].split(' ')


def write_to_file(rules: List[RuleDefinition], output_path: str) -> None:
    with open(output_path + '/htaccess_test.txt', 'w') as f:
        for rule in rules:
            line = 'Redirect '

            if rule.is_temp:
                line += '302 '
            else:
                line += '301 '

            line += str(rule.old_url) + ' ' + str(rule.new_url)
            f.write(line)<commit_msg>mut-redirects: Don't append '/htaccess_test.txt' to output filename<commit_after>"""
Usage:
    mut-redirects <source_path> -o <output>

    -h, --help             List CLI prototype, arguments, and options.
    <source_path>          Path to the file(s) containing redirect rules.
    -o, --output <output>  File path for the output .htaccess file.
"""

# Spec URL: https://docs.google.com/document/d/1oI2boFmtzvbbvt-uQawY9k_gLSLbW7LQO2RjVkvtRgg/edit?ts=57caf48b#

import re
import collections
from typing import List, Optional, Dict
from docopt import docopt

RuleDefinition = collections.namedtuple('RuleDefinition', ('is_temp', 'version', 'old_url', 'new_url', 'is_symlink'))


class RedirectContext:
    def __init__(self) -> None:
        self.rules = []  # type: List[RuleDefinition]
        self.symlinks = []  # type: List[List[str]]
        self.definitions = {}  # type: Dict[str, str]

    def add_definition(self, key: str, value: str) -> None:
        self.definitions[key] = value

    def generate_rule(self, is_temp: bool, version: str, old_url: str, new_url: str, is_symlink: bool = False) -> None:
        context_url = self.definitions['base']

        # if url contains {version} - substitute in the correct version
        old_url_sub = self.rule_substitute(old_url, version)
        new_url_sub = self.rule_substitute(new_url, version)

        # reformatting the old url
        if len(old_url_sub) > 0:
            if old_url_sub[0] != '/':
                old_url_sub = '/' + old_url_sub

            if not is_symlink:
                if old_url_sub[-1] == '/':
                    old_url_sub = old_url_sub[:-1]
                old_url_sub = '/' + context_url + old_url_sub

        new_rule = RuleDefinition(is_temp, version, old_url_sub, new_url_sub, False)

        # check for symlinks
        if len(self.symlinks) > 0:
            for symlink in self.symlinks:
                if version == symlink[1]:
                    self.generate_rule(is_temp, symlink[0], old_url, new_url, True)

        self.rules.append(new_rule)

    def rule_substitute(self, input_string: str, version: str = '') -> str:
        # look for strings between { }
        sub_regex = '{(.*?)}'
        matches = re.findall(sub_regex, input_string, re.DOTALL)
        if (version != ''):
            input_string = input_string.replace('${version}', version)

        input_string = input_string.strip()

        if not matches:
            return input_string

        for match in matches:
            # substitute the definition value for the match
            if match != 'version':
                input_string = input_string.replace('${' + match + '}', self.definitions[match])
        return input_string


def parse_versions(defs: Dict[str, str]) -> List[str]:
    return defs['versions'].split(' ')


def write_to_file(rules: List[RuleDefinition], output_path: str) -> None:
    with open(output_path, 'w') as f:
        for rule in rules:
            line = 'Redirect '

            if rule.is_temp:
                line += '302 '
            else:
                line += '301 '

            line += str(rule.old_url) + ' ' + str(rule.new_url)
            f.write(line)
{'fraction_non_alphanumeric': 0.11974161020954782, 'fraction_numerical': 0.006932409012131715, 'mean_word_length': 2.351636747624076, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 27, 'https://': 2, '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': '220082', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1975, 'n_words': 486}