# -*- coding: utf-8 -*-
import os
import glob
import pathlib2 as pathlib
from pybtex.database.input import bibtex
from pybtex.exceptions import PybtexError


def handle_files_args(*paths_args):
    """
    Handle file(s) arguments from command line

    This method takes the string(s) which were passed to the cli which indicate the files on which to operate. It expands the path arguments and creates a list of `pathlib.Path` objects which unambiguously point to the files indicated by the cli arguments.

    :param str *paths_args: Paths to files.
    :rtype: list
    """
    paths = []

    for paths_arg in paths_args:
        # Handle paths implicitly rooted at user home dir
        paths_arg = os.path.expanduser(paths_arg)

        # Expand wildcards
        paths_arg = glob.glob(paths_arg)

        # Create list of pathlib.Path objects
        paths.extend([pathlib.Path(path_arg) for path_arg in paths_arg])

    return paths


def import_bib_files(*paths):
    """
    Create dictionary of path and corresponding BibTeX

    For each argument passed to this method, the corresponding BibTeX file is parsed. This method constructs a dictionary with each `pathlib.Path` argument as a key and the corresponding parsed BibTeX as the value. If the file specified by an argument to this method cannot be parsed, a value of `pybtex.exceptions.PybtexError` is recorded in the dictionary.

    :param patlib.Path *paths: Path to BibTeX file.
    :rtype dict:
    """
    bibs_paths_dict = {}
    for path in paths:
        parser = bibtex.Parser()
        fqpn = str(path.resolve())
        try:
            bib = parser.parse_file(fqpn)
        except PybtexError, e:
            bib = e
        bibs_paths_dict[path] = bib
        del parser

    return bibs_paths_dict

@@ -1772,8 +1772,1007 @@
 hs_dict%0A
+%0A%0Adef bib_subdict(bibs, val_type):%0A    %22%22%22%0A    Subdict with keys of instance %60val_type%60%0A%0A    :param dict bibs:%0A    :param type val_type:%0A    :rtype dict:%0A    %22%22%22%0A    subdict = %7Bkey: val for (key, val) in bibs.iteritems() if isinstance(val, val_type)%7D%0A    return subdict%0A%0A%0Adef generate_test_message(bibs, verbose):%0A    %22%22%22%0A    Generate message for %22test%22 functionality%0A%0A    :param dict bibs:%0A    :param bool verbose:%0A    :rtype str:%0A    %22%22%22%0A    terse_msgs = %5Bkey.resolve() for (key, val) in bibs.iteritems()%5D%0A    if verbose:%0A        verbose_msgs = %5Bgenerate_verbose_err_output_message(val) for (key, val) in bibs.iteritems()%5D%0A    else:%0A        verbose_msgs = %5B%22%22%5D * len(terse_msgs)%0A%0A    return msgs%0A%0A%0Adef generate_verbose_err_output_message(err):%0A    %22%22%22%0A    Generate output message for an error%0A    %22%22%22%0A    msg = %22%22%0A    try:%0A        msg += val.error_type + %22%5Cn%22%0A        msg += val.message + %22%5Cn%22%0A        msg += val.linno + %22%5Cn%22%0A        msg += get_context()%0A    except:%0A        pass%0A%0A    return msg%0A%0A

Add methods to generate verbose output