import sys
import os
import importlib.metadata
import argparse

from . import meta


def main():

    this_file_path = __file__

    assert '__main__.py' == os.path.basename(this_file_path)

    package_module_dir_path = os.path.dirname(this_file_path)


    assert 'NAME' not in globals()

    exec(meta.get_meta_str(package_module_dir_path))

    assert 'NAME' in globals()


    metadata = None
    try:
        metadata = importlib.metadata.metadata(NAME)
    except importlib.metadata.PackageNotFoundError:
        pass

    name    = metadata['Name']    if metadata else 'name_not_available'
    summary = metadata['Summary'] if metadata else 'summary_not_available'
    version = metadata['Version'] if metadata else 'version_not_available'


    arg_parser = argparse.ArgumentParser(
        prog                  = name,
        description           = summary,
        formatter_class       = argparse.ArgumentDefaultsHelpFormatter,
        fromfile_prefix_chars = '@'
    )
    
    arg_parser.add_argument(
        '-V', '--version',
        action  = 'version',
        version = f'{name} {version}'
    )
    
    args = arg_parser.parse_args()


    data_dir_path = os.path.join(package_module_dir_path, 'data')

    assert os.path.isdir(data_dir_path)

    quote_file_path = os.path.join(data_dir_path, 'quote.txt')

    assert os.path.isfile(quote_file_path)

    with open(quote_file_path, 'r') as quote_file:

        quote_str = quote_file.read().strip()

    print(quote_str)
    
    return 0


if '__main__' == __name__:
    sys.exit(main())


import os
import logging
import argparse
import tarfile
import copy
from pathlib import Path

from allennlp.common import Params
from allennlp.common.util import import_module_and_submodules

from machamp import util

logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
                    level=logging.INFO)
logger = logging.getLogger(__name__)

parser = argparse.ArgumentParser()
parser.add_argument("archive", type=str, help="The archive file")
parser.add_argument("input_file", type=str, help="The input file to predict")
parser.add_argument("pred_file", type=str, help="The output prediction file")
parser.add_argument("--dataset", default=None, type=str,
                    help="name of the dataset, needed to know the word_idx/sent_idxs to read from")
#parser.add_argument("--write_scores", default=None, type=str, # TODO
#                    help="If set, evaluate the prediction and store it in the given file")
parser.add_argument("--device", default=None, type=int, help="CUDA device number; set to -1 for CPU")
parser.add_argument("--batch_size", default=None, type=int, help="The size of each prediction batch")
parser.add_argument("--raw_text", action="store_true", help="Input raw sentences, one per line in the input file.")
args = parser.parse_args()

import_module_and_submodules("machamp")

archive_dir = Path(args.archive).resolve().parent

if not os.path.isfile(archive_dir / "weights.th"):
    with tarfile.open(args.archive) as tar:
        tar.extractall(archive_dir)

config_file = archive_dir / "config.json"

params = Params.from_file(config_file)

if args.device is not None:
    params['trainer']['cuda_device'] = args.device
params['dataset_reader']['is_raw'] = args.raw_text

if args.dataset is None and len(params['dataset_reader']['datasets']) > 1:
    logger.error("please provide --dataset, because we currently don't support writing " +
                 "tasks of multiple datasets in one run.\nOptions: " +
                 str([dataset for dataset in params['dataset_reader']['datasets']]))
    exit(1)

if args.dataset not in params['dataset_reader']['datasets'] and args.dataset is not None:
    logger.error("Non existing --dataset option specified, please pick one from: " + 
                 str([dataset for dataset in params['dataset_reader']['datasets']]))
    exit(1)

if args.dataset:
    datasets = copy.deepcopy(params['dataset_reader']['datasets'])
    for iter_dataset in datasets:
        if iter_dataset != args.dataset:
            del params['dataset_reader']['datasets'][iter_dataset]

util.predict_model_with_archive("machamp_predictor", params, archive_dir, args.input_file, args.pred_file,
                                batch_size=args.batch_size)
