Skip to content

Commit

Permalink
fix ruff issues
Browse files Browse the repository at this point in the history
  • Loading branch information
msaipraneeth committed Oct 2, 2024
1 parent 70b13de commit d2b875f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 79 deletions.
1 change: 1 addition & 0 deletions cmem_plugin_graphql/workflow/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""workflow plugin"""
30 changes: 13 additions & 17 deletions cmem_plugin_graphql/workflow/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import io
import json
from typing import Sequence, Optional, Dict, Any, Tuple, Iterator
from collections.abc import Iterator, Sequence
from typing import Any

import jinja2
import validators
Expand All @@ -15,14 +16,14 @@
)
from cmem_plugin_base.dataintegration.plugins import WorkflowPlugin
from cmem_plugin_base.dataintegration.utils import write_to_dataset
from gql import gql, Client
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
from graphql import GraphQLSyntaxError, GraphQLError
from graphql import GraphQLError, GraphQLSyntaxError

from cmem_plugin_graphql.workflow.utils import (
get_dict,
is_jinja_template,
get_entities_from_list,
is_jinja_template,
)


Expand Down Expand Up @@ -127,7 +128,7 @@ def __init__( # nosec
if oauth_access_token:
self.headers["Authorization"] = f"Bearer {oauth_access_token}"

def set_graphql_variable_values(self, variable_values):
def set_graphql_variable_values(self, variable_values: str) -> None:
"""Validate and set graphql_variable_values"""
try:
if not variable_values:
Expand All @@ -142,7 +143,7 @@ def set_graphql_variable_values(self, variable_values):
except json.decoder.JSONDecodeError as ex:
raise ValueError("Variables String is not valid.") from ex

def set_graphql_query(self, query):
def set_graphql_query(self, query: str) -> None:
"""Validate and set graphql_query"""
query = query.strip()
try:
Expand All @@ -155,14 +156,11 @@ def set_graphql_query(self, query):
except GraphQLSyntaxError as ex:
raise ValueError("Query string is not Valid.") from ex

def execute(
self, inputs: Sequence[Entities], context: ExecutionContext
) -> Entities:
def execute(self, inputs: Sequence[Entities], context: ExecutionContext) -> Entities:
"""Execute GraphQL query"""
self.log.info("Start GraphQL query.")
dataset_id = (
f"{context.task.project_id()}:{self.graphql_dataset}"
if self.graphql_dataset
else None
f"{context.task.project_id()}:{self.graphql_dataset}" if self.graphql_dataset else None
)
processed_entities: int = 0
failed_entities: int = 0
Expand Down Expand Up @@ -196,7 +194,7 @@ def execute(
processed_entities += 1
payload.append(result)

summary: list[Tuple[str, str]] = []
summary: list[tuple[str, str]] = []
warnings: list[str] = []
summary.append(("Failed entities", str(failed_entities)))
context.report.update(
Expand All @@ -217,9 +215,7 @@ def execute(

return get_entities_from_list(payload)

def process_entities(
self, entities: Entities
) -> Iterator[Optional[Dict[str, Any]]]:
def process_entities(self, entities: Entities) -> Iterator[dict[str, Any] | None]:
"""Process entities"""
# Select your transport with a defined url endpoint
transport = AIOHTTPTransport(url=self.graphql_url, headers=self.headers)
Expand All @@ -243,6 +239,6 @@ def process_entities(
GraphQLSyntaxError,
json.decoder.JSONDecodeError,
) as ex:
self.log.error(f"Failed entity: {type(ex)}")
self.log.error(f"Failed entity: {type(ex)}") # noqa: TRY400

yield result
24 changes: 13 additions & 11 deletions cmem_plugin_graphql/workflow/utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
"""Utils module"""

import json
import uuid
from typing import Dict, Iterator, Any, List, Set, Optional
from collections.abc import Iterator
from typing import Any

import jinja2
from cmem_plugin_base.dataintegration.entity import (
Entities,
Entity,
EntityPath,
EntitySchema,
)
import jinja2


def get_dict(entities: Entities) -> Iterator[Dict[str, str]]:
"""get dict from entities"""
def get_dict(entities: Entities) -> Iterator[dict[str, str]]:
"""Get dict from entities"""
paths = entities.schema.paths
for entity in entities.entities:
result = {}
Expand All @@ -30,10 +32,10 @@ def is_jinja_template(value: str) -> bool:
return res != value


def get_entities_from_list(data: List[Dict[str, Any]]) -> Entities:
"""generate entities from list"""
paths: List[str] = []
unique_paths: Set[str] = set()
def get_entities_from_list(data: list[dict[str, Any]]) -> Entities:
"""Generate entities from list"""
paths: list[str] = []
unique_paths: set[str] = set()
entities = []
# first pass to extract paths
for dict_ in data:
Expand All @@ -51,9 +53,9 @@ def get_entities_from_list(data: List[Dict[str, Any]]) -> Entities:
return Entities(entities=entities, schema=schema)


def create_entity(paths: List[str], dict_: Dict[str, Any]) -> Entity:
def create_entity(paths: list[str], dict_: dict[str, Any]) -> Entity:
"""Create entity from dict based on order from paths list"""
values: List[List[Optional[str]]] = []
values: list[list[str | None]] = []
for path in paths:
value = dict_.get(path)
if value is None:
Expand All @@ -62,5 +64,5 @@ def create_entity(paths: List[str], dict_: Dict[str, Any]) -> Entity:
values.append([value])
else:
values.append([json.dumps(value)])
entity_uri = f"urn:uuid:{str(uuid.uuid4())}"
entity_uri = f"urn:uuid:{uuid.uuid4()!s}"
return Entity(uri=entity_uri, values=values)
41 changes: 21 additions & 20 deletions poetry.lock

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

7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cmem-plugin-graphql"
version = "0.0.0"
version = "3.1.1.dev1+70b13de.dirty"
license = "Apache-2.0"
description = "Send queries to a GraphQL endpoint and save the results in a JSON dataset."
homepage = "https://github.com/eccenca/cmem-plugin-graphql"
Expand All @@ -20,7 +20,7 @@ include = ["CHANGELOG.md"]

[tool.poetry.dependencies]# if you need to change python version here, change it also in .python-version
python = "^3.11"
validators = "^0.20.0"
validators = "^0.34.0"
gql = {extras = ["all"], version ="^3.5.0b6" }
Jinja2 = "^3.1.2"

Expand All @@ -42,13 +42,14 @@ pytest-html = "^4.1.1"
pytest-memray = { version = "^1.7.0", markers = "platform_system != 'Windows'" }
ruff = "^0.6.1"
safety = "^1.10.3"
types-requests = "^2.32.0.20240914"

[build-system]
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning"]
build-backend = "poetry_dynamic_versioning.backend"

[tool.poetry-dynamic-versioning]
enable = true
enable = false
vcs = "git"
dirty = true
bump = true
Expand Down
Loading

0 comments on commit d2b875f

Please sign in to comment.