# Path: PW_explorer/helper.py
# def preprocess_clingo_output(clingo_raw_output: list):
# 
#     start = 0
#     for i, line in enumerate(clingo_raw_output):
#         if line.strip() == 'Solving...':
#             start = i + 1
#             break
# 
#     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_clingo_output,
)
from .meta_data_parser import parse_pwe_meta_data

def get_clingo_output(clingo_in_fnames: list, num_solutions: int=0, show_warnings=False, other_args: list=None):

    t = ['clingo', '-n {}'.format(num_solutions)]
    # TODO The thing below won't do much if warnings are sent to stderr which I suspect they are.
    # TODO Also need to shift preprocessing burden to load_world instead
    if not show_warnings:
        t.append('-Wnone')
    t.extend(clingo_in_fnames)
    if other_args:
        t.extend(other_args)
    process_ = subprocess.Popen(t, stdout=subprocess.PIPE)
    clingo_output = process_.communicate()[0]
    # clingo_output = subprocess.check_output()
    meta_data = {}
    #attribute_defs = {}
    #temporal_decs = {}
    for fname in clingo_in_fnames:
        with open(fname, 'r') as f:
            clingo_rules = f.read().splitlines()

# Path: PW_explorer/helper.py
# def preprocess_clingo_output(clingo_raw_output: list):
# 
#     start = 0
#     for i, line in enumerate(clingo_raw_output):
#         if line.strip() == 'Solving...':
#             start = i + 1
#             break
# 
#     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_clingo.py
import os
import subprocess as subprocess
from .helper import (
    preprocess_clingo_output,
)
from .meta_data_parser import parse_pwe_meta_data


def get_clingo_output(clingo_in_fnames: list, num_solutions: int=0, show_warnings=False, other_args: list=None):

    t = ['clingo', '-n {}'.format(num_solutions)]
    # TODO The thing below won't do much if warnings are sent to stderr which I suspect they are.
    # TODO Also need to shift preprocessing burden to load_world instead
    if not show_warnings:
        t.append('-Wnone')
    t.extend(clingo_in_fnames)
    if other_args:
        t.extend(other_args)
    process_ = subprocess.Popen(t, stdout=subprocess.PIPE)
    clingo_output = process_.communicate()[0]
    # clingo_output = subprocess.check_output()
    meta_data = {}
    #attribute_defs = {}
    #temporal_decs = {}
    for fname in clingo_in_fnames:
        with open(fname, 'r') as f:
            clingo_rules = f.read().splitlines()

            f_meta_data = parse_pwe_meta_data(clingo_rules)