Skip to content

Commit

Permalink
Add Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Dec 3, 2024
1 parent 236260c commit e0fb708
Show file tree
Hide file tree
Showing 29 changed files with 120 additions and 74 deletions.
31 changes: 15 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,25 @@ repos:
rev: v0.1.8
hooks:
- id: ripsecrets
- repo: https://github.com/PyCQA/autoflake
rev: v2.3.1
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.1
hooks:
- id: autoflake
- repo: https://github.com/asottile/pyupgrade
rev: v3.19.0
hooks:
- id: pyupgrade
- id: ruff-format
args:
- --py310-plus
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 24.10.0
- --line-length=110
- repo: https://github.com/PyCQA/prospector
rev: v1.13.3
hooks:
- id: black
- id: prospector
args:
- --line-length=110
- --profile=app/.prospector.yaml
- --tool=ruff
- --die-on-tool-error
- --output-format=pylint
additional_dependencies:
- prospector-profile-duplicated==1.8.0 # pypi
- prospector-profile-utils==1.13.0 # pypi
- ruff==0.8.1 # pypi
- repo: https://github.com/sbrunner/jsonschema-validator
rev: 0.3.2
hooks:
Expand Down
8 changes: 2 additions & 6 deletions acceptance_tests/acceptance/file/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

@pytest.fixture(scope="package")
def composition(request):
"""
Fixture that will wait that the composition is started, used for all the tests.
"""
"""Fixture that will wait that the composition is started, used for all the tests."""
del request
for slave in ("api",):
path = os.path.join("/config", slave)
Expand All @@ -22,8 +20,6 @@ def composition(request):

@pytest.fixture
def app_connection(composition): # pylint: disable=redefined-outer-name
"""
Fixture that returns a connection to a running batch container.
"""
"""Fixture that returns a connection to a running batch container."""
del composition
return Connection(base_url="http://api_file:8080/scm/", origin="http://example.com/")
3 changes: 2 additions & 1 deletion acceptance_tests/acceptance/file/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import os

import yaml
from acceptance import get_hash, wait_sync
from c2cwsgiutils.acceptance.connection import Connection

from acceptance import get_hash, wait_sync


def test_ok(app_connection: Connection):
test_git_hash = get_hash("/repos/test_git")
Expand Down
8 changes: 2 additions & 6 deletions acceptance_tests/acceptance/inline/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

@pytest.fixture(scope="package")
def composition(request):
"""
Fixture that will wait that the composition is started, used for all the tests.
"""
"""Fixture that will wait that the composition is started, used for all the tests."""
del request
for slave in ("api",):
path = os.path.join("/config", slave)
Expand All @@ -22,8 +20,6 @@ def composition(request):

@pytest.fixture
def app_connection(composition): # pylint: disable=redefined-outer-name
"""
Fixture that returns a connection to a running batch container.
"""
"""Fixture that returns a connection to a running batch container."""
del composition
return Connection(base_url="http://api_inline:8080/scm/", origin="http://example.com/")
3 changes: 2 additions & 1 deletion acceptance_tests/acceptance/inline/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import os

from acceptance import get_hash, wait_sync
from c2cwsgiutils.acceptance.connection import Connection

from acceptance import get_hash, wait_sync


def test_ok(app_connection: Connection):
test_git_hash = get_hash("/repos/test_git")
Expand Down
10 changes: 4 additions & 6 deletions acceptance_tests/acceptance/normal/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@


def wait_slaves():
"""Wait for the slaves to be up."""

def what() -> bool:
r = requests.get("http://api:8080/scm/1/status", headers={"X-Scm-Secret": "changeme"})
if r.status_code == 200:
Expand Down Expand Up @@ -41,9 +43,7 @@ def what() -> bool:

@pytest.fixture(scope="package")
def composition(request):
"""
Fixture that will wait that the composition is started, used for all the tests.
"""
"""Fixture that will wait that the composition is started, used for all the tests."""
del request
for slave in ("api", "slave", "slave-others"):
path = os.path.join("/config", slave)
Expand All @@ -57,8 +57,6 @@ def composition(request):

@pytest.fixture
def app_connection(composition: None): # pylint: disable=redefined-outer-name
"""
Fixture that returns a connection to a running batch container.
"""
"""Fixture that returns a connection to a running batch container."""
del composition
return Connection(base_url="http://api:8080/scm/", origin="http://example.com/")
3 changes: 2 additions & 1 deletion acceptance_tests/acceptance/normal/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import pytest
import requests
from acceptance import get_hash, wait_sync
from c2cwsgiutils.acceptance.connection import Connection

from acceptance import get_hash, wait_sync


@pytest.fixture()
def git_source(app_connection: Connection):
Expand Down
3 changes: 0 additions & 3 deletions app/.bandit.yaml

This file was deleted.

16 changes: 11 additions & 5 deletions app/.prospector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ inherits:
- utils:base
- utils:no-design-checks
- utils:fix
- utils:unsafe
- utils:c2cwsgiutils
- duplicated

pylint:
disable:
- missing-timeout # Done by c2cwsgiutils
ignore-paths:
- shared_config_manager/configuration.py

bandit:
mypy:
options:
config: .bandit.yaml
python_version: '3.10'

ruff:
disable:
- D102 # Missing docstring in public method
- D107 # Missing docstring in `__init__`
3 changes: 0 additions & 3 deletions app/gunicorn.conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def on_starting(server: gunicorn.arbiter.Arbiter) -> None:
Called just before the master process is initialized.
"""

del server

prometheus.start()
Expand All @@ -87,7 +86,6 @@ def post_fork(server: gunicorn.arbiter.Arbiter, worker: gunicorn.workers.base.Wo
Called just after a worker has been forked.
"""

del server, worker

prometheus.cleanup()
Expand All @@ -99,7 +97,6 @@ def child_exit(server: gunicorn.arbiter.Arbiter, worker: gunicorn.workers.base.W
Called just after a worker has been exited, in the master process.
"""

del server

multiprocess.mark_process_dead(worker.pid) # type: ignore [no-untyped-call]
30 changes: 29 additions & 1 deletion app/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ c2cwsgiutils = { version = "6.1.5", extras = ["broadcast", "standard", "oauth2",
pyramid-mako = "1.1.0"

[tool.poetry.dev-dependencies]
prospector = { extras = ["with_mypy", "with_bandit", "with_pyroma"], version = "1.13.3" }
prospector = { version = "1.13.3", extras = ["with_mypy", "with_bandit", "with_pyroma", "with_ruff"] }
prospector-profile-duplicated = "1.8.0"
prospector-profile-utils = "1.13.0"
types-pyyaml = "6.0.12.20240917"
Expand Down
2 changes: 2 additions & 0 deletions app/shared_config_manager/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@


def forbidden(request: pyramid.request.Request) -> pyramid.response.Response:
"""Redirect to the login page if the user is not authenticated."""
is_auth = c2cwsgiutils.auth.is_auth(request)

if is_auth:
Expand All @@ -26,6 +27,7 @@ def forbidden(request: pyramid.request.Request) -> pyramid.response.Response:


def main(_: Any, **settings: Any) -> Any:
"""Get the WSGI application."""
config = Configurator(settings=settings, route_prefix=os.environ.get("ROUTE_PREFIX", "/scm"))

config.include(c2cwsgiutils.pyramid.includeme)
Expand Down
3 changes: 1 addition & 2 deletions app/shared_config_manager/scripts/shared_config_slave.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

import c2cwsgiutils.setup_process
import prometheus_client

from shared_config_manager import slave_status # noqa: F401, pylint: disable=unused-import
from shared_config_manager.sources import registry


def main() -> None:
"""Get the WSGI application."""
parser = argparse.ArgumentParser(description="Run the shared config slave")
c2cwsgiutils.setup_process.fill_arguments(parser)
args = parser.parse_args()
Expand Down
9 changes: 5 additions & 4 deletions app/shared_config_manager/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@


class User:
"""User object for the application."""

auth_type: str
login: str | None
name: str | None
Expand Down Expand Up @@ -53,9 +55,10 @@ def has_access(self, source_config: SourceConfig) -> bool:


class SecurityPolicy:
"""Security policy for the application."""

def identity(self, request: pyramid.request.Request) -> User:
"""Return app-specific user object."""

if not hasattr(request, "user"):
user = None

Expand Down Expand Up @@ -114,13 +117,12 @@ def identity(self, request: pyramid.request.Request) -> User:
request,
)

setattr(request, "user", user)
request.user = user

return request.user # type: ignore

def authenticated_userid(self, request: pyramid.request.Request) -> str | None:
"""Return a string ID for the user."""

identity = self.identity(request)

if identity is None:
Expand All @@ -132,7 +134,6 @@ def permits(
self, request: pyramid.request.Request, context: SourceConfig, permission: str
) -> Allowed | Denied:
"""Allow access to everything if signed in."""

identity = self.identity(request)

if identity is None:
Expand Down
2 changes: 1 addition & 1 deletion app/shared_config_manager/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _refresh_all(request: pyramid.request.Request) -> dict[str, Any]:
raise HTTPServerError("Master source not initialized")
registry.MASTER_SOURCE.validate_auth(request)
nb_refresh = 0
for id_ in registry.get_sources().keys():
for id_ in registry.get_sources():
registry.refresh(id_=id_, request=request)
nb_refresh += 1
return {"status": 200, "nb_refresh": nb_refresh}
Expand Down
2 changes: 2 additions & 0 deletions app/shared_config_manager/slave_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

@broadcast.decorator(expect_answers=True)
def get_slaves_status() -> SlaveStatus:
"""Get the status of all the slaves."""
return {"sources": registry.get_stats()}


@broadcast.decorator(expect_answers=True)
def get_source_status(id_: str) -> SourceStatus:
"""Get the status of a source."""
source = registry.get_source(id_)
if source is None:
return {"filtered": id_ in registry.FILTERED_SOURCES}
Expand Down
6 changes: 3 additions & 3 deletions app/shared_config_manager/sources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@


class BaseSource:
"""Base class for sources."""

def __init__(self, id_: str, config: SourceConfig, is_master: bool) -> None:
self._id = id_
self._config = config
Expand Down Expand Up @@ -207,7 +209,7 @@ def get_stats(self) -> SourceStatus:
config_copy = copy.deepcopy(self._config)
stats_ = cast(SourceStatus, config_copy)
for template_stats, template_engine in zip(
stats_.get("template_engines", []), self._template_engines
stats_.get("template_engines", []), self._template_engines, strict=False
):
template_engine.get_stats(template_stats)

Expand Down Expand Up @@ -264,12 +266,10 @@ def _hide_sensitive(data: dict[str, str] | None) -> None:
@broadcast.decorator(expect_answers=False)
def _set_refresh_success(source: str) -> None:
"""Set refresh in success in all process."""

_REFRESH_ERROR_GAUGE.labels(source=source).set(0)


@broadcast.decorator(expect_answers=False)
def _set_fetch_success(source: str) -> None:
"""Set fetch in success in all process."""

_FETCH_ERROR_GAUGE.labels(source=source).set(0)
2 changes: 2 additions & 0 deletions app/shared_config_manager/sources/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@


class GitSource(SshBaseSource):
"""Source that get files with git."""

def _do_refresh(self) -> None:
self._checkout()
self._copy(self._copy_dir(), excludes=[".git"])
Expand Down
Loading

0 comments on commit e0fb708

Please sign in to comment.