Skip to content

Commit

Permalink
apply pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
leej3 committed Jun 24, 2024
1 parent a98c5e0 commit d9eaec3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
13 changes: 7 additions & 6 deletions osm_cli/cli/main.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion osm_cli/converters/converter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod

from pydantic import FilePath, BaseModel
from pydantic import BaseModel, FilePath


class Converter(ABC, BaseModel):
Expand Down
15 changes: 7 additions & 8 deletions osm_cli/converters/pdf_converter.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
5 changes: 2 additions & 3 deletions tests/test_file_processing.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()

0 comments on commit d9eaec3

Please sign in to comment.