def anuncio_get_or_create(anuncio, year, borme):
    """Devuelve una instancia de Anuncio.

    :rtype: (borme.models.Anuncio, bool anuncio created)
    """

    try:
        nuevo_anuncio = Anuncio.objects.get(id_anuncio=anuncio.id, year=year)
        created = False
    except Anuncio.DoesNotExist:
        nuevo_anuncio = Anuncio(
                        id_anuncio=anuncio.id,
                        year=year,
                        borme=borme,
                        datos_registrales=anuncio.datos_registrales)
        created = True

    return nuevo_anuncio, created def borme_get_or_create(_borme):
    """Devuelve una instancia de Borme.

    :param _borme: Instancia BORME
    :param filename: Nombre del archivo BORME-PDF
    :type borme: bormeparser.Borme
    :type filename: str
    :rtype: (borme.models.BormeLog, bool bormelog created)
    """

    try:
        borme = Borme.objects.get(cve=_borme.cve)
        created = False
    except Borme.DoesNotExist:
        borme = Borme(cve=_borme.cve, date=_borme.date, url=_borme.url,
                      from_reg=_borme.anuncios_rango[0],
                      until_reg=_borme.anuncios_rango[1],
                      province=_borme.provincia.name,
                      section=_borme.seccion)
        borme.save()
        # year, type, from_page, until_page, pages, num?, filename?
        created = True

    return borme, created def bormelog_get_or_create(_borme, filename):
    """Devuelve una instancia de BormeLog.

    :param _borme: Instancia BORME
    :param filename: Nombre del archivo BORME-PDF
    :type _borme: borme.models.Borme
    :type filename: str
    :rtype: (borme.models.BormeLog, bool bormelog created)
    """

    try:
        borme_log = BormeLog.objects.get(borme=_borme)
        created = False
    except BormeLog.DoesNotExist:
        borme_log = BormeLog(borme=_borme, path=filename)
        created = True

    return borme_log, created def company_get_or_create(empresa, tipo, slug_c):
    """Devuelve una instancia de Company.

    :rtype: (borme.models.Company, bool company created)
    """

    try:
        company = Company.objects.get(slug=slug_c)
        created = False
    except Company.DoesNotExist:
        company = Company(name=empresa, type=tipo)
        created = True

    return company, created def person_get_or_create(nombre):
    """Devuelve una instancia de Person.

    :rtype: (borme.models.Person, bool person created)
    """

    try:
        slug_p = slugify(nombre)
        person = Person.objects.get(slug=slug_p)
        created = False
    except Person.DoesNotExist:
        person = Person(name=nombre)
        created = True

    return person, created def parse_empresa(cve, nombre):
    empresa, tipo = regex_empresa_tipo(nombre)
    slug_c = slugify(empresa)

    if tipo == '':
        logger.warn("[{}] Tipo de empresa no detectado: {}"
                    .format(cve, empresa))

    return empresa, tipo, slug_c def logger_acto(acto):
    logger.debug(acto.name)
    logger.debug(acto.value) def logger_anuncio_create(anuncio_id, empresa, tipo):
    logger.debug("Creando anuncio {}: {} {}".format(anuncio_id, empresa, tipo)) def logger_borme_create(cve):
    logger.debug("Creando borme {}".format(cve)) def logger_cargo(nombre_cargo, nombres):
    logger.debug("{} {} {}".format(nombre_cargo, nombres, len(nombres))) def logger_empresa_similar(slug, company, empresa, cve):
    logger.warn("[{cve}] WARNING: Empresa similar. Mismo slug: {0}\n"
                "[{cve}] {1}\n"
                "[{cve}] {2}\n"
                .format(slug, company.name, empresa, cve=cve)) def logger_empresa_create(empresa, tipo):
    logger.debug('Creando empresa %s %s' % (empresa, tipo)) def logger_persona_create(nombre):
    logger.debug("Creando persona: {}".format(nombre)) def logger_persona_similar(slug, name, nombre, cve):
    logger.warn("[{cve}] WARNING: Persona similar. Mismo slug: {0}\n"
                "[{cve}] {0}\n"
                "[{cve}] {1}\n"
                .format(slug, name, nombre, cve=cve)) def logger_resume_import(cve="X"):
    # TODO: --from date
    logger.error("[{cve}] Una vez arreglado, reanuda la importación:\n"
                 "[{cve}]   python manage.py importbormetoday local"
                 .format(cve=cve)) def files_exist(files):
    return all([os.path.exists(f) for f in files]) def get_borme_json_path(date):
    year = '%02d' % date.year
    month = '%02d' % date.month
    day = '%02d' % date.day
    return os.path.join(settings.BORME_JSON_ROOT, year, month, day) def get_borme_pdf_path(date):
    year = '%02d' % date.year
    month = '%02d' % date.month
    day = '%02d' % date.day
    return os.path.join(settings.BORME_PDF_ROOT, year, month, day) def get_borme_xml_filepath(date):
    year = str(date.year)
    month = '%02d' % date.month
    day = '%02d' % date.day
    filename = 'BORME-S-%s%s%s.xml' % (year, month, day)
    return os.path.join(settings.BORME_XML_ROOT, year, month, filename)
from django.conf import settings
from django.utils import timezone
from bormeparser.borme import BormeXML
from bormeparser.exceptions import BormeDoesntExistException
from bormeparser.regex import is_company, is_acto_cargo_entrante
from bormeparser.utils import FIRST_BORME
from borme.models import (
        anuncio_get_or_create,
        borme_get_or_create,
        bormelog_get_or_create,
        company_get_or_create,
        person_get_or_create,
)
from borme.utils.strings import parse_empresa
from . import actos
from .logger import (
        logger_acto,
        logger_anuncio_create,
        logger_borme_create,
        logger_cargo,
        logger_empresa_similar,
        logger_empresa_create,
        logger_persona_create,
        logger_persona_similar,
        logger_resume_import,
)
from .path import (
        files_exist,
        get_borme_json_path,
        get_borme_pdf_path,
        get_borme_xml_filepath
)
import datetime
import logging
import time
import os
import bormeparser
                    logger.info('[%s] Elapsed time: %.2f seconds' % (borme.cve, elapsed_time))

            # Remove handlers
            logger.removeHandler(fh1)
            logger.removeHandler(fh2)
            next_date = bxml.next_borme
    except KeyboardInterrupt:
        logger.info('\nImport aborted.')

    elapsed_time = time.time() - total_start_time
    logger.info("\nBORMEs creados: {created_bormes}/{total_bormes}\n"
                "Anuncios creados: {created_anuncios}/{total_anuncios}\n"
                "Empresas creadas: {created_companies}/{total_companies}\n"
                "Personas creadas: {created_persons}/{total_persons}"
                .format(**total_results))
    logger.info("Total elapsed time: %.2f seconds" % elapsed_time)

    return True, total_results


def from_pdf_file(filename, create_json=True):
    """Importa un archivo BORME-PDF en la BD.

    :param filename: Archivo a importar
    :param create_json: Crear BORME-JSON como paso intermedio
    :type filename: str
    :type create_json: bool
    :rtype: (bool, dict)
    """
    results = {
        'created_anuncios': 0,
        'created_bormes': 0,
        'created_companies': 0,
        'created_persons': 0,
        'errors': 0
    }

    try:
        borme = bormeparser.parse(filename, bormeparser.SECCION.A)
        results = _from_instance(borme)
        if create_json:
            json_path = get_borme_json_path(borme.date)
            os.makedirs(json_path, exist_ok=True)
            json_filepath = os.path.join(json_path, '%s.json' % borme.cve)
            borme.to_json(json_filepath)
    except Exception as e:
        logger.error('[X] Error grave (III) en bormeparser.parse(): %s' % filename)
        logger.error('[X] %s: %s' % (e.__class__.__name__, e))

    if not all(map(lambda x: x == 0, results.values())):
        _print_results(results, borme)
    return True, results


def from_json_file(filename):
    """Importa un archivo BORME-JSON en la BD.

    :param filename: Archivo a importar
    :type filename: str or file
    :rtype: (bool, dict)
    """
    results = {
        'created_anuncios': 0,
        'created_bormes': 0,
        'created_companies': 0,
        'created_persons': 0,
        'errors': 0
    }

    try:
        borme = bormeparser.Borme.from_json(filename)
        results = _from_instance(borme)
    except Exception as e:
        logger.error('[X] Error grave (III) en bormeparser.Borme.from_json(): %s' % filename)
        logger.error('[X] %s: %s' % (e.__class__.__name__, e))

    if not all(map(lambda x: x == 0, results.values())):
        _print_results(results, borme)
    return True, results


def _print_results(results, borme):
    """Muestra un resumen del proceso de importación.

    Una vez finalizado el proceso de importación, muestra un resumen con los
    números de bormes, anuncios, empresas y personas importados.

    :param results: Diccionario que contiene los resultados del proceso
    :param borme: Instancia BORME
    :type results: dict.Borme
    :type borme: bormeparser.Borme
    """
    log_message = "[{cve}] BORMEs creados: {bormes}/1\n" \
                  "[{cve}] Anuncios creados: {anuncios}/{total_anuncios}\n" \
                  "[{cve}] Empresas creadas: {companies}/{total_companies}\n" \
                  "[{cve}] Personas creadas: {persons}/{total_persons}" \
                  .format(cve=borme.cve, bormes=results['created_bormes'],
                          anuncios=results['created_anuncios'],
                          total_anuncios=len(borme.get_anuncios()),
                          companies=results['created_companies'],
                          total_companies=results['total_companies'],
                          persons=results['created_persons'],
                          total_persons=results['total_persons'])
    logger.info(log_message)


def _load_cargo_empresa(nombre, borme, anuncio, borme_embed,
                        nombre_cargo, acto, company):
    """Importa en la BD la empresa que aparece en un cargo.

    Inserta la empresa si no existe e inserta los cargos.
    Actualiza las fechas de entrada/salida del cargo.

    :rtype: (dict, bool cargo created)
    """

    empresa, tipo, slug_c = parse_empresa(borme.cve, nombre)
    c, created = company_get_or_create(empresa, tipo, slug_c)

    if created:

        logger_empresa_create(empresa, tipo)