Skip to content

Commit

Permalink
chore: replace black with ruff as the formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
wood-push-melon committed May 24, 2024
1 parent 578b496 commit abf459e
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 156 deletions.
16 changes: 12 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@ repos:
- id: end-of-file-fixer
- id: requirements-txt-fixer
- id: trailing-whitespace
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
additional_dependencies:
- tomli
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
name: isort (python)
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.7
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- repo: https://github.com/psf/black
rev: 24.4.0
hooks:
- id: black
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.9.0
hooks:
Expand Down
3 changes: 1 addition & 2 deletions fmt-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
black
isort
ruff
10 changes: 4 additions & 6 deletions lib/charms/hydra/v0/hydra_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,10 @@ def send_endpoint_relation_data(self, admin_endpoint: str, public_endpoint: str)

relations = self.model.relations[self._relation_name]
for relation in relations:
relation.data[self._charm.app].update(
{
"admin_endpoint": admin_endpoint,
"public_endpoint": public_endpoint,
}
)
relation.data[self._charm.app].update({
"admin_endpoint": admin_endpoint,
"public_endpoint": public_endpoint,
})


class HydraEndpointsRelationError(Exception):
Expand Down
2 changes: 1 addition & 1 deletion lib/charms/hydra/v0/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,5 +799,5 @@ def set_client_credentials_in_relation_data(
# TODO: What if we are refreshing the client_secret? We need to add a
# new revision for that
secret = self._create_juju_secret(client_secret, relation)
data = dict(client_id=client_id, client_secret_id=secret.id)
data = {"client_id": client_id, "client_secret_id": secret.id}
relation.data[self.model.app].update(_dump_data(data))
10 changes: 2 additions & 8 deletions lint-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
black
flake8==6.1.0
flake8-docstrings
flake8-copyright
flake8-builtins
pyproject-flake8
pep8-naming
isort
codespell
isort
ruff
66 changes: 46 additions & 20 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,59 @@ show_missing = true
minversion = "6.0"
log_cli_level = "INFO"

# Formatting tools configuration
[tool.black]
line-length = 99
target-version = ["py38"]
# Linting and formatting tools configuration
[tool.codespell]
# https://github.com/codespell-project/codespell
skip = ".git,.tox,build,lib,venv,.venv,.mypy_cache,icon.svg,__pycache__"

[tool.isort]
# Profiles: https://pycqa.github.io/isort/docs/configuration/profiles.html
line_length = 99
profile = "black"

# Linting tools configuration
[tool.flake8]
max-line-length = 99
max-doc-length = 99
[tool.ruff]
# Default settings: https://docs.astral.sh/ruff/configuration/
# Settings: https://docs.astral.sh/ruff/settings/
line-length = 99
include = ["pyproject.toml", "src/**/*.py", "tests/**/*.py", "lib/charms/hydra/**/*.py"]
extend-exclude = ["__pycache__", "*.egg_info"]
target-version = "py38"
preview = true

[tool.ruff.lint]
# Rules: https://docs.astral.sh/ruff/rules/
select = ["E", "W", "F", "C", "N", "D", "CPY"]
ignore = [
"D100",
"D101",
"D102",
"D103",
"D105",
"D107",
"D203",
"D204",
"D213",
"D215",
"D400",
"D404",
"D406",
"D407",
"D408",
"D409",
"D413",
"E501",
"N818"
]
per-file-ignores = {"tests/*" = ["D100","D101","D102","D103","D104"]}

[tool.ruff.lint.flake8-copyright]
author = "Canonical Ltd."

[tool.ruff.lint.mccabe]
max-complexity = 10
exclude = [".git", "__pycache__", ".tox", "build", "dist", "*.egg_info", "venv"]
select = ["E", "W", "F", "C", "N", "R", "D", "H"]
# Ignore W503, E501 because using black creates errors with this
# Ignore D107 Missing docstring in __init__
ignore = ["W503", "E501", "D107"]
# D100, D101, D102, D103: Ignore missing docstrings in tests
per-file-ignores = ["tests/*:D100,D101,D102,D103,D104"]
docstring-convention = "google"
# Check for properly formatted copyright header in each file
copyright-check = "True"
copyright-author = "Canonical Ltd."
copyright-regexp = "Copyright\\s\\d{4}([-,]\\d{4})*\\s+%(author)s"

[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.mypy]
pretty = true
Expand Down
112 changes: 49 additions & 63 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
ActiveStatus,
BlockedStatus,
MaintenanceStatus,
ModelError,
Relation,
Secret,
SecretNotFoundError,
Expand Down Expand Up @@ -350,13 +349,11 @@ def _get_database_relation_info(self) -> dict:
relation_id = self.database.relations[0].id
relation_data = self.database.fetch_relation_data()[relation_id]

if not all(
[
relation_data.get("username"),
relation_data.get("password"),
relation_data.get("endpoints"),
]
):
if not all([
relation_data.get("username"),
relation_data.get("password"),
relation_data.get("endpoints"),
]):
return None

return {
Expand Down Expand Up @@ -701,7 +698,7 @@ def _on_client_created(self, event: ClientCreatedEvent) -> None:
event.defer()
return

self._set_oauth_relation_peer_data(event.relation_id, dict(client_id=client["client_id"]))
self._set_oauth_relation_peer_data(event.relation_id, {"client_id": client["client_id"]})
self.oauth.set_client_credentials_in_relation_data(
event.relation_id, client["client_id"], client["client_secret"]
)
Expand Down Expand Up @@ -767,17 +764,15 @@ def _on_create_oauth_client_action(self, event: ActionEvent) -> None:

event.log("Creating client")

cmd_kwargs = remove_none_values(
{
"audience": event.params.get("audience"),
"grant_type": event.params.get("grant-types"),
"redirect_uri": event.params.get("redirect-uris"),
"response_type": event.params.get("response-types"),
"scope": event.params.get("scope"),
"client_secret": event.params.get("client-secret"),
"token_endpoint_auth_method": event.params.get("token-endpoint-auth-method"),
}
)
cmd_kwargs = remove_none_values({
"audience": event.params.get("audience"),
"grant_type": event.params.get("grant-types"),
"redirect_uri": event.params.get("redirect-uris"),
"response_type": event.params.get("response-types"),
"scope": event.params.get("scope"),
"client_secret": event.params.get("client-secret"),
"token_endpoint_auth_method": event.params.get("token-endpoint-auth-method"),
})

try:
client = self._hydra_cli.create_client(**cmd_kwargs)
Expand All @@ -786,18 +781,16 @@ def _on_create_oauth_client_action(self, event: ActionEvent) -> None:
return

event.log("Successfully created client")
event.set_results(
{
"client-id": client.get("client_id"),
"client-secret": client.get("client_secret"),
"audience": client.get("audience"),
"grant-types": ", ".join(client.get("grant_types", [])),
"redirect-uris": ", ".join(client.get("redirect_uris", [])),
"response-types": ", ".join(client.get("response_types", [])),
"scope": client.get("scope"),
"token-endpoint-auth-method": client.get("token_endpoint_auth_method"),
}
)
event.set_results({
"client-id": client.get("client_id"),
"client-secret": client.get("client_secret"),
"audience": client.get("audience"),
"grant-types": ", ".join(client.get("grant_types", [])),
"redirect-uris": ", ".join(client.get("redirect_uris", [])),
"response-types": ", ".join(client.get("response_types", [])),
"scope": client.get("scope"),
"token-endpoint-auth-method": client.get("token_endpoint_auth_method"),
})

def _on_get_oauth_client_info_action(self, event: ActionEvent) -> None:
if not self._hydra_service_is_running:
Expand All @@ -822,12 +815,10 @@ def _on_get_oauth_client_info_action(self, event: ActionEvent) -> None:
event.log(f"Successfully fetched client: {client_id}")
# We dump everything in the result, but we have to first convert it to the
# format the juju action expects
event.set_results(
{
k.replace("_", "-"): ", ".join(v) if isinstance(v, list) else v
for k, v in client.items()
}
)
event.set_results({
k.replace("_", "-"): ", ".join(v) if isinstance(v, list) else v
for k, v in client.items()
})

def _on_update_oauth_client_action(self, event: ActionEvent) -> None:
if not self._hydra_service_is_running:
Expand All @@ -853,19 +844,16 @@ def _on_update_oauth_client_action(self, event: ActionEvent) -> None:
)
return

cmd_kwargs = remove_none_values(
{
"audience": event.params.get("audience") or client.get("audience"),
"grant_type": event.params.get("grant-types") or client.get("grant_types"),
"redirect_uri": event.params.get("redirect-uris") or client.get("redirect_uris"),
"response_type": event.params.get("response-types")
or client.get("response_types"),
"scope": event.params.get("scope") or client["scope"].split(" "),
"client_secret": event.params.get("client-secret") or client.get("client_secret"),
"token_endpoint_auth_method": event.params.get("token-endpoint-auth-method")
or client.get("token_endpoint_auth_method"),
}
)
cmd_kwargs = remove_none_values({
"audience": event.params.get("audience") or client.get("audience"),
"grant_type": event.params.get("grant-types") or client.get("grant_types"),
"redirect_uri": event.params.get("redirect-uris") or client.get("redirect_uris"),
"response_type": event.params.get("response-types") or client.get("response_types"),
"scope": event.params.get("scope") or client["scope"].split(" "),
"client_secret": event.params.get("client-secret") or client.get("client_secret"),
"token_endpoint_auth_method": event.params.get("token-endpoint-auth-method")
or client.get("token_endpoint_auth_method"),
})
event.log(f"Updating client: {client_id}")
try:
client = self._hydra_cli.update_client(client_id, **cmd_kwargs)
Expand All @@ -874,18 +862,16 @@ def _on_update_oauth_client_action(self, event: ActionEvent) -> None:
return

event.log(f"Successfully updated client: {client_id}")
event.set_results(
{
"client-id": client.get("client_id"),
"client-secret": client.get("client_secret"),
"audience": client.get("audience"),
"grant-types": ", ".join(client.get("grant_types", [])),
"redirect-uris": ", ".join(client.get("redirect_uris", [])),
"response-types": ", ".join(client.get("response_types", [])),
"scope": client.get("scope"),
"token-endpoint-auth-method": client.get("token_endpoint_auth_method"),
}
)
event.set_results({
"client-id": client.get("client_id"),
"client-secret": client.get("client_secret"),
"audience": client.get("audience"),
"grant-types": ", ".join(client.get("grant_types", [])),
"redirect-uris": ", ".join(client.get("redirect_uris", [])),
"response-types": ", ".join(client.get("response_types", [])),
"scope": client.get("scope"),
"token-endpoint-auth-method": client.get("token_endpoint_auth_method"),
})

def _on_delete_oauth_client_action(self, event: ActionEvent) -> None:
if not self._hydra_service_is_running:
Expand Down
1 change: 0 additions & 1 deletion src/hydra_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

"""A helper class for interacting with the hydra CLI."""


import json
import logging
import re
Expand Down
Loading

0 comments on commit abf459e

Please sign in to comment.