# 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

def get_telingo_output(telingo_in_fnames: list, num_solutions: int=0):

    t = ['telingo', '-n {}'.format(num_solutions), '-Wnone']
    t.extend(telingo_in_fnames)
    process_ = subprocess.Popen(t, stdout=subprocess.PIPE)
    telingo_output = process_.communicate()[0]
    # clingo_output = subprocess.check_output()
    meta_data = {}
    #attribute_defs = {}
    #temporal_decs = {}
    for fname in telingo_in_fnames:
        with open(fname, 'r') as f:
            telingo_rules = f.read().splitlines()

# 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


def get_telingo_output(telingo_in_fnames: list, num_solutions: int=0):

    t = ['telingo', '-n {}'.format(num_solutions), '-Wnone']
    t.extend(telingo_in_fnames)
    process_ = subprocess.Popen(t, stdout=subprocess.PIPE)
    telingo_output = process_.communicate()[0]
    # clingo_output = subprocess.check_output()
    meta_data = {}
    #attribute_defs = {}
    #temporal_decs = {}
    for fname in telingo_in_fnames:
        with open(fname, 'r') as f:
            telingo_rules = f.read().splitlines()

            f_meta_data = parse_pwe_meta_data(telingo_rules)