# Path: PW_explorer/helper.py
# def preprocess_telingo_output(clingo_raw_output: list):
#     # TODO Take another look at this to ensure correctness
#     start = 0
#     for i, line in enumerate(clingo_raw_output):
#         if line.strip() == 'Solving...':
#             start = i + 1
# 
#     return clingo_raw_output[start:]
# 
# Path: PW_explorer/meta_data_parser.py
# def parse_pwe_meta_data(asp_rules, silent=False, print_parse_tree=False):
#     if isinstance(asp_rules, str):
#         asp_rules = asp_rules.splitlines()
#     md_comments = preprocess(asp_rules)
#     md_comments = "\n".join(md_comments)
#     parsed_meta_data = parse_meta_data_from_string(md_comments, silent, print_parse_tree)
#     return parsed_meta_data


import os
import subprocess as subprocess
from .helper import (
    preprocess_telingo_output,
)
from .meta_data_parser import parse_pwe_meta_data
            for md_type, md in f_meta_data.items():
                if md_type in meta_data:
                    meta_data[md_type].update(md)
                else:
                    meta_data[md_type] = md

    teling_out_lines = telingo_output.splitlines()
    teling_out_lines = list(map(lambda x: str(x, 'utf-8'), teling_out_lines))


    return teling_out_lines, meta_data


def run_telingo(telingo_rules, num_solutions: int=0):
    """
    :param telingo_rules: string or list of strings
    :param num_solutions:
    :return:
    """

    if isinstance(telingo_rules, str):
        telingo_rules = telingo_rules.splitlines()

    dummy_fname = 'svjsihkankjbyerhoihsyvgjnclsdihcysbfhcbygweincbsydgibwyebcsygdyc.lp4'
    with open(dummy_fname, 'w') as f:
        f.write('\n'.join(telingo_rules))

    telingo_output, meta_data = get_telingo_output([dummy_fname], num_solutions=num_solutions)
    os.remove(dummy_fname)


# Path: PW_explorer/helper.py
# def preprocess_telingo_output(clingo_raw_output: list):
#     # TODO Take another look at this to ensure correctness
#     start = 0
#     for i, line in enumerate(clingo_raw_output):
#         if line.strip() == 'Solving...':
#             start = i + 1
# 
#     return clingo_raw_output[start:]
# 
# Path: PW_explorer/meta_data_parser.py
# def parse_pwe_meta_data(asp_rules, silent=False, print_parse_tree=False):
#     if isinstance(asp_rules, str):
#         asp_rules = asp_rules.splitlines()
#     md_comments = preprocess(asp_rules)
#     md_comments = "\n".join(md_comments)
#     parsed_meta_data = parse_meta_data_from_string(md_comments, silent, print_parse_tree)
#     return parsed_meta_data


# Path: PW_explorer/run_telingo.py
import os
import subprocess as subprocess
from .helper import (
    preprocess_telingo_output,
)
from .meta_data_parser import parse_pwe_meta_data

            for md_type, md in f_meta_data.items():
                if md_type in meta_data:
                    meta_data[md_type].update(md)
                else:
                    meta_data[md_type] = md

    teling_out_lines = telingo_output.splitlines()
    teling_out_lines = list(map(lambda x: str(x, 'utf-8'), teling_out_lines))


    return teling_out_lines, meta_data


def run_telingo(telingo_rules, num_solutions: int=0):
    """
    :param telingo_rules: string or list of strings
    :param num_solutions:
    :return:
    """

    if isinstance(telingo_rules, str):
        telingo_rules = telingo_rules.splitlines()

    dummy_fname = 'svjsihkankjbyerhoihsyvgjnclsdihcysbfhcbygweincbsydgibwyebcsygdyc.lp4'
    with open(dummy_fname, 'w') as f:
        f.write('\n'.join(telingo_rules))

    telingo_output, meta_data = get_telingo_output([dummy_fname], num_solutions=num_solutions)
    os.remove(dummy_fname)


    return preprocess_telingo_output(telingo_output), meta_data ## TODO Change preprocess_clingo_output