Skip to content

Commit

Permalink
apply safe ruff fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
seebi committed Sep 12, 2024
1 parent e4efb8a commit ed0275c
Show file tree
Hide file tree
Showing 31 changed files with 315 additions and 315 deletions.
1 change: 0 additions & 1 deletion cmem_plugin_base/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
"""cmem-plugin-base"""

34 changes: 20 additions & 14 deletions cmem_plugin_base/dataintegration/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
The classes in this file are only for documentation purposes. The actual classes will
be injected by DataIntegration and will follow the signatures of the classes below.
"""

from dataclasses import dataclass, field
from typing import Optional, Tuple, Literal
from typing import Literal

from cmem_plugin_base.dataintegration.entity import Entities

Expand All @@ -17,11 +18,13 @@ def di_version(self) -> str:

def encrypt(self, value: str) -> str:
"""Encrypts a value using the secret key, which is configured
in 'plugin.parameters.password.crypt.key'"""
in 'plugin.parameters.password.crypt.key'
"""

def decrypt(self, value: str) -> str:
"""Decrypts a value using the secret key, which is configured
in 'plugin.parameters.password.crypt.key'"""
in 'plugin.parameters.password.crypt.key'
"""


class UserContext:
Expand Down Expand Up @@ -50,31 +53,32 @@ def task_id(self) -> str:
@dataclass()
class ExecutionReport:
"""Workflow operators may generate execution reports. An execution report holds
basic information and various statistics about the operator execution."""
basic information and various statistics about the operator execution.
"""

entity_count: int = 0
"""The number of entities that have been processed.
This value may be displayed in real-time in the UI."""

operation: Optional[str] = None
operation: str | None = None
"Short label for the executed operation, e.g., read or write (optional)."

operation_desc: str = "entities processed"
"Short description of the operation (plural, past tense)."

summary: list[Tuple[str, str]] = field(default_factory=list)
summary: list[tuple[str, str]] = field(default_factory=list)
"""Generates a short summary of this report.
A sequence of key-value pairs representing the summary table."""

warnings: list[str] = field(default_factory=list)
"""If issues occurred during execution, this contains a list of user-friendly
messages."""

error: Optional[str] = None
error: str | None = None
"""Error message in case a fatal error occurred. If an error is set, the workflow
execution will be stopped after the operator has been executed."""

sample_entities: Optional[Entities] = None
sample_entities: Entities | None = None
"""Sample of entities that were output by this task."""


Expand All @@ -83,7 +87,8 @@ class ReportContext:

def update(self, report: ExecutionReport) -> None:
"""Updates the current execution report.
May be called repeatedly during operator execution."""
May be called repeatedly during operator execution.
"""


class PluginContext:
Expand All @@ -92,7 +97,7 @@ class PluginContext:
system: SystemContext
"""General system information."""

user: Optional[UserContext]
user: UserContext | None
"""The user that creates or updates the plugin. If the plugin is loaded from an
existing project, this might be the configured super user. If DataIntegration is
run outside of a Corporate Memory environment, no user is available.
Expand All @@ -108,14 +113,15 @@ class WorkflowContext:
def workflow_id(self) -> str:
"""Retrieve the identifier of the current workflow"""

def status(self) -> Literal['Idle', 'Waiting', 'Running', 'Canceling', 'Finished']:
def status(self) -> Literal["Idle", "Waiting", "Running", "Canceling", "Finished"]:
"""Retrieve the execution status of this plugin within the current workflow.
One of the following:
- Idle: Plugin has not been started yet.
- Waiting: Plugin has been started and is waiting to be executed.
- Running: Plugin is currently being executed.
- Canceling: Plugin has been requested to stop.
- Finished: Plugin has finished execution."""
- Finished: Plugin has finished execution.
"""


class ExecutionContext:
Expand All @@ -124,15 +130,15 @@ class ExecutionContext:
system: SystemContext
"""General system information."""

user: Optional[UserContext]
user: UserContext | None
"""The user that issued the plugin execution. If a scheduler initiated the
execution, this might be the configured super user. If DataIntegration is
run outside of a Corporate Memory environment, no user is available."""

task: TaskContext
"""Task metadata about the executed plugin."""

workflow: Optional[WorkflowContext]
workflow: WorkflowContext | None
"""Workflow metadata about the executed plugin.
None, if this plugin is not executed within a workflow."""

Expand Down
50 changes: 22 additions & 28 deletions cmem_plugin_base/dataintegration/description.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""Classes for describing plugins"""

import inspect
import sys
from base64 import b64encode
from dataclasses import dataclass, field
from inspect import _empty
from mimetypes import guess_type
from pkgutil import get_data
from typing import Optional, List, Type, Any
from typing import Any

from cmem_plugin_base.dataintegration.plugins import WorkflowPlugin, TransformPlugin
from cmem_plugin_base.dataintegration.plugins import TransformPlugin, WorkflowPlugin
from cmem_plugin_base.dataintegration.types import (
ParameterType,
ParameterTypes,
Expand All @@ -29,11 +30,7 @@ class Icon:
file from the same module, you can use package=__package__.
"""

def __init__(
self,
file_name: str,
package: str
) -> None:
def __init__(self, file_name: str, package: str) -> None:
self.file_name = file_name

self.package = package
Expand All @@ -51,13 +48,9 @@ def __init__(

self.mime_type = guess_type(self.file_name)[0]
if self.mime_type is None:
raise ValueError(
f"Could not guess the mime type of the file '{self.file_name}'."
)
raise ValueError(f"Could not guess the mime type of the file '{self.file_name}'.")
if not self.mime_type.startswith("image/"):
raise ValueError(
f"Guessed mime type '{self.mime_type}' does not start with 'image/'."
)
raise ValueError(f"Guessed mime type '{self.mime_type}' does not start with 'image/'.")

def __str__(self):
data_base64 = b64encode(self.data).decode()
Expand Down Expand Up @@ -85,8 +78,8 @@ def __init__(
name: str,
label: str = "",
description: str = "",
param_type: Optional[ParameterType] = None,
default_value: Optional[Any] = None,
param_type: ParameterType | None = None,
default_value: Any | None = None,
advanced: bool = False,
visible: bool = True,
) -> None:
Expand Down Expand Up @@ -117,12 +110,12 @@ def __init__( # pylint: disable=too-many-arguments
self,
plugin_class,
label: str,
plugin_id: Optional[str] = None,
plugin_id: str | None = None,
description: str = "",
documentation: str = "",
categories: Optional[List[str]] = None,
parameters: Optional[List[PluginParameter]] = None,
icon: Optional[Icon] = None
categories: list[str] | None = None,
parameters: list[PluginParameter] | None = None,
icon: Icon | None = None,
) -> None:
# Set the type of the plugin. Same as the class name of the plugin
# base class, e.g., 'WorkflowPlugin'.
Expand Down Expand Up @@ -190,7 +183,8 @@ class PluginDiscoveryResult:

class Categories:
"""A list of common plugin categories. At the moment, in the UI,
categories are only utilized for rule operators, such as transform plugins."""
categories are only utilized for rule operators, such as transform plugins.
"""

# Plugins in the 'Recommended' category will be shown preferably
RECOMMENDED: str = "Recommended"
Expand Down Expand Up @@ -238,12 +232,12 @@ class Plugin:
def __init__(
self,
label: str,
plugin_id: Optional[str] = None,
plugin_id: str | None = None,
description: str = "",
documentation: str = "",
categories: Optional[List[str]] = None,
parameters: Optional[List[PluginParameter]] = None,
icon: Optional[Icon] = None
categories: list[str] | None = None,
parameters: list[PluginParameter] | None = None,
icon: Icon | None = None,
):
self.label = label
self.description = description
Expand All @@ -268,15 +262,15 @@ def __call__(self, func):
documentation=self.documentation,
categories=self.categories,
parameters=self.retrieve_parameters(func),
icon=self.icon
icon=self.icon,
)
Plugin.plugins.append(plugin_desc)
return func

def retrieve_parameters(self, plugin_class: Type) -> List[PluginParameter]:
def retrieve_parameters(self, plugin_class: type) -> list[PluginParameter]:
"""Retrieves parameters from a plugin class and matches them with the user
parameter definitions."""

parameter definitions.
"""
# Only return parameters for user-defined init methods.
if not hasattr(plugin_class.__init__, "__code__"):
return []
Expand Down
12 changes: 8 additions & 4 deletions cmem_plugin_base/dataintegration/discovery.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Package and plugin discovery module."""

import importlib
import importlib.util
import json
Expand All @@ -9,10 +10,10 @@
from types import ModuleType

from cmem_plugin_base.dataintegration.description import (
PluginDescription,
Plugin,
PluginDiscoveryResult,
PluginDescription,
PluginDiscoveryError,
PluginDiscoveryResult,
)


Expand All @@ -36,18 +37,21 @@ def delete_modules(module_name: str = "cmem") -> None:
"""
if module_name in sys.modules:
module = sys.modules[module_name]
if hasattr(module, '__path__'):
if hasattr(module, "__path__"):
for _loader, name, _ in pkgutil.walk_packages(module.__path__):
delete_modules(module.__name__ + "." + name)
del sys.modules[module.__name__]


def import_modules(package_name: str = "cmem",) -> list[PluginDescription]:
def import_modules(
package_name: str = "cmem",
) -> list[PluginDescription]:
"""Finds and imports all plugins within a base package.
:param package_name: The base package. Will recurse into all submodules
of this package.
"""

def import_submodules(module: ModuleType) -> list[ModuleType]:
modules = []
for _loader, name, is_pkg in pkgutil.walk_packages(module.__path__):
Expand Down
61 changes: 33 additions & 28 deletions cmem_plugin_base/dataintegration/entity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Instance of any given concept."""
from typing import Sequence, Iterator, Optional

from collections.abc import Iterator, Sequence


class EntityPath:
Expand All @@ -12,25 +13,26 @@ class EntityPath:
nested elements.
"""

def __init__(self, path: str,
is_relation: bool = False,
is_single_value: bool = False) -> None:
def __init__(self, path: str, is_relation: bool = False, is_single_value: bool = False) -> None:
self.path = path
self.is_relation = is_relation
self.is_single_value = is_single_value

def __repr__(self):
obj = {
'path': self.path, 'is_relation': self.is_relation,
'is_single_value': self.is_single_value
"path": self.path,
"is_relation": self.is_relation,
"is_single_value": self.is_single_value,
}
return f"EntityPath({obj})"

def __eq__(self, other):
return (isinstance(other, EntityPath)
and self.path == other.path
and self.is_relation == other.is_relation
and self.is_single_value == other.is_single_value)
return (
isinstance(other, EntityPath)
and self.path == other.path
and self.is_relation == other.is_relation
and self.is_single_value == other.is_single_value
)


class EntitySchema:
Expand All @@ -43,29 +45,30 @@ class EntitySchema:
:param sub_schemata: Nested entity schemata
"""

def __init__(self,
type_uri: str,
paths: Sequence[EntityPath],
path_to_root: EntityPath = EntityPath(""),
sub_schemata: Optional[Sequence['EntitySchema']] = None) -> None:
def __init__(
self,
type_uri: str,
paths: Sequence[EntityPath],
path_to_root: EntityPath = EntityPath(""),
sub_schemata: Sequence["EntitySchema"] | None = None,
) -> None:
self.type_uri = type_uri
self.paths = paths
self.path_to_root = path_to_root
self.sub_schemata = sub_schemata

def __repr__(self):
obj = {
"type_uri": self.type_uri, "paths": self.paths,
"path_to_root": self.path_to_root
}
obj = {"type_uri": self.type_uri, "paths": self.paths, "path_to_root": self.path_to_root}
return f"EntitySchema({obj})"

def __eq__(self, other):
return (isinstance(other, EntitySchema)
and self.type_uri == other.type_uri
and self.paths == other.paths
and self.path_to_root == other.path_to_root
and self.sub_schemata == other.sub_schemata)
return (
isinstance(other, EntitySchema)
and self.type_uri == other.type_uri
and self.paths == other.paths
and self.path_to_root == other.path_to_root
and self.sub_schemata == other.sub_schemata
)


class Entity:
Expand All @@ -92,10 +95,12 @@ class Entities:
:param sub_entities Additional entity collections.
"""

def __init__(self,
entities: Iterator[Entity],
schema: EntitySchema,
sub_entities: Optional[Sequence['Entities']] = None) -> None:
def __init__(
self,
entities: Iterator[Entity],
schema: EntitySchema,
sub_entities: Sequence["Entities"] | None = None,
) -> None:
self.entities = entities
self.schema = schema
self.sub_entities = sub_entities
Loading

0 comments on commit ed0275c

Please sign in to comment.