def parse_tickers()

in src/backend/domain/services/steps/utils.py [0:0]


def parse_tickers(query: str, primary_provider_id: str) -> Tuple[Dict[str, Any], Dict[str, Any], Dict[str, Any]]:
    symbols, supp_symbols, synth_formulas, supp_synth_formulas = [], [], {}, {}
    available_operations = ["*", "/", "+", "-"]
    special_symbols = [":"]
    special_symbols_not_to_remove = ["@", "."]
    quote_special_symbols = ["/"]
    quotes = ['"', "'", "‘", "’", "`"]
    quote_counter = 0

    record_symbol = ""

    for item in [i.strip() for i in query.split(",")]:
        is_supp = check_if_supplementary(item)

        if is_supp:
            item = remove_supp_parentheses(item)

        item_true_symbols = []
        item_symbols = {}
        item_supp_symbols = {}
        is_synth = False

        if "=" in item:
            synth_name, synth_formula = item.split("=")
        else:
            synth_name, synth_formula = None, item

        for char in synth_formula:
            if char in quotes:
                if quote_counter == 0:
                    quote_counter = 1
                    continue
                else:
                    quote_counter = 0
                    item_true_symbols.append(record_symbol)
                    record_symbol = ""
                    continue

            if quote_counter == 1:
                record_symbol += char
                continue

            if (
                char.isalpha()
                or char.isdigit()
                or char in special_symbols
                or char in special_symbols_not_to_remove
                or quote_counter == 1
                and char in quote_special_symbols
            ):
                record_symbol += char
                continue

            if char in available_operations and quote_counter == 0:
                is_synth = True

            if char in available_operations and record_symbol:
                if check_digit_ticker(record_symbol):
                    item_true_symbols.append(record_symbol)
                record_symbol = ""

        if record_symbol != "":
            item_true_symbols.append(record_symbol)
            record_symbol = ""

        for quote in quotes:
            synth_formula = synth_formula.replace(quote, "")

        for true_symbol in item_true_symbols:
            # if "@" not in true_symbol:
            #     true_symbol_with_provider = f"{true_symbol}@{primary_provider_id}"
            #     # Find all tickers without @ symbol and replace them with ticker@primary_provider_id
            #     synth_formula = re.compile(f"{true_symbol}(?!@)").sub(true_symbol_with_provider, synth_formula)
            #     true_symbol = true_symbol_with_provider

            tmp = true_symbol

            for symbol in special_symbols:
                tmp = tmp.replace(symbol, "")
            for symbol in quote_special_symbols:
                tmp = tmp.replace(symbol, "")
            if tmp[0].isdigit():
                tmp = "_" + tmp

            tmp = tmp.replace("@", "_")
            synth_formula = synth_formula.replace(true_symbol, tmp)

            if is_supp:
                item_supp_symbols[true_symbol] = tmp
            else:
                item_symbols[true_symbol] = tmp

        if is_synth:
            if synth_name is None:
                synth_name = "".join(item_symbols.values())

            if is_supp:
                supp_synth_formulas[synth_name.strip()] = synth_formula.strip()
            else:
                synth_formulas[synth_name.strip()] = synth_formula.strip()

        symbols.extend(item_symbols.keys())
        supp_symbols.extend(item_supp_symbols.keys())

    symbols = list(dict.fromkeys(symbols))
    supp_symbols = list(dict.fromkeys(supp_symbols))
    provider_to_ticker = get_symbol_providers(symbols, primary_provider_id)
    provider_to_supp_ticker = get_symbol_providers(supp_symbols, primary_provider_id)

    all_symbols = {
        "tradable": symbols,
        "supplementary": supp_symbols,
    }

    all_synth_formulas = {
        "tradable": synth_formulas,
        "supplementary": supp_synth_formulas,
    }

    all_provider_to_ticker = {
        "tradable": provider_to_ticker,
        "supplementary": provider_to_supp_ticker,
    }

    return all_symbols, all_synth_formulas, all_provider_to_ticker