Skip to content

Commit

Permalink
make package and use pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
leej3 committed Jun 24, 2024
1 parent 26ee209 commit e60932b
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 153 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -e .[dev]
- name: Start ScienceBeam Docker container
run: |
docker run -d --rm -p 8082:8070 elifesciences/sciencebeam-parser
- name: Run tests
run: |
tox
tox
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Ignore the virtual environment directory
_version.py
node_modules
venv/
*coverage*
.idea
Expand Down
35 changes: 35 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
repos:
- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: "v0.4.9"
hooks:
- id: ruff
args: ["--fix"]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
files: ".*\\.py"
exclude: "examples|docs/examples"
- id: check-added-large-files
- id: check-toml
- id: end-of-file-fixer
exclude: "examples|docs/examples"

# - repo: https://github.com/pre-commit/mirrors-prettier
# rev: v4.0.0-alpha.8
# hooks:
# - id: prettier

- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
name: isort
args: ["--profile", "black"]
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ OpenSciMetrics (OSM) applies NLP and LLM-based metrics and indicators related to
Windows: `venv\Scripts\activate`<br>
macOS and Linux: `source venv/bin/activate`

- Next, run `pip install -r requirements.txt` to install all the dependencies.
- Finally, run `python -m osm.cli pdf-xml "path_to_file_name.pdf" file_id`
- Next, run `pip install -e .` to install the package with its dependencies.
- Finally, run `osm pdf-xml "path_to_file_name.pdf" file_id`

# How to run tests of the application
Run `tox`
# How to run the unit tests
- Navigate to the project's root directory and run `python -m pytest`
- Navigate to the project's root directory and run `pytest`
20 changes: 0 additions & 20 deletions commands/file_processing.py

This file was deleted.

Empty file removed osm/__init__.py
Empty file.
15 changes: 0 additions & 15 deletions osm/cli.py

This file was deleted.

3 changes: 3 additions & 0 deletions osm_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import _version

__version__ = _version.version
18 changes: 18 additions & 0 deletions osm_cli/cli/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import click

from osm_cli.logging.logger import logger


@click.group()
def osm():
"""Main command for OSM"""
pass


@osm.command()
@click.argument("file_path", type=click.Path(exists=True))
@click.argument("file_id", type=str)
def pdf_xml(file_path, file_id):
"""This function converts a file from PDF to XML"""
# Function Implementation
logger.info(f"Converted: {file_path} with ID: {file_id} to XML")
File renamed without changes.
3 changes: 2 additions & 1 deletion logs/logger.py → osm_cli/logging/logger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from rich.logging import RichHandler
import logging

from rich.logging import RichHandler

logging.basicConfig(
level=logging.INFO, format="%(message)s", datefmt="[%X]", handlers=[RichHandler()]
)
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions osm_cli/utils/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pydantic.v1 import BaseSettings


class AppConfig(BaseSettings):
PORT: int = 8082
HOST: str = "localhost"
PROTOCAL: str = "http"


config = AppConfig()
56 changes: 56 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[project]
name = "osm-cli"
description = "Open Science Metrics (OSM) client for tracking scientific transparency and reproducibility metrics"
readme = "README.md"
requires-python = ">=3.8"
keywords = [
"open science",
"altmetrics",
"scientific transparency",
"reproducibility"
]
dynamic = ["version"]

dependencies = [
"click>=8.1.7",
"rich>=13.7.1",
]

[project.optional-dependencies]
dev = [
"tox>=4.15.0",
"pytest>=8.2.1",
"pytest-cov",
"ruff>=0.4.9",
"build",
"twine",
"pre-commit",
"pkginfo>=1.10"
]

[project.urls]
homepage = "https://website"
source = "https://github.com/nimh-dsst/osm-cli"
issues = "https://github.com/nimh-dsst/osm-cli/issues"

[build-system]
requires = ["setuptools>=42", "wheel", "setuptools_scm[toml]>=3.4"]
build-backend = "setuptools.build_meta"

[tool.setuptools_scm]
write_to = "osm_cli/_version.py"

[tool.setuptools.packages.find]
where = ["."]
include = ["osm_cli"]

[project.scripts]
osm-cli = "osm_cli.cli.main:osm"

[tool.black]
line-length = 88
target-version = ["py38", "py39", "py310"]

[tool.ruff]
line-length = 88
indent-width = 4
5 changes: 0 additions & 5 deletions requirements.txt

This file was deleted.

77 changes: 0 additions & 77 deletions ruff.toml

This file was deleted.

41 changes: 16 additions & 25 deletions tests/test_file_processing.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
import pytest
from click.testing import CliRunner
import os
import logging
from pathlib import Path

from commands.file_processing import pdf_xml

import pytest
from click.testing import CliRunner

@pytest.mark.usefixtures("caplog")
class TestFileProcessing:
def setup_method(self):
# Create a temporary PDF file for testing
self.pdf_path = "test_sample.pdf"
with open(self.pdf_path, "wb") as f:
f.write(b"%PDF-1.4\n%Test PDF content\n")
from osm_cli.cli.main import pdf_xml

def teardown_method(self):
# Remove the temporary PDF file and any generated XML file
if os.path.exists(self.pdf_path):
os.remove(self.pdf_path)
xml_output = f"{self.pdf_path.replace('.pdf', '')}_test_file.xml"
if os.path.exists(xml_output):
os.remove(xml_output)

def test_pdf_xml_command(self, caplog):
caplog.set_level(logging.INFO)
@pytest.fixture
def sample_pdf():
pdf_path = Path("test_sample.pdf")
pdf_path.write_bytes(b"%PDF-1.4\n%Test PDF content\n")
yield pdf_path
pdf_path.unlink()

runner = CliRunner()
result = runner.invoke(pdf_xml, [self.pdf_path, "test_file"])

assert result.exit_code == 0
assert f"Converted: {self.pdf_path}" in caplog.text
def test_pdf_converter(caplog, sample_pdf):
caplog.set_level(logging.INFO)
runner = CliRunner()
result = runner.invoke(pdf_xml, [str(sample_pdf), "test_file"])
assert result.exit_code == 0
assert f"Converted: {sample_pdf}" in caplog.text
13 changes: 8 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ envlist = py39, py310, py311, py312, lint, format

[testenv]
deps =
pytest
pytest-cov
-r requirements.txt
.[dev]
commands =
python -m pytest --cov=commands --cov-report=term-missing --cov-report=html --cov-report=xml
pytest --cov=commands --cov-report=term-missing --cov-report=html --cov-report=xml

[testenv:lint]
description = Run ruff to lint the code
Expand All @@ -17,4 +15,9 @@ commands =
[testenv:format]
description = Check that code is formatted with ruff
commands =
ruff format --check
ruff format --check

[testenv:.package]
description = Generate distribution package
basepython = python3
commands = python -m build --sdist --wheel --outdir {distdir}

0 comments on commit e60932b

Please sign in to comment.