# 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 .meta_data_parser import parse_pwe_meta_data

def get_dlv_output(dlv_input_fnames: list, num_solutions: int=0, wfs_mode: bool=False,
                   dlv_max_int: int=None, other_args: list=None):
    """
    :param dlv_input_fnames: list of dlv filepaths
    :param num_solutions: number of solutions to generate. Default: 0 i.e. generate all solutions
    :param wfs_mode: Use the well-founded semantics form
    :param dlv_max_int: Set the -N parameter while running dlv
    :param other_args: Other arguments to pass to dlv. Provide a list of strings eg. ['-n=1', '-N=10', '-silent']
    :return: dlv output and parsed attribute definitions
    """
    t = ['dlv', '-n={}'.format(num_solutions), '-silent']
    if wfs_mode:
        t.append('-wf')
    if dlv_max_int:
        t.append('-N={}'.format(dlv_max_int))
    if other_args:
        t.extend(other_args)
    t.extend(dlv_input_fnames)
    process_ = subprocess.Popen(t, stdout=subprocess.PIPE)
    dlv_output = process_.communicate()[0]
    # dlv_output = subprocess.check_output()
    meta_data = {}
    #attribute_defs = {}
    #temporal_decs = {}
    for fname in dlv_input_fnames:
        with open(fname, 'r') as f:
            dlv_rules = f.read().splitlines()

# 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_dlv.py
import os
import subprocess as subprocess
from .meta_data_parser import parse_pwe_meta_data


def get_dlv_output(dlv_input_fnames: list, num_solutions: int=0, wfs_mode: bool=False,
                   dlv_max_int: int=None, other_args: list=None):
    """
    :param dlv_input_fnames: list of dlv filepaths
    :param num_solutions: number of solutions to generate. Default: 0 i.e. generate all solutions
    :param wfs_mode: Use the well-founded semantics form
    :param dlv_max_int: Set the -N parameter while running dlv
    :param other_args: Other arguments to pass to dlv. Provide a list of strings eg. ['-n=1', '-N=10', '-silent']
    :return: dlv output and parsed attribute definitions
    """
    t = ['dlv', '-n={}'.format(num_solutions), '-silent']
    if wfs_mode:
        t.append('-wf')
    if dlv_max_int:
        t.append('-N={}'.format(dlv_max_int))
    if other_args:
        t.extend(other_args)
    t.extend(dlv_input_fnames)
    process_ = subprocess.Popen(t, stdout=subprocess.PIPE)
    dlv_output = process_.communicate()[0]
    # dlv_output = subprocess.check_output()
    meta_data = {}
    #attribute_defs = {}
    #temporal_decs = {}
    for fname in dlv_input_fnames:
        with open(fname, 'r') as f:
            dlv_rules = f.read().splitlines()

            f_meta_data = parse_pwe_meta_data(dlv_rules)