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


class BibFile(object):
    """
    `BibFile` objects are immutable.

    :param pathlib.Path path: Path to file possibly containing BibTeX data.
    """
    def __init__(self, path):
        self.path = path
        self._parse_bib_file()


    def _parse_bib_file(self):
        """
        Parse BibTeX file located at `self.path`

        This method attempts to parse the BibTeX file located at `self.path`. If the file is parseable, `self.bib` is set to a `pybtex.database.BibliographyData` object, containing the bibliography data contained in the file. If the file is unparseable, `self.bib` is set to the exception raised by the parser.
        """
        parser = bibtex.Parser()
        try:
            bib = parser.parse_file(str(self.path.resolve()))
        except PybtexError, e:
            bib = e

        self.bib = bib


    def terse_msg(self):
        """
        STDOUT message listing `self.path`

        This method generates and returns the message to be returned to STDOUT which corresponds to `self.path`.

        :rtype: str
        """
        msg = str(self.path.resolve())
        return msg


    def verbose_msg(self):
        """
        STDOUT message corresponding to `self.path` when --verbose set

        This method generates and returns the message to be returned to STDOUT which corresponds to `self.path` in the event the "--verbose" flag was passed on the command-line. If `self.bib` is of type `BibliographyData`, this method will return an empty string. If `self.bib` is of type `PybtexError`, this method will gather data from the exception into a string which is returned.
        :rtype: str
        """
        msg = ""
        if isinstance(self.bib, TokenRequired):
            msg += self.bib.error_type + ": "
            msg += self.bib.message + "\n"
            msg += str(self.bib.lineno) + " "
            msg += self.bib.get_context()
        elif isinstance(self.bib, PybtexError):
            msg += self.bib.message

        return msg

def gen_bib_dict_test_msg(bib_dict, verbose=False):
    """
    Generate appropriate STDOUT message given bib_dict

    This method creates the string to be printed to STDOUT for a single bib_dict. It generates either a terse or verbose message based on the state of the `verbose` argument.

    :param dict bib_dict: Dictionary containing bib file information.
    :param bool verbose: Directive to construct verbose/terse STDOUT string.
    :rtype: str
    """
    msg = bib_dict["terse_msg"]
    if verbose:
        msg += "\n" + bib_dict["verbose_msg"]

    return msg


@@ -2178,25 +2178,17 @@
 sg%0A%0A
-def gen_bib_dict_
+%0A    def 
 test
@@ -2192,24 +2192,20 @@
 est_msg(
-bib_dict
+self
 , verbos
@@ -2214,28 +2214,36 @@
 False):%0A    
-%22%22%22%0A
+    %22%22%22%0A    
     Generate
@@ -2273,25 +2273,14 @@
 sage
- given bib_dict%0A%0A
+%0A%0A    
     
@@ -2459,151 +2459,8 @@
     
-:param dict bib_dict: Dictionary containing bib file information.%0A    :param bool verbose: Directive to construct verbose/terse STDOUT string.%0A
     
@@ -2471,28 +2471,36 @@
 pe: str%0A    
-%22%22%22%0A
+    %22%22%22%0A    
     msg = bi
@@ -2497,26 +2497,21 @@
   msg = 
-bib_dict%5B%22
+self.
 terse_ms
@@ -2511,19 +2511,23 @@
 erse_msg
-%22%5D%0A
+()%0A    
     if v
@@ -2534,16 +2534,20 @@
 erbose:%0A
+    
         
@@ -2564,18 +2564,13 @@
 %22 + 
-bib_dict%5B%22
+self.
 verb
@@ -2580,12 +2580,16 @@
 _msg
-%22%5D%0A%0A
+()%0A%0A    
     

Refactor gen_bib_dict_test_msg into BibFile class