Skip to content

Commit

Permalink
Minor refactor, set up linter and pytest github action workflows.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroBone committed Jan 22, 2024
1 parent 5b00071 commit a7793e0
Show file tree
Hide file tree
Showing 45 changed files with 214 additions and 79 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Run linter

on:
push:
branches:
- main

permissions:
contents: read

jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup PDM
uses: pdm-project/setup-pdm@v3
- name: Install dependencies
run: pdm install
- name: Run ruff linter
run: pdm run ci-ruff
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Run tests

on:
push:
branches:
- main

permissions:
contents: read

jobs:
pytest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup PDM
uses: pdm-project/setup-pdm@v3
- name: Install dependencies
run: pdm install
- name: Run pytest
run: pdm run ci-pytest
19 changes: 14 additions & 5 deletions docs_dynamic/gen_ref_pages.py → docs_dynamic/gen_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@ def snake_case_to_title(input_str: str, /) -> str:

src = Path(__file__).parent.parent / "src"

internal_module = src / "officialeye" / "_internal"
api_module = src / "officialeye" / "api"

mod_symbol = '<code class="doc-symbol doc-symbol-nav doc-symbol-module"></code>'

for path in sorted(src.rglob("officialeye/**/*.py")):

module_path = path.relative_to(src).with_suffix("")
doc_path = path.relative_to(src).with_suffix(".md")
full_doc_path = Path("reference", doc_path)

if internal_module in path.parents:
# the current module is a submodule of the internal api
doc_path = path.relative_to(internal_module).with_suffix(".md")
full_doc_path = Path("dev", "api", doc_path)
elif api_module in path.parents:
# the current module is part of the public api
doc_path = path.relative_to(api_module).with_suffix(".md")
full_doc_path = Path("api", doc_path)
else:
continue

parts = tuple(module_path.parts)

Expand All @@ -40,6 +52,3 @@ def snake_case_to_title(input_str: str, /) -> str:
fd.write(f"::: {ident}")

mkdocs_gen_files.set_edit_path(full_doc_path, path)

with mkdocs_gen_files.open("reference/summary.md", "w") as nav_file:
nav_file.writelines(nav.build_literate_nav())
5 changes: 3 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ plugins:
enabled: !ENV [CI, false]
- gen-files:
scripts:
- docs_dynamic/gen_ref_pages.py
- docs_dynamic/gen_api.py
- literate-nav:
nav_file: summary.md
- mkdocstrings:
Expand Down Expand Up @@ -121,7 +121,8 @@ nav:
- License: usage/other/license.md
- Development:
- dev/changelog.md
- "API Reference": reference/
- "Internal API": dev/api/
- "API Reference": api/

extra_css:
- assets/stylesheets/style.css
Expand Down
64 changes: 62 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ docs-build = "mkdocs build"
docs-deploy = "mkdocs gh-deploy --force"

# other scripts
tests-integration = {shell = "pytest src/tests/integration/*.py"}
count-loc = {shell = "find src/officialeye -name '*.py' | xargs wc -l"}

ci-pytest = {shell = "pytest src/tests/"}
ci-ruff = {shell = "ruff check --output-format=github"}

[tool.pdm.dev-dependencies]
doc = [
"mkdocs-material",
Expand All @@ -63,4 +65,62 @@ doc = [
]
test = [
"pytest"
]
]
lint = [
"ruff"
]

[tool.ruff]
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".ipynb_checkpoints",
".mypy_cache",
".nox",
".pants.d",
".pyenv",
".pytest_cache",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
".vscode",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"site-packages",
"venv"
]
line-length = 150

target-version = "py310"

[tool.ruff.lint]
select = [
# pycodestyle
"E",
# Pyflakes
"F",
# flake8-bugbear
"B",
# flake8-simplify
"SIM",
# isort
"I",
]

ignore = [
"D417",
"SIM108" # Usage of ternary operators instead of if-then-else
]

[tool.ruff.lint.pydocstyle]
convention = "google"
2 changes: 1 addition & 1 deletion src/officialeye/_internal/config/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import abc
from typing import Dict, Callable
from typing import Callable, Dict


class Config(abc.ABC):
Expand Down
5 changes: 2 additions & 3 deletions src/officialeye/_internal/context/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import os
import tempfile
from typing import List, Dict, Union
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Dict, List, Union

import click
import cv2
Expand All @@ -16,8 +15,8 @@
from officialeye._internal.logger.singleton import get_logger

if TYPE_CHECKING:
from officialeye._internal.template.template import Template
from officialeye._internal.io.driver import IODriver
from officialeye._internal.template.template import Template


# TODO: move part of the Context class methods to IO driver
Expand Down
2 changes: 1 addition & 1 deletion src/officialeye/_internal/context/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __exit__(self, exception_type: any, exception_value: BaseException, tracebac
)
oe_error.add_external_cause(exception_value)
else:
assert False
raise AssertionError()

self._context.get_io_driver().handle_error(oe_error)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from officialeye._internal.diffobject.difference_modes import DIFF_MODE_OVERRIDE, DIFF_MODE_ADD, DIFF_MODE_REMOVE
from officialeye._internal.diffobject.difference_modes import DIFF_MODE_ADD, DIFF_MODE_OVERRIDE, DIFF_MODE_REMOVE
from officialeye._internal.diffobject.exception import DiffObjectException
from officialeye._internal.diffobject.specification import DiffObjectSpecification
from officialeye._internal.diffobject.specification_entry import DiffObjectSpecificationEntry
Expand Down
4 changes: 2 additions & 2 deletions src/officialeye/_internal/diffobject/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _validator(entry_point: Dict[str, any], /):
_validator(value)
continue

assert False
raise AssertionError()

_validator(self._spec)

Expand Down Expand Up @@ -62,7 +62,7 @@ def _mapper(entry_point: Dict[str, any], /) -> yml.Validator:
mapped_dict[yml.Optional(key)] = _mapper(value)
continue

assert False
raise AssertionError()

return yml.Map(mapped_dict)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import strictyaml as yml

from officialeye._internal.diffobject.difference_modes import DIFF_MODE_OVERRIDE, DIFF_MODE_ADD
from officialeye._internal.diffobject.difference_modes import DIFF_MODE_ADD, DIFF_MODE_OVERRIDE
from officialeye._internal.diffobject.exception import DiffObjectException
from officialeye._internal.diffobject.specification_entry import DiffObjectSpecificationEntry

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import strictyaml as yml

from officialeye._internal.diffobject.difference_modes import DIFF_MODE_OVERRIDE, DIFF_MODE_ADD, DIFF_MODE_REMOVE
from officialeye._internal.diffobject.difference_modes import DIFF_MODE_ADD, DIFF_MODE_OVERRIDE, DIFF_MODE_REMOVE
from officialeye._internal.diffobject.exception import DiffObjectException
from officialeye._internal.diffobject.specification_entry import DiffObjectSpecificationEntry

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import strictyaml as yml

from officialeye._internal.diffobject.difference_modes import DIFF_MODE_OVERRIDE, DIFF_MODE_ADD, DIFF_MODE_REMOVE
from officialeye._internal.diffobject.difference_modes import DIFF_MODE_ADD, DIFF_MODE_OVERRIDE, DIFF_MODE_REMOVE
from officialeye._internal.diffobject.exception import DiffObjectException
from officialeye._internal.diffobject.specification_entry import DiffObjectSpecificationEntry

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import strictyaml as yml

from officialeye._internal.diffobject.difference_modes import DIFF_MODE_OVERRIDE, DIFF_MODE_ADD
from officialeye._internal.diffobject.difference_modes import DIFF_MODE_ADD, DIFF_MODE_OVERRIDE
from officialeye._internal.diffobject.exception import DiffObjectException
from officialeye._internal.diffobject.specification_entry import DiffObjectSpecificationEntry

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import strictyaml as yml

from officialeye._internal.diffobject.difference_modes import DIFF_MODE_OVERRIDE, DIFF_MODE_ADD, DIFF_MODE_REMOVE
from officialeye._internal.diffobject.difference_modes import DIFF_MODE_ADD, DIFF_MODE_OVERRIDE, DIFF_MODE_REMOVE
from officialeye._internal.diffobject.exception import DiffObjectException
from officialeye._internal.diffobject.specification_entry import DiffObjectSpecificationEntry

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import strictyaml as yml

from officialeye._internal.diffobject.difference_modes import DIFF_MODE_OVERRIDE, DIFF_MODE_ADD
from officialeye._internal.diffobject.difference_modes import DIFF_MODE_ADD, DIFF_MODE_OVERRIDE
from officialeye._internal.diffobject.exception import DiffObjectException
from officialeye._internal.diffobject.specification_entry import DiffObjectSpecificationEntry

Expand Down
2 changes: 0 additions & 2 deletions src/officialeye/_internal/error/errors/internal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Union

from officialeye._internal.error.codes import ERR_INTERNAL
from officialeye._internal.error.error import OEError
from officialeye._internal.error.modules import ERR_MODULE_INTERNAL
Expand Down
2 changes: 1 addition & 1 deletion src/officialeye/_internal/error/errors/io.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from officialeye._internal.error.codes import ERR_IO_INVALID_PATH, ERR_IO_INVALID_SUPERVISION_ENGINE, ERR_IO_OPERATION_NOT_SUPPORTED_BY_DRIVER
from officialeye._internal.error.error import OEError
from officialeye._internal.error.codes import ERR_IO_INVALID_SUPERVISION_ENGINE, ERR_IO_OPERATION_NOT_SUPPORTED_BY_DRIVER, ERR_IO_INVALID_PATH
from officialeye._internal.error.modules import ERR_MODULE_IO


Expand Down
2 changes: 1 addition & 1 deletion src/officialeye/_internal/error/errors/matching.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from officialeye._internal.error.codes import ERR_MATCHING_INVALID_ENGINE_CONFIG, ERR_MATCHING_MATCH_COUNT_OUT_OF_BOUNDS
from officialeye._internal.error.error import OEError
from officialeye._internal.error.codes import ERR_MATCHING_MATCH_COUNT_OUT_OF_BOUNDS, ERR_MATCHING_INVALID_ENGINE_CONFIG
from officialeye._internal.error.modules import ERR_MODULE_MATCHING


Expand Down
2 changes: 1 addition & 1 deletion src/officialeye/_internal/error/errors/supervision.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from officialeye._internal.error.error import OEError
from officialeye._internal.error.codes import ERR_SUPERVISION_CORRESPONDENCE_NOT_FOUND, ERR_SUPERVISION_INVALID_ENGINE_CONFIG
from officialeye._internal.error.error import OEError
from officialeye._internal.error.modules import ERR_MODULE_SUPERVISION


Expand Down
13 changes: 12 additions & 1 deletion src/officialeye/_internal/error/errors/template.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
from officialeye._internal.error.codes import (
ERR_TEMPLATE_ID_NOT_UNIQUE,
ERR_TEMPLATE_INVALID_CONCURRENCY_CONFIG,
ERR_TEMPLATE_INVALID_FEATURE,
ERR_TEMPLATE_INVALID_FEATURE_CLASS,
ERR_TEMPLATE_INVALID_INTERPRETATION,
ERR_TEMPLATE_INVALID_KEYPOINT,
ERR_TEMPLATE_INVALID_MATCHING_ENGINE,
ERR_TEMPLATE_INVALID_MUTATOR,
ERR_TEMPLATE_INVALID_SUPERVISION_ENGINE,
ERR_TEMPLATE_INVALID_SYNTAX,
)
from officialeye._internal.error.error import OEError
from officialeye._internal.error.codes import *
from officialeye._internal.error.modules import ERR_MODULE_TEMPLATE


Expand Down
2 changes: 1 addition & 1 deletion src/officialeye/_internal/interpretation/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import cv2

from officialeye._internal.context.context import Context
from officialeye._internal.interpretation.serializable import Serializable
from officialeye._internal.interpretation.config import InterpretationMethodConfig
from officialeye._internal.interpretation.serializable import Serializable


class InterpretationMethod(abc.ABC):
Expand Down
4 changes: 2 additions & 2 deletions src/officialeye/_internal/interpretation/methods/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def interpret(self, feature_img: cv2.Mat, template_id: str, feature_id: str, /)
feature_class_generator = feature.get_feature_class().get_features()

# check if the generator generates at least two elements
if sum(1 for _ in zip(range(2), feature_class_generator)) == 2:
if sum(1 for _ in zip(range(2), feature_class_generator, strict=False)) == 2:
raise ErrTemplateInvalidInterpretation(
"while applying the '{FileMethod.METHOD_ID}' interpretation method.",
f"This method cannot be applied if there are at least two features inheriting the feature class defining this method."
"This method cannot be applied if there are at least two features inheriting the feature class defining this method."
)

os.makedirs(os.path.dirname(self._path), exist_ok=True)
Expand Down
2 changes: 1 addition & 1 deletion src/officialeye/_internal/io/drivers/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, context: Context, /):
def handle_show_result(self, template: Template, img: cv2.Mat, /):
raise ErrIOOperationNotSupportedByDriver(
f"while trying to output the result of showing the template '{template.template_id}'",
f"Driver 'run' does not support this operation."
"Driver 'run' does not support this operation."
)

def handle_error(self, error: OEError, /):
Expand Down
1 change: 0 additions & 1 deletion src/officialeye/_internal/logger/singleton.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from officialeye._internal.logger.logger import Logger


_logger = Logger()


Expand Down
2 changes: 1 addition & 1 deletion src/officialeye/_internal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
from officialeye._internal.io.drivers.run import RunIODriver
from officialeye._internal.io.drivers.test import TestIODriver
from officialeye._internal.logger.singleton import get_logger
from officialeye.meta import OFFICIALEYE_GITHUB, OFFICIALEYE_VERSION
from officialeye._internal.template.analyze import do_analyze
from officialeye._internal.template.create import create_example_template_config_file
from officialeye._internal.template.schema.loader import load_template
from officialeye.meta import OFFICIALEYE_GITHUB, OFFICIALEYE_VERSION

_context_manager: ContextManager = ContextManager()

Expand Down
Loading

0 comments on commit a7793e0

Please sign in to comment.