# Path: td_biblio/utils/loaders.py
# class BibTeXLoader(BaseLoader):
#     """BibTeXLoader
# 
#     This loader is designed to import a bibtex file items.
# 
#     Usage:
# 
#     >>> from td_biblio.utils.loaders import BibTeXLoader
#     >>> loader = BibTeXLoader()
#     >>> loader.load_records(bibtex_filename='foo.bib')
#     >>> loader.save_records()
#     """
# 
#     def to_record(self, input):
#         """Convert a bibtex item to a valid record"""
# 
#         # Simple fields
#         record = input.copy()
# 
#         # Journal
#         record["journal"] = input["journal"]
# 
#         # Publication date
#         pub_date = {"day": 1, "month": 1, "year": 1900}
#         input_date = dict((k, v) for (k, v) in input.items() if k in pub_date.keys())
#         pub_date.update(input_date)
#         # Check if month is numerical or not
#         try:
#             int(pub_date["month"])
#         except ValueError:
#             pub_date["month"] = strptime(pub_date["month"], "%b").tm_mon
#         # Convert date fields to integers
#         pub_date = dict((k, int(v)) for k, v in pub_date.items())
#         record["publication_date"] = datetime.date(**pub_date)
# 
#         record["is_partial_publication_date"] = not all(
#             [True if k in input else False for k in pub_date.keys()]
#         )
# 
#         # Authors
#         record["authors"] = []
#         for author in input["author"]:
#             splited = author.split(", ")
#             record["authors"].append(
#                 {"first_name": " ".join(splited[1:]), "last_name": splited[0]}
#             )
#         return record
# 
#     def load_records(self, bibtex_filename=None):
#         """Load all bibtex items as valid records"""
# 
#         with open(bibtex_filename) as bibtex_file:
#             # Parse BibTex file
#             parser = BibTexParser()
#             parser.customization = td_biblio_customization
#             bp = bibtexparser.load(bibtex_file, parser=parser)
#             self.records = [self.to_record(r) for r in bp.get_entry_list()]


import logging
from django.core.management.base import BaseCommand, CommandError
from td_biblio.utils.loaders import BibTeXLoader
# -*- coding: utf-8 -*-




class Command(BaseCommand):
    help = "Import entries from a BibTex file"

    def _get_logger(self):
        logger = logging.getLogger("td_biblio")
        return logger

    def add_arguments(self, parser):
        parser.add_argument("bibtex", help="The path to the BibTeX file to import")

    def handle(self, *args, **options):
        logger = self._get_logger()

        bibtex = options.get("bibtex", None)
        if bibtex is None:

# Path: td_biblio/utils/loaders.py
# class BibTeXLoader(BaseLoader):
#     """BibTeXLoader
# 
#     This loader is designed to import a bibtex file items.
# 
#     Usage:
# 
#     >>> from td_biblio.utils.loaders import BibTeXLoader
#     >>> loader = BibTeXLoader()
#     >>> loader.load_records(bibtex_filename='foo.bib')
#     >>> loader.save_records()
#     """
# 
#     def to_record(self, input):
#         """Convert a bibtex item to a valid record"""
# 
#         # Simple fields
#         record = input.copy()
# 
#         # Journal
#         record["journal"] = input["journal"]
# 
#         # Publication date
#         pub_date = {"day": 1, "month": 1, "year": 1900}
#         input_date = dict((k, v) for (k, v) in input.items() if k in pub_date.keys())
#         pub_date.update(input_date)
#         # Check if month is numerical or not
#         try:
#             int(pub_date["month"])
#         except ValueError:
#             pub_date["month"] = strptime(pub_date["month"], "%b").tm_mon
#         # Convert date fields to integers
#         pub_date = dict((k, int(v)) for k, v in pub_date.items())
#         record["publication_date"] = datetime.date(**pub_date)
# 
#         record["is_partial_publication_date"] = not all(
#             [True if k in input else False for k in pub_date.keys()]
#         )
# 
#         # Authors
#         record["authors"] = []
#         for author in input["author"]:
#             splited = author.split(", ")
#             record["authors"].append(
#                 {"first_name": " ".join(splited[1:]), "last_name": splited[0]}
#             )
#         return record
# 
#     def load_records(self, bibtex_filename=None):
#         """Load all bibtex items as valid records"""
# 
#         with open(bibtex_filename) as bibtex_file:
#             # Parse BibTex file
#             parser = BibTexParser()
#             parser.customization = td_biblio_customization
#             bp = bibtexparser.load(bibtex_file, parser=parser)
#             self.records = [self.to_record(r) for r in bp.get_entry_list()]


# Path: td_biblio/management/commands/bibtex_import.py
import logging
from django.core.management.base import BaseCommand, CommandError
from td_biblio.utils.loaders import BibTeXLoader

# -*- coding: utf-8 -*-




class Command(BaseCommand):
    help = "Import entries from a BibTex file"

    def _get_logger(self):
        logger = logging.getLogger("td_biblio")
        return logger

    def add_arguments(self, parser):
        parser.add_argument("bibtex", help="The path to the BibTeX file to import")

    def handle(self, *args, **options):
        logger = self._get_logger()

        bibtex = options.get("bibtex", None)
        if bibtex is None:

            raise CommandError("A BibTeX file path is required")