Skip to content

Commit

Permalink
Entity factory and cleanup (#21)
Browse files Browse the repository at this point in the history
Flesh out an OTEAPI function strategy plugin for converting
between a parsed data source to a SOFT7 Entity instance.

Ignore importlib.metadata deprecation warning:
See issue #395 in oteapi-core to follow up on when this ignore statement
can be removed.

Remove all the noise in the repo, focus on:
- the entity_instance factory
- the OTEAPI plugins
- the pydantic models

Remove unused dependencies.
Update pre-commit hooks.
Update dependency versions.
Update GH Actions.
  • Loading branch information
CasperWA authored Apr 3, 2024
1 parent 9268eb3 commit 02a9dd6
Show file tree
Hide file tree
Showing 39 changed files with 2,937 additions and 1,184 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
jobs:
publish:
name: External
uses: SINTEF/ci-cd/.github/workflows/cd_release.yml@v2.5.1
uses: SINTEF/ci-cd/.github/workflows/cd_release.yml@v2.7.4
if: github.repository == 'SINTEF/soft7' && startsWith(github.ref, 'refs/tags/v')
with:
# General
Expand Down
37 changes: 36 additions & 1 deletion .github/workflows/ci_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
jobs:
basics:
name: External
uses: SINTEF/ci-cd/.github/workflows/ci_tests.yml@v2.5.1
uses: SINTEF/ci-cd/.github/workflows/ci_tests.yml@v2.7.4
with:
# General
install_extras: '[dev]'
Expand All @@ -31,3 +31,38 @@ jobs:

# Documentation
run_build_docs: false

pytest:
name: pytest
runs-on: ubuntu-latest

strategy:
matrix:
# Supported Python versions (according to pyproject.toml)
python-version: ['3.9', '3.10', '3.11', '3.12']

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install -U pip
pip install -U setuptools wheel
pip install -e .[testing]
- name: Run pytest
run: pytest -v -rs --cov=s7 --cov-report=xml --cov-report=term-missing

- name: Upload code coverage report
if: github.repository == 'SINTEF/soft7'
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
flags: pytest
27 changes: 18 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,39 @@ repos:
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.292
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.1
hooks:
- id: ruff
args:
- "--fix"
- "--exit-non-zero-on-fix"
- id: pyupgrade
args: ["--py39-plus", "--keep-runtime-typing"]

- repo: https://github.com/ambv/black
rev: 23.9.1
rev: 24.3.0
hooks:
- id: black

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.3
hooks:
- id: ruff
args:
- "--fix"
- "--exit-non-zero-on-fix"
- "--show-fixes"
- "--no-unsafe-fixes"

- repo: https://github.com/PyCQA/bandit
rev: 1.7.5
rev: 1.7.8
hooks:
- id: bandit
args: ["-r"]
files: ^s7/.*$

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.6.0
rev: v1.9.0
hooks:
- id: mypy
exclude: ^tests/.*$
additional_dependencies:
- pydantic>=2,<3
- types-PyYAML
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 SINTEF
Copyright (c) 2023 SINTEF

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,73 @@
# SOFT7

## OTEAPI plugin

The `soft7` packages comes with an [OTEAPI](https://github.com/EMMC-ASBL/oteapi-core) plugin that allows one to convert any core parser data to a SOFT7 Entity instance.

To use the plugin, call the `'soft7'` `functionType` function strategy.

### Test the plugin

At the root of the repository is a [Docker Compose](https://docs.docker.com/compose/) file, which, when run, will start an [OTEAPI Service](https://github.com/EMMC-ASBL/oteapi-services#readme) that includes the `soft7` OTEAPI plugin.

To start the service, run:

```bash
docker compose pull
docker compose up -d
```

To follow along with the installation of the `soft7` package and startup of the OTEAPI Service, run:

```bash
docker logs -f soft7-oteapi-1
```

Press Ctrl+C to stop following the logs.

To eventually stop the services, run:

```bash
docker compose down
```

But first, let's test the plugin.

Open a Python shell, an [IPython shell](https://ipython.org/), or a [Jupyter Notebook](https://jupyter.org/), and run:

```python
from s7.factories import create_datasource

# Let us use an OPTIMADE structure from the Materials Project as our "raw" data source.
# The chosen structure is mp-1228448 (Al2O3):
# https://materialsproject.org/materials/mp-1228448/
# For more information about OPTIMADE, see https://www.optimade.org/
# For more information about the Materials Project, see https://materialsproject.org/
dataresource_config = {
"downloadUrl": (
"https://optimade.materialsproject.org/v1/structures/mp-1228448?"
"response_format=json"
),
"mediaType": "application/json",
}

# We need to setup a mapping configuration to tell the plugin how to map the OPTIMADE
# structure to a SOFT7 Entity instance.
# This requires knowledge of the OPTIMADE structure and the SOFT7 Entity.
# In our case the OPTIMADE structure specification is available at
# https://github.com/Materials-Consortia/OPTIMADE/blob/v1.1.0/optimade.rst#structures-entries
# and the SOFT7 Entity of choice is the `OPTIMADEStructure` Entity, which can be found
# at http://onto-ns.com/meta/1.0/OPTIMADEStructure
mapping_config = {
"mappingType": "triples",
"prefixes": {
"optimade": "https://optimade.materialsproject.org/v1/structures/mp-1228448#",
"soft7": "http://onto-ns.com/meta/1.0/OPTIMADEStructure#",
},
"triples": {
("optimade:data.id", "", "soft7:properties.id"),
("optimade:data.type", "", "soft7:properties.type"),
("optimade:data.attributes", "", "soft7:properties."),
}
}
```
17 changes: 17 additions & 0 deletions agraph.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# AllegroGraph configuration file
RunAs agraph
SessionPorts 10000-10034
Port 10035
SettingsDirectory /agraph/data/settings
LogDir /agraph/data
PidFile /agraph/data/agraph.pid
InstanceTimeout 604800
SuperUser dbuser:test123
<RootCatalog>
Main /agraph/data/rootcatalog
</RootCatalog>

<SystemCatalog>
Main /agraph/data/systemcatalog
InstanceTimeout 10
</SystemCatalog>
48 changes: 48 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
version: "3"

services:
oteapi:
image: ghcr.io/emmc-asbl/oteapi:${DOCKER_OTEAPI_VERSION:-latest}
ports:
- "${PORT:-8080}:8080"
environment:
OTEAPI_REDIS_TYPE: redis
OTEAPI_REDIS_HOST: redis
OTEAPI_REDIS_PORT: 6379
OTEAPI_prefix: "${OTEAPI_prefix:-/api/v1}"
OTEAPI_INCLUDE_REDISADMIN: "${OTEAPI_INCLUDE_REDISADMIN:-False}"
OTEAPI_EXPOSE_SECRETS: "${OTEAPI_EXPOSE_SECRETS:-True}"
OTEAPI_AUTHENTICAION_DEPENDENCIES:
OTEAPI_PLUGIN_PACKAGES: "-v -e /soft7"
depends_on:
- redis
networks:
- otenet
volumes:
- "${PWD}:/soft7"

redis:
image: redis:latest
volumes:
- redis-persist:/data
networks:
- otenet

agraph:
image: franzinc/agraph:v7.2.0
volumes:
- agraph-data:/agraph/data
- ./agraph.cfg:/agraph/etc/agraph.cfg
ports:
- "10000-10035:10000-10035"
restart: on-failure
shm_size: 4g
networks:
- otenet

volumes:
redis-persist:
agraph-data:

networks:
otenet:
83 changes: 74 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Natural Language :: English",
"Operating System :: OS Independent",
]
Expand All @@ -27,19 +29,24 @@ requires-python = "~=3.9"
dynamic = ["version"]

dependencies = [
"graphviz",
"Jinja2",
"oteapi-core>=0.6.0,<1",
"otelib>=0.4.0,<1",
"pydantic~=2.4",
"pymongo",
"pyyaml",
"rdflib",
"httpx~=0.27.0",
"oteapi-core>=0.7.0.dev1,<1",
"pyyaml~=6.0",
"typing-extensions~=4.10; python_version < '3.12'",
]

# Other dependencies governed by oteapi-core:
# - pydantic

[project.optional-dependencies]
testing = [
"pytest~=8.1",
"pytest-cov~=4.1",
"pytest-httpx~=0.30.0",
]
dev = [
"pre-commit~=2.18",
"pre-commit~=3.6",
"soft7[testing]",
]

[project.urls]
Expand All @@ -50,6 +57,16 @@ Source = "https://github.com/SINTEF/soft7"
Changelog = "https://github.com/SINTEF/soft7/blob/main/CHANGELOG.md"
Package = "https://pypi.org/project/soft7"

# Register OTEAPI strategies
[project.entry-points."oteapi.function"]
"soft7.soft7" = "s7.oteapi_plugin.soft7_function:SOFT7Generator"
"soft7.SOFT7" = "s7.oteapi_plugin.soft7_function:SOFT7Generator"
"soft7.soft" = "s7.oteapi_plugin.soft7_function:SOFT7Generator"
"soft7.SOFT" = "s7.oteapi_plugin.soft7_function:SOFT7Generator"
[project.entry-points."oteapi.parse"]
"soft7.application/yaml" = "s7.oteapi_plugin.yaml_parser:YAMLDataParseStrategy"
"soft7.text/x-yaml" = "s7.oteapi_plugin.yaml_parser:YAMLDataParseStrategy"

[tool.flit.module]
name = "s7"

Expand All @@ -62,3 +79,51 @@ warn_unused_configs = true
show_error_codes = true
allow_redefinition = true
plugins = ["pydantic.mypy"]

[tool.pytest.ini_options]
minversion = "8.1"
addopts = "-rs --cov=s7 --cov-report=term-missing:skip-covered --no-cov-on-fail"
filterwarnings = [
"error", # Fail on any warning

# Ignore deprecation warnings from importlib.metadata (used by oteapi-core)
"ignore:SelectableGroups dict interface is deprecated.*:DeprecationWarning",
]

[tool.ruff.lint]
extend-select = [
"E", # pycodestyle
"F", # pyflakes
"B", # flake8-bugbear
"BLE", # flake8-blind-except
"I", # isort
"ARG", # flake8-unused-arguments
"C4", # flake8-comprehensions
"ICN", # flake8-import-conventions
"G", # flake8-logging-format
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # pylint
"PT", # flake8-pytest-style
"PTH", # flake8-use-pathlib
"RET", # flake8-return
"RUF", # Ruff-specific
"SIM", # flake8-simplify
"T20", # flake8-print
"YTT", # flake8-2020
"EXE", # flake8-executable
"PYI", # flake8-pyi
]
ignore = [
"PLR", # Design related pylint codes

# # Self assignment of variable
# "PLW0127",
]
isort.required-imports = ["from __future__ import annotations"]

[tool.ruff.lint.extend-per-file-ignores]
"tests/**" = [
"BLE", # flake8-blind-except
"PL", # pylint
]
6 changes: 6 additions & 0 deletions s7/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""SOFT7"""

from __future__ import annotations

import logging

__version__ = "0.0.6"

logging.getLogger("s7").setLevel(logging.DEBUG)
Loading

0 comments on commit 02a9dd6

Please sign in to comment.