Untitled

                Never    
#END TODO BLOCK

REGEX_EXTENSION = "rgx"
PLACEHOLDER = "~&~&"
COMMENT_PREFIX = "## "
CMD_QUIT = "!q"

REGEX_ENTRY_SPLITTER = " / "
CONFIG_ENTRY_SPLITTER = " = "

#Format: 'key': ['requirement 1', 'requirement 2', ...]; None if no requirements
possible_keys = {
    "ignore_case": None,
    "to_lower": None
}

config = {}

class ConfigQuery:
    def __init__(self, found, data):
        self.found = found
        self.data = data
    def is_bool(self, bool):
        if (self.found and self.data == bool):
            return True
        return False

def load_config(lines):
    lines_cpy = []
    for line in lines:
        if (CONFIG_ENTRY_SPLITTER in line and REGEX_ENTRY_SPLITTER not in line):
            two_part = line.lower().split(CONFIG_ENTRY_SPLITTER)
            if (two_part[0] in possible_keys):
                config [
                        two_part[0]
                    ] = two_part[1]
            else:
                print(f"Ignoring unrecognised key: {two_part[0]}")
        else:
            lines_cpy += [line]
    return lines_cpy

def get_config(key):
    if (key not in config):
        return ConfigQuery(False, None)
    cfg_value = config[key]
    if (cfg_value == "true" or cfg_value == "yes"):
        return ConfigQuery(True, True)
    if (cfg_value == "false" or cfg_value == "no"):
        return ConfigQuery(True, False)
    return ConfigQuery(True, cfg_value)

def sanitise_config():
    cfg_keys = config.keys()
    purge_keys = []
    for key in cfg_keys:
        requirements = possible_keys[key]
        if (requirements != None):
            for requirement in requirements:
                if (requirement not in cfg_keys or
                    cfg_keys[requirement] == False or
                    cfg_keys[requirement] == None):
                    print(f"The option '{key}' requires the '{requirement} to be set, ignoring it'")
                    purge_keys.append(key)
    for key in purge_keys:
        del config[key]

def sanitise_lines(lines):
    lines_copy = []
    for line in lines:
        if (line and not line.isspace() and not line[:len(COMMENT_PREFIX)] == COMMENT_PREFIX):
            stripped = line.strip('\n')
            lines_copy += [stripped]
    return lines_copy

def load_regex_file(filename, encoding="utf-8"):
    rfile  = open(filename, 'r', encoding=encoding)
    rlines = rfile.readlines()
    rlines = sanitise_lines(rlines)
    rlines = load_config(rlines)
    sanitise_config()
    return rlines

def regex_from_lines(rgx):
    regex_dict = {}
    for line_number, line in enumerate(rgx):
        line = line.replace("\/", PLACEHOLDER)
        two_part = line.split(REGEX_ENTRY_SPLITTER)
        two_part[0] = two_part[0].replace(PLACEHOLDER, "\/")
        two_part[1] = two_part[1].replace(PLACEHOLDER, "\/")
        if (len(two_part) == 2):
            try:
                if (get_config("ignore_case").is_bool(True)):
                    pattern = re.compile(two_part[0], re.MULTILINE | re.IGNORECASE)
                else:
                    pattern = re.compile(two_part[0], re.MULTILINE)
                regex_dict[
                    pattern
                ] = two_part[1]
            except re.error as regex_error:
                print(f"Ignoring pattern '{two_part[0]}': {regex_error.msg}")
    return regex_dict

def apply_regex(text, regex):
    for pattern, replacement in regex.items():
        try:
            text = re.sub(pattern, replacement, text)
        except Exception as sub_error:
            print(f"'{pattern}' -> '{replacement}' substitution failed: {sub_error.msg}'")
    if (get_config("to_lower").is_bool(True)):
        return text.lower()
    return text

def launch():
    fnprompt = input("Enter the name of the regex: ")
    if (fnprompt.count('.') < 1):
        fnprompt = fnprompt + '.' + REGEX_EXTENSION
    rgxf = load_regex_file(fnprompt)
    rgx  = regex_from_lines(rgxf)
    while True:
        qprompt = input("> ")
        if (qprompt.lower() == CMD_QUIT):
            exit(0)
        qregexd = apply_regex(qprompt, rgx)
        print(qregexd)

Raw Text