diff --git a/osm_cli/cli/main.py b/osm_cli/cli/main.py index fe08f418..8625b098 100644 --- a/osm_cli/cli/main.py +++ b/osm_cli/cli/main.py @@ -1,8 +1,9 @@ import click import requests -from osm_cli.logging.logger import logger from osm_cli.converters.pdf_converter import PDFConverter +from osm_cli.logging.logger import logger + @click.group() def osm(): @@ -25,17 +26,17 @@ def pdf_xml(file_path, file_id): converter = PDFConverter() if not converter.is_docker_running(): - raise click.ClickException('Please make sure the docker is running') + raise click.ClickException("Please make sure the docker is running") if not converter.is_host_ready(): - raise click.ClickException('The converter server is offline') + raise click.ClickException("The converter server is offline") xml_content = converter.convert(file_path) # Save the converted xml contents - output_file: str = f'docs/examples/sciencebeam_xml_outputs/{file_id}.xml' - with open(output_file, 'w', encoding='utf-8') as xml_file: + output_file: str = f"docs/examples/sciencebeam_xml_outputs/{file_id}.xml" + with open(output_file, "w", encoding="utf-8") as xml_file: xml_file.write(xml_content) - logger.info(f'Converted: {file_path} with ID: {file_id} to XML') + logger.info(f"Converted: {file_path} with ID: {file_id} to XML") except requests.RequestException as error: logger.error("Request error:", error) diff --git a/osm_cli/converters/converter.py b/osm_cli/converters/converter.py index 4bf98da7..c549f039 100644 --- a/osm_cli/converters/converter.py +++ b/osm_cli/converters/converter.py @@ -1,6 +1,6 @@ from abc import ABC, abstractmethod -from pydantic import FilePath, BaseModel +from pydantic import BaseModel, FilePath class Converter(ABC, BaseModel): diff --git a/osm_cli/converters/pdf_converter.py b/osm_cli/converters/pdf_converter.py index 4bcabe70..ea7c009b 100644 --- a/osm_cli/converters/pdf_converter.py +++ b/osm_cli/converters/pdf_converter.py @@ -1,9 +1,9 @@ import socket import docker +import requests from docker.errors import DockerException from pydantic import FilePath -import requests from osm_cli.converters.converter import Converter from osm_cli.utils.config import config @@ -22,12 +22,11 @@ def convert(self, pdf_path: FilePath) -> str: Returns: XML content as a string """ - sciencebeam_url: str = f'{self.protocol}://{self.host}:{self.port}/api/convert' - with open(pdf_path, 'rb') as pdf_file: - files = {'file': pdf_file} - headers = {'Accept': 'application/tei+xml'} - response = requests.post( - sciencebeam_url, files=files, headers=headers) + sciencebeam_url: str = f"{self.protocol}://{self.host}:{self.port}/api/convert" + with open(pdf_path, "rb") as pdf_file: + files = {"file": pdf_file} + headers = {"Accept": "application/tei+xml"} + response = requests.post(sciencebeam_url, files=files, headers=headers) if response.status_code == 200: return response.text @@ -56,7 +55,7 @@ def is_docker_running(self): """ try: client = docker.from_env() - client.images.get('elifesciences/sciencebeam-parser') + client.images.get("elifesciences/sciencebeam-parser") return True except DockerException: return False diff --git a/tests/test_file_processing.py b/tests/test_file_processing.py index 98dc3af3..fa18dd8d 100644 --- a/tests/test_file_processing.py +++ b/tests/test_file_processing.py @@ -1,8 +1,8 @@ import logging +from pathlib import Path import pytest from click.testing import CliRunner -from pathlib import Path from osm_cli.cli.main import pdf_xml @@ -18,10 +18,9 @@ def sample_pdf(tmp_path): def test_pdf_converter(caplog, sample_pdf): caplog.set_level(logging.INFO) file_id = "test_file" - output_file = f'docs/examples/sciencebeam_xml_outputs/{file_id}.xml' + output_file = f"docs/examples/sciencebeam_xml_outputs/{file_id}.xml" runner = CliRunner() result = runner.invoke(pdf_xml, [str(sample_pdf), file_id]) assert result.exit_code == 0 assert f"Converted: {sample_pdf}" in caplog.text assert Path(output_file).exists() -