Skip to content

Commit

Permalink
chore: apply ruff fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Nov 23, 2024
1 parent 3906866 commit 3fdb812
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 210 deletions.
1 change: 0 additions & 1 deletion ckanext/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8

# this is a namespace package
try:
Expand Down
5 changes: 2 additions & 3 deletions ckanext/transmute/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import annotations

from typing import Any

from ckan.plugins.interfaces import Interface


class ITransmute(Interface):
"""Main extension point of ckanext-transmute.
"""
"""Main extension point of ckanext-transmute."""

def get_transmutators(self) -> dict[str, Any]:
"""Register custom transmutation functions.
Expand Down Expand Up @@ -53,5 +53,4 @@ def get_transmutation_schemas(self):
Returns:
Mapping with definitions of named schemas.
"""

return {}
30 changes: 13 additions & 17 deletions ckanext/transmute/logic/action.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
from __future__ import annotations

import logging
import contextvars
from typing import Any, Union

from ckan import types
import ckan.plugins.toolkit as tk
import logging
from typing import Any

import ckan.lib.navl.dictization_functions as df
from ckan.logic import validate, ValidationError
import ckan.plugins.toolkit as tk
from ckan import types
from ckan.logic import ValidationError, validate

from ckanext.transmute.types import Field, MODE_COMBINE
from ckanext.transmute.schema import SchemaParser, SchemaField
from ckanext.transmute.schema import transmute_schema
from ckanext.transmute.exception import TransmutatorError
from ckanext.transmute.utils import get_transmutator, SENTINEL, get_schema

from ckanext.transmute.schema import SchemaField, SchemaParser, transmute_schema
from ckanext.transmute.types import MODE_COMBINE, Field
from ckanext.transmute.utils import SENTINEL, get_schema, get_transmutator

log = logging.getLogger(__name__)
data_ctx = contextvars.ContextVar("data")
Expand Down Expand Up @@ -60,14 +57,13 @@ def tsm_transmute(context: types.Context, data_dict: dict[str, Any]) -> dict[str


def _transmute_data(data, definition, root):
"""Mutates an actual data in `data` dict
"""Mutates an actual data in `data` dict.
Args:
data (dict: [str, Any]): a data to mutate
definition (SchemaParser): SchemaParser object
root (str): a root schema type
"""

schema = definition.types[root]

if not schema:
Expand Down Expand Up @@ -166,12 +162,12 @@ def _process_field(


def _default_from(data: dict[str, Any], field: SchemaField):
default_from: Union[list[str], str] = field.get_default_from()
default_from: list[str] | str = field.get_default_from()
return _get_external_fields(data, default_from, field)


def _replace_from(data: dict[str, Any], field: SchemaField):
replace_from: Union[list[str], str] = field.get_replace_from()
replace_from: list[str] | str = field.get_replace_from()
return _get_external_fields(data, replace_from, field)


Expand Down Expand Up @@ -202,7 +198,7 @@ def _combine_from_fields(data: dict[str, Any], external_fields: list[str]):


def _get_first_filled(data: dict[str, Any], external_fields: list[str]):
"""Return first not-empty field value"""
"""Return first not-empty field value."""
for field_name in external_fields:
field_value = data[field_name]

Expand All @@ -211,7 +207,7 @@ def _get_first_filled(data: dict[str, Any], external_fields: list[str]):


def _apply_validators(field: Field, validators: list[str | list[str]]):
"""Applies validators sequentially to the field value
"""Applies validators sequentially to the field value.
Args:
field (Field): Field object
Expand Down
19 changes: 11 additions & 8 deletions ckanext/transmute/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import json
from typing import Any

import ckan.plugins as p
import ckan.plugins.toolkit as tk

from ckanext.transmute.interfaces import ITransmute
from ckanext.transmute.logic.action import get_actions
from ckanext.transmute.logic.auth import get_auth_functions
from ckanext.transmute.transmutators import get_transmutators
from ckanext.transmute.interfaces import ITransmute

from . import utils

Expand All @@ -27,12 +28,12 @@ def update_config(self, config_):

# IActions
def get_actions(self):
"""Registers a list of extension specific actions"""
"""Registers a list of extension specific actions."""
return get_actions()

# IAuthFunctions
def get_auth_functions(self):
"""Registers a list of extension specific auth function"""
"""Registers a list of extension specific auth function."""
return get_auth_functions()

# ITransmute
Expand All @@ -41,8 +42,10 @@ def get_transmutators(self):

def get_transmutation_schemas(self) -> dict[str, Any]:
prefix = "ckanext.transmute.schema."
return {
key[len(prefix) :]: json.load(open(tk.config[key]))
for key in tk.config
if key.startswith(prefix)
}
schemas: dict[str, Any] = {}
for key in tk.config:
if not key.startswith(prefix):
continue
with open(tk.config[key]) as src:
schemas[key[len(prefix) :]] = json.load(src)
return schemas
25 changes: 12 additions & 13 deletions ckanext/transmute/schema.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from __future__ import annotations

from typing import Any, Optional, Union

import copy
import dataclasses
from typing import Any

from ckan.logic.schema import validator_args
from ckan import types
from ckanext.transmute.exception import SchemaParsingError, SchemaFieldError
from ckan.logic.schema import validator_args

from ckanext.transmute.exception import SchemaFieldError, SchemaParsingError
from ckanext.transmute.utils import SENTINEL


Expand All @@ -16,15 +16,15 @@ class SchemaField:
name: str
type: str
definition: dict[str, Any]
map: Optional[str] = None
map: str | None = None
validators: list[Any] = dataclasses.field(default_factory=list)
multiple: bool = False
remove: bool = False
default: Any = SENTINEL
default_from: Optional[str] = None
default_from: str | None = None
value: Any = SENTINEL
replace_from: Optional[str] = None
inherit_mode: Optional[str] = "combine"
replace_from: str | None = None
inherit_mode: str | None = "combine"
update: bool = False
validate_missing: bool = False
weight: int = 0
Expand All @@ -39,7 +39,7 @@ def __repr__(self):
def is_multiple(self) -> bool:
return bool(self.multiple)

def get_default_from(self) -> Union[list[str], str]:
def get_default_from(self) -> list[str] | str:
if not self.default_from:
raise SchemaFieldError("Field: `default_from` field name is not defined")

Expand All @@ -51,7 +51,7 @@ def get_default_from(self) -> Union[list[str], str]:

return self._get_sibling_field_name(self.default_from)

def get_replace_from(self) -> Union[list[str], str]:
def get_replace_from(self) -> list[str] | str:
if not self.replace_from:
raise SchemaFieldError("Field: `replace_from` field name is not defined")

Expand Down Expand Up @@ -82,7 +82,7 @@ def get_root_type(self):
if not root_type:
raise SchemaParsingError("Schema: root type is missing")

if not root_type in self.schema.get("types", []):
if root_type not in self.schema.get("types", []):
raise SchemaParsingError("Schema: root_type is declared but not defined")

return root_type
Expand All @@ -104,7 +104,7 @@ def _parse_field(
self, field_name: str, field_meta: dict[str, Any], _type: str
) -> SchemaField:
"""Create a SchemaField combining all the
information about field
information about field.
Args:
field_name (str): current field original name
Expand All @@ -114,7 +114,6 @@ def _parse_field(
Returns:
SchemaField: SchemaField object
"""

params: dict[str, Any] = dict({"type": _type}, **field_meta)
return SchemaField(name=field_name, definition=self.types[_type], **params)

Expand Down
2 changes: 1 addition & 1 deletion ckanext/transmute/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest


@pytest.fixture
@pytest.fixture()
def tsm_schema():
return {
"root": "Dataset",
Expand Down
Loading

0 comments on commit 3fdb812

Please sign in to comment.