Skip to content

Commit

Permalink
fix some easy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
seebi committed Sep 13, 2024
1 parent c6b4fbf commit b15b28f
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 54 deletions.
1 change: 1 addition & 0 deletions cmem_plugin_base/dataintegration/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""dataintegration"""
32 changes: 19 additions & 13 deletions cmem_plugin_base/dataintegration/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,49 @@ class SystemContext:
"""Passed into methods to request general system information."""

def di_version(self) -> str:
"""The version of the running DataIntegration instance."""
"""Get the version of the running DataIntegration instance"""

def encrypt(self, value: str) -> str:
"""Encrypts a value using the secret key, which is configured
in 'plugin.parameters.password.crypt.key'
"""Encrypt a value using the secret key
The key is configured 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'
"""Decrypt a value using the secret key
The key is configured in 'plugin.parameters.password.crypt.key'.
"""


class UserContext:
"""Passed into methods that are triggered by a user interaction."""

def user_uri(self) -> str:
"""The URI of the user."""
"""Get the URI of the user"""

def user_label(self) -> str:
"""The name of the user."""
"""Get the name of the user"""

def token(self) -> str:
"""Retrieves the OAuth token for the user."""
"""Retrieve the oAuth token for the user"""


class TaskContext:
"""Passed into objects that are part of a DataIntegration task/project."""

def project_id(self) -> str:
"""The identifier of the project."""
"""Get the identifier of the project"""

def task_id(self) -> str:
"""The identifier of the task."""
"""Get the identifier of the task"""


@dataclass()
class ExecutionReport:
"""Workflow operators may generate execution reports. An execution report holds
"""Workflow Execution Report
Workflow operators may generate execution reports. An execution report holds
basic information and various statistics about the operator execution.
"""

Expand Down Expand Up @@ -86,7 +90,8 @@ class ReportContext:
"""Passed into workflow plugins that may generate a report during execution."""

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

Expand Down Expand Up @@ -115,12 +120,13 @@ def workflow_id(self) -> str:

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.
"""


Expand Down
23 changes: 13 additions & 10 deletions cmem_plugin_base/dataintegration/description.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def __init__(self, file_name: str, package: str) -> None:
raise ValueError(f"Guessed mime type '{self.mime_type}' does not start with 'image/'.")

def __str__(self):
"""Get data URI for the icon
https://en.wikipedia.org/wiki/Data_URI_scheme
"""
data_base64 = b64encode(self.data).decode()
return f"""data:{self.mime_type};base64,{data_base64}"""

Expand All @@ -73,7 +77,7 @@ class PluginParameter:
:param visible: If true, the parameter will be displayed to the user in the UI.
"""

def __init__(
def __init__( # noqa: PLR0913
self,
name: str,
label: str = "",
Expand Down Expand Up @@ -104,9 +108,7 @@ class PluginDescription:
:param icon: An optional custom plugin icon.
"""

# pylint: disable=too-many-instance-attributes

def __init__( # pylint: disable=too-many-arguments
def __init__( # noqa: PLR0913
self,
plugin_class,
label: str,
Expand Down Expand Up @@ -182,8 +184,10 @@ 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.
"""A list of common plugin categories.
At the moment, in the UI, categories are only utilized for rule operators,
such as transform plugins.
"""

# Plugins in the 'Recommended' category will be shown preferably
Expand Down Expand Up @@ -229,7 +233,7 @@ class Plugin:

plugins: list[PluginDescription] = []

def __init__(
def __init__( # noqa: PLR0913
self,
label: str,
plugin_id: str | None = None,
Expand All @@ -254,6 +258,7 @@ def __init__(
self.parameters = parameters

def __call__(self, func):
"""Allow to call the instance"""
plugin_desc = PluginDescription(
plugin_class=func,
label=self.label,
Expand All @@ -268,9 +273,7 @@ def __call__(self, func):
return func

def retrieve_parameters(self, plugin_class: type) -> list[PluginParameter]:
"""Retrieves parameters from a plugin class and matches them with the user
parameter definitions.
"""
"""Retrieve parameters from a plugin class and matches them with the user parameter defs"""
# Only return parameters for user-defined init methods.
if not hasattr(plugin_class.__init__, "__code__"):
return []
Expand Down
6 changes: 3 additions & 3 deletions cmem_plugin_base/dataintegration/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_packages():


def delete_modules(module_name: str = "cmem") -> None:
"""Finds and deletes all plugins within a base package.
"""Find and delete all plugins within a base package.
:param module_name: The base package. Will recurse into all submodules
of this package.
Expand All @@ -46,7 +46,7 @@ def delete_modules(module_name: str = "cmem") -> None:
def import_modules(
package_name: str = "cmem",
) -> list[PluginDescription]:
"""Finds and imports all plugins within a base package.
"""Find and import all plugins within a base package.
:param package_name: The base package. Will recurse into all submodules
of this package.
Expand Down Expand Up @@ -96,7 +96,7 @@ def discover_plugins(package_name: str = "cmem_plugin") -> PluginDiscoveryResult
try:
for plugin in import_modules(package_name=name):
plugin_descriptions.plugins.append(plugin)
except BaseException as ex:
except BaseException as ex: # noqa: BLE001
error = PluginDiscoveryError(
package_name=name,
error_message=str(ex),
Expand Down
12 changes: 8 additions & 4 deletions cmem_plugin_base/dataintegration/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ def __init__(self, path: str, is_relation: bool = False, is_single_value: bool =
self.is_relation = is_relation
self.is_single_value = is_single_value

def __repr__(self):
def __repr__(self) -> str:
"""Get a string representation"""
obj = {
"path": self.path,
"is_relation": self.is_relation,
"is_single_value": self.is_single_value,
}
return f"EntityPath({obj})"

def __eq__(self, other):
def __eq__(self, other) -> bool:
"""Compare"""
return (
isinstance(other, EntityPath)
and self.path == other.path
Expand Down Expand Up @@ -57,11 +59,13 @@ def __init__(
self.path_to_root = path_to_root
self.sub_schemata = sub_schemata

def __repr__(self):
def __repr__(self) -> str:
"""Get a string representation"""
obj = {"type_uri": self.type_uri, "paths": self.paths, "path_to_root": self.path_to_root}
return f"EntitySchema({obj})"

def __eq__(self, other):
def __eq__(self, other) -> bool:
"""Compare"""
return (
isinstance(other, EntitySchema)
and self.type_uri == other.type_uri
Expand Down
10 changes: 4 additions & 6 deletions cmem_plugin_base/dataintegration/parameter/choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ def __init__(self, choice_list: collections.OrderedDict[str, str]):
self.choice_list = choice_list

def label(
self, value: str, depend_on_parameter_values: list[Any], context: PluginContext
self, value: str, depend_on_parameter_values: list[Any], context: PluginContext # noqa: ARG002
) -> str | None:
"""Returns the label for the given choice value."""
"""Return the label for the given choice value."""
return self.choice_list[value]

def autocomplete(
self,
query_terms: list[str],
depend_on_parameter_values: list[Any],
context: PluginContext,
self, query_terms: list[str], depend_on_parameter_values: list[Any], context: PluginContext, # noqa: ARG002
) -> list[Autocompletion]:
"""Autocompletion request - Returns all results that match ALL provided query terms."""
result = []
for identifier in self.choice_list:
label = self.choice_list[identifier]
Expand Down
1 change: 1 addition & 0 deletions cmem_plugin_base/dataintegration/parameter/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def autocomplete(
depend_on_parameter_values: list[Any],
context: PluginContext,
) -> list[Autocompletion]:
"""Autocompletion request - Returns all results that match ALL provided query terms"""
setup_cmempy_user_access(context=context.user)
graphs = get_graphs_list()
result = []
Expand Down
4 changes: 2 additions & 2 deletions cmem_plugin_base/dataintegration/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class WorkflowPlugin(PluginBase):
Should be `None`, if this operator does not return any output."""

def execute(self, inputs: Sequence[Entities], context: ExecutionContext) -> Entities | None:
"""Executes the workflow plugin on a given collection of entities.
"""Execute the workflow plugin on a given collection of entities.
:param inputs: Contains a separate collection of entities for each
input. Currently, DI sends ALWAYS an input. in case no connected
Expand All @@ -85,7 +85,7 @@ class TransformPlugin(PluginBase):
"""Base class of all transform operator plugins."""

def transform(self, inputs: Sequence[Sequence[str]]) -> Sequence[str]:
"""Transforms a collection of values.
"""Transform a collection of values.
:param inputs: A sequence which contains as many elements as there are input
operators for this transformation.
Expand Down
6 changes: 4 additions & 2 deletions cmem_plugin_base/dataintegration/ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def __init__(self, schema: EntitySchema):


class FlexibleSchemaPort(Port):
"""Port that does not have a fixed schema, but will adapt its schema to the
connected port.
"""Port that does not have a fixed schema, but will adapt its schema to the connected port.
Flexible input ports will adapt the schema to the connected output.
Flexible output ports will adapt the schema to the connected input.
It is not allowed to connect two flexible ports.
Expand All @@ -27,6 +27,7 @@ class FlexibleSchemaPort(Port):

class UnknownSchemaPort(Port):
"""Port for which the schema is not known in advance.
This includes output ports with a schema that depends on external factors
(e.g., REST requests).
"""
Expand All @@ -45,5 +46,6 @@ def __init__(self, ports: Sequence[Port]):

class FlexibleNumberOfInputs(InputPorts):
"""Operator accepts a flexible number of inputs.
At the moment, each input is a flexible schema port.
"""
21 changes: 11 additions & 10 deletions cmem_plugin_base/dataintegration/types.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Parameter types."""

from collections.abc import Iterable
from dataclasses import dataclass
from enum import Enum
from inspect import Parameter
from typing import Optional, TypeVar, Generic, Type, Iterable, Any
from typing import Any, Generic, Optional, Type, TypeVar

from cmem_plugin_base.dataintegration.context import PluginContext

Expand All @@ -15,16 +16,18 @@ class Autocompletion:
value: str
"""The value to which the parameter value should be set."""

label: Optional[str]
label: str | None
"""An optional label that a human user would see instead."""


T = TypeVar("T")


class ParameterType(Generic[T]):
"""Represents a plugin parameter type.
Provides string-based serialization and autocompletion."""
"""Represent a plugin parameter type.
Provides string-based serialization and autocompletion.
"""

name: str
"""The name by which this type can be identified. If available,
Expand All @@ -45,8 +48,6 @@ class ParameterType(Generic[T]):
The values of all parameters specified here will be provided
to the autocomplete function."""

# flake8: noqa
# pylint: disable=no-member
def get_type(self):
"""Retrieves the type that is supported by a given instance."""
return self.__orig_bases__[0].__args__[0]
Expand All @@ -58,15 +59,13 @@ def to_string(self, value: T) -> str:
"""Converts parameter values into their string representation."""
return str(value)

# pylint: disable=unused-argument
def autocomplete(
self,
query_terms: list[str],
depend_on_parameter_values: list[Any],
context: PluginContext,
) -> list[Autocompletion]:
"""Autocompletion request.
Returns all results that match ALL provided query terms.
"""Autocompletion request - Returns all results that match ALL provided query terms.
:param query_terms: A list of lower case conjunctive search terms.
:param depend_on_parameter_values The values of the parameters specified by
Expand Down Expand Up @@ -126,7 +125,8 @@ class BoolParameterType(ParameterType[bool]):

name = "boolean"

def from_string(self, value: str, context: PluginContext) -> bool:
def from_string(self, value: str, context: PluginContext) -> bool: # noqa: ARG002
"""Get boolean value from string"""
lower = value.lower()
if lower in ("true", "1"):
return True
Expand All @@ -135,6 +135,7 @@ def from_string(self, value: str, context: PluginContext) -> bool:
raise ValueError("Value must be either 'true' or 'false'")

def to_string(self, value: bool) -> str:
"""Get string value from boolean"""
if value:
return "true"
return "false"
Expand Down
Loading

0 comments on commit b15b28f

Please sign in to comment.