Beispiel #1
0
def download_asset(old_path, new_fname, dest_path):
    """Returns msg, if error"""
    if old_path.startswith("http"):
        location = old_path
    else:
        try:
            location = urljoin(config.get("STATIC_URL_FILE"), old_path.strip())
        except ValueError as exc:
            return 'cannot join URL parts "%s" and "%s": %s' % (
                config.get("STATIC_URL_FILE"),
                old_path,
                exc,
            )

    # Verifica se o arquivo ja foi baixado anteriormente
    filename_m, ext_m = files.extract_filename_ext_by_path(old_path)
    dest_path_file = os.path.join(dest_path,
                                  "%s%s" % (new_fname.strip(), ext_m))
    if os.path.exists(dest_path_file):
        logger.info("Arquivo ja baixado: %s", dest_path_file)
        return

    try:
        request_file = request.get(location,
                                   timeout=int(config.get("TIMEOUT") or 10))
    except request.HTTPGetError as e:
        try:
            msg = str(e)
        except TypeError:
            msg = "Unknown error"
        logger.error(e)
        return msg
    else:
        files.write_file_binary(dest_path_file, request_file.content)
Beispiel #2
0
def objXML2file(file_path, obj_xml, pretty=False):
    files.write_file_binary(
        file_path,
        etree.tostring(
            obj_xml,
            doctype=config.DOC_TYPE_XML,
            xml_declaration=True,
            method="xml",
            pretty_print=pretty,
        ),
    )
Beispiel #3
0
def get_asset(old_path, new_fname, dest_path):
    """Obtém os ativos digitais no sistema de arquivo e realiza a persistência
    no ``dest_path``.

    Args:
        old_path: Caminho do ativo
        new_fname: Novo nome para o ativo
        dest_path: Pasta de destino

    Retornos:
        Sem retornos.

        Persiste o ativo no ``dest_path``

    Exceções:
        IOError
        TypeError
    """
    if old_path.startswith("http"):
        asset_path = urlparse(old_path).path
    else:
        asset_path = old_path

    asset_path = asset_path.strip('/')

    # Verifica se o arquivo ja foi baixado anteriormente
    filename_m, ext_m = files.extract_filename_ext_by_path(old_path)
    dest_path_file = os.path.join(dest_path,
                                  "%s%s" % (new_fname.strip(), ext_m))
    if os.path.exists(dest_path_file):
        logger.debug("Arquivo já armazenado na pasta de destino: %s",
                     dest_path_file)
        return

    paths = [
        os.path.join(config.get('SOURCE_IMG_FILE'), asset_path),
        os.path.join(config.get('SOURCE_PDF_FILE'), asset_path),
    ]
    if (filename_m, ext_m) == ("seta", ".gif"):
        seta_path = os.path.join(config.get('SOURCE_IMG_FILE'), "img",
                                 "seta.gif")
        paths.insert(0, seta_path)

    try:
        for path in paths:
            path = find_file(path)
            if path:
                break
        content = files.read_file_binary(path)
    except (TypeError, FileNotFoundError, IOError):
        raise AssetNotFoundError(f"Not found {old_path}")
    else:
        files.write_file_binary(dest_path_file, content)
Beispiel #4
0
    def test_write_binary_file(self):
        expected_text = b"<a><b>bar</b></a>"
        filename = "foo_test_binary.txt"

        try:
            files.write_file_binary(filename, expected_text)

            with open(filename, "rb") as f:
                text = f.read()
        finally:
            os.remove(filename)

        self.assertEqual(expected_text, text)
Beispiel #5
0
def download_asset(old_path, new_fname, dest_path):
    """Returns msg, if error"""
    location = urljoin(config.get("STATIC_URL_FILE"), old_path)
    try:
        request_file = request.get(location,
                                   timeout=int(config.get("TIMEOUT") or 10))
    except request.HTTPGetError as e:
        try:
            msg = str(e)
        except TypeError:
            msg = "Unknown error"
        logger.error(e)
        return msg
    else:
        filename_m, ext_m = files.extract_filename_ext_by_path(old_path)
        files.write_file_binary(
            os.path.join(dest_path, "%s%s" % (new_fname, ext_m)),
            request_file.content)
Beispiel #6
0
def migrate_logos_to_website(session, website_img_dir):
    """Read all Journals from Website MongoDB collection and, for each one, get journal
    logo from current website, save to website media directory, create an image record
    in SQLite Image Table and update journal document with logo URL.

    session: SQLite DB session created in `connect_to_databases`
    website_img_dir: Website media directory
    """
    journals = Journal.objects.all()
    if len(journals) == 0:
        raise exceptions.NoJournalInWebsiteError(
            "No journals in Website Database. Migrate Isis Journals first.")

    for journal in journals:
        logger.debug("Journal acronym %s", journal.acronym)
        logo_old_filename = "glogo.gif"
        logo_url = "{}img/revistas/{}/glogo.gif".format(
            config.get("STATIC_URL_FILE"), journal.acronym)
        try:
            logger.debug("Getting Journal logo in %s", logo_url)
            request_file = request.get(logo_url,
                                       timeout=int(
                                           config.get("TIMEOUT") or 10))
        except request.HTTPGetError as e:
            try:
                msg = str(e)
            except TypeError:
                msg = "Unknown error"
            logger.error(msg)
        else:
            logo_filename = "_".join([journal.acronym, logo_old_filename])
            dest_path_file = os.path.join(website_img_dir, logo_filename)
            logger.debug("Saving Journal logo in %s", dest_path_file)
            files.write_file_binary(dest_path_file, request_file.content)

            image_path = "images/%s" % logo_filename
            logger.debug("Saving logo as image in %s", image_path)
            session.add(Image(name=logo_filename, path=image_path))
            session.commit()

            journal.logo_url = "/media/%s" % image_path
            logger.debug("Updating Journal with logo_url %s", journal.logo_url)
            journal.save()
Beispiel #7
0
    def optimise_xml_to_web(self, target_path, xml_target_path, pid):
        xml_filename = os.path.basename(xml_target_path)

        def read_file(filename):
            file_source_path = os.path.join(target_path, filename)
            try:
                with open(file_source_path, "rb") as file_obj:
                    file_bytes = file_obj.read()
            except OSError as exc:
                raise packtools.exceptions.SPPackageError(
                    "[%s] -  Error reading file {} during {} optimization: {}".
                    format(pid, filename, xml_filename, str(exc)))
            else:
                logger.debug('File "%s" reading %s bytes', file_source_path,
                             len(file_bytes))
                return file_bytes

        logger.debug("Optimizing XML file %s", xml_filename)
        try:
            xml_web_optimiser = packtools.XMLWebOptimiser(
                xml_filename, os.listdir(target_path), read_file, target_path)
        except (etree.XMLSyntaxError, etree.SerialisationError) as exc:
            logger.error(
                '[%s] - Error creating XMLWebOptimiser for "%s": %s',
                pid,
                xml_target_path,
                str(exc),
            )
        else:
            optimised_xml = xml_web_optimiser.get_xml_file()
            logger.debug("Saving optimised XML file %s", xml_filename)
            xml.objXML2file(xml_target_path,
                            etree.fromstring(optimised_xml),
                            pretty=True)

            # Salva ativos digitais otimizados
            for asset_filename, asset_bytes in xml_web_optimiser.get_optimised_assets(
            ):
                if asset_bytes is None:
                    logger.error(
                        '[%s] - Error saving image file "%s" referenced in "%s": '
                        "no file bytes",
                        pid,
                        asset_filename,
                        xml_filename,
                    )
                else:
                    image_target_path = os.path.join(target_path,
                                                     asset_filename)
                    logger.debug('Saving image file "%s"', image_target_path)
                    files.write_file_binary(image_target_path, asset_bytes)
            for asset_filename, asset_bytes in xml_web_optimiser.get_assets_thumbnails(
            ):
                if asset_bytes is None:
                    logger.error(
                        '[%s] - Error saving image file "%s" referenced in "%s": '
                        "no file bytes",
                        pid,
                        asset_filename,
                        xml_filename,
                    )
                else:
                    image_target_path = os.path.join(target_path,
                                                     asset_filename)
                    logger.debug('Saving image file "%s"', image_target_path)
                    files.write_file_binary(image_target_path, asset_bytes)