Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tidy codebase #24

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions .github/workflows/unit-tests.yml → .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
name: Unit Tests
name: Tests

on: [push, pull_request]
on: push

jobs:
test:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: pre-commit/[email protected]
tox-tests:
runs-on: ubuntu-latest

strategy:
matrix:
python-version: [3.9]

steps:

- name: Checkout repository
uses: actions/checkout@v2

Expand All @@ -30,12 +37,16 @@ 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
docker run -d --rm -p 8070 elifesciences/sciencebeam-parser

- name: Run tests
run: |
tox
tox

- name: Test packaging
run: |
tox -e .package
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"]
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ 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`

# Using pre-commit for commit checks

Pre-commit will run all of its hooks on every commit you make. To install
pre-commit and its hooks, run the following commands:

```
pip install pre-commit
pre-commit install
```
19 changes: 0 additions & 19 deletions commands/file_processing.py

This file was deleted.

Empty file removed osm/__init__.py
Empty file.
13 changes: 0 additions & 13 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.
8 changes: 3 additions & 5 deletions logs/logger.py → osm_cli/logging/logger.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from rich.logging import RichHandler
import logging

from rich.logging import RichHandler

logging.basicConfig(
level=logging.INFO,
format="%(message)s",
datefmt="[%X]",
handlers=[RichHandler()]
level=logging.INFO, format="%(message)s", datefmt="[%X]", handlers=[RichHandler()]
)

logger = logging.getLogger("rich")
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()
55 changes: 55 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
[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 = ["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.

39 changes: 15 additions & 24 deletions tests/test_file_processing.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
import pytest
from click.testing import CliRunner
import os
import logging

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(tmp_path):
pdf_path = tmp_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
17 changes: 10 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
[tox]
envlist = py39, py310, py311, py312, lint
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
python -m pytest --cov=osm_cli --cov-report=term-missing --cov-report=html --cov-report=xml

[testenv:lint]
description = Run ruff to lint the code
commands =
ruff check .

[testenv:format]
description = Run ruff to format the code
description = Check that code is formatted with ruff
commands =
ruff check --fix .
ruff format --check

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