Skip to content

Commit

Permalink
refactor: project queries (#1566)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas1312 authored Oct 30, 2023
1 parent bdb25ca commit 24f5409
Show file tree
Hide file tree
Showing 46 changed files with 301 additions and 291 deletions.
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ ignore = [
"FBT001", # boolean-type-hint-positional-argument
"FBT002", # boolean-type-hint-keyword-argument
"PLR0913", # Too many arguments to function call
"TD002", # Missing author in TODO"
"TRY003", # Avoid specifying long messages outside the exception class
]

Expand All @@ -134,14 +135,20 @@ ignore = [
"D103", # Missing docstring in public function
"ERA001", # found commented out code
"INP001", # __init__.py missing
"ISC003", # Explicitly concatenated string should be implicitly concatenated"
"N802", # function name should be lowercase
"N806", # variable should be lowercase
"PERF401", # Use a list comprehension to create a transformed list"
"PGH003", # use specific rule codes when ignoring type issues
"PLR2004", # magic value used in comparison
"S101", # use of assert detected
"SIM115", # Use context handler for opening files
"SLF001", # private member accessed
"T201", # print found
]
"*.ipynb" = [
"E402", # Module level import not at top of file"
]

[tool.ruff.pydocstyle]
convention = "google"
Expand Down
1 change: 0 additions & 1 deletion src/kili/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""Kili Python SDK."""

__version__ = "2.147.4"
1 change: 1 addition & 0 deletions src/kili/adapters/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Adapters."""
1 change: 1 addition & 0 deletions src/kili/adapters/kili_api_gateway/api_key/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Api key Kili Gateway module."""
3 changes: 2 additions & 1 deletion src/kili/adapters/kili_api_gateway/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ class ProjectOperationMixin(BaseOperationMixin):
"""Mixin extending Kili API Gateway class with Projects related operations."""

def get_project(self, project_id: ProjectId, fields: ListOrTuple[str]) -> Dict:
"""Get project fields."""
"""Get project."""
fragment = fragment_builder(fields)
query = get_projects_query(fragment)
result = self.graphql_client.execute(
query=query, variables={"where": {"id": project_id}, "first": 1, "skip": 0}
)
projects = result["data"]

if len(projects) == 0:
raise NotFound(
f"project ID: {project_id}. The project does not exist or you do not have access"
Expand Down
Empty file.
62 changes: 0 additions & 62 deletions src/kili/core/graphql/operations/project/queries.py

This file was deleted.

6 changes: 3 additions & 3 deletions src/kili/entrypoints/cli/project/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import click

from kili.domain.project import ProjectId
from kili.entrypoints.cli.common_args import Options
from kili.entrypoints.cli.helpers import get_kili_client
from kili.services.project import get_project_field


@click.command(name="copy")
Expand Down Expand Up @@ -45,7 +45,7 @@ def copy_project(
with_assets: bool = False,
with_labels: bool = False,
) -> None:
"""Copy an existing Kili project.
r"""Copy an existing Kili project.
The copy can include or not the json interface, quality settings, members, assets and labels of
the source project.
Expand Down Expand Up @@ -83,5 +83,5 @@ def copy_project(
copy_assets=with_assets,
copy_labels=with_labels,
)
title = get_project_field(kili, new_proj_id, "title")
title = kili.kili_api_gateway.get_project(ProjectId(new_proj_id), ("title",))["title"]
print(f'Project copied successfully. New project id: "{new_proj_id}", with title: "{title}"')
8 changes: 5 additions & 3 deletions src/kili/entrypoints/cli/project/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from tabulate import tabulate

from kili.core.constants import INPUT_TYPE
from kili.domain.project import ProjectId
from kili.entrypoints.cli.common_args import Options
from kili.entrypoints.cli.helpers import get_kili_client
from kili.entrypoints.queries.project.helpers import get_project_url
from kili.services.project import get_project_field


@click.command(name="create")
Expand Down Expand Up @@ -38,7 +38,7 @@ def create_project(
description: str,
tablefmt: str,
):
"""Create a Kili project.
r"""Create a Kili project.
interface must be a path pointing to your json interface file
Expand Down Expand Up @@ -75,7 +75,9 @@ def create_project(
json_interface = json.load(interface_file)

elif project_id_src is not None:
json_interface = get_project_field(kili, project_id_src, "jsonInterface")
json_interface = kili.kili_api_gateway.get_project(
ProjectId(project_id_src), ("jsonInterface",)
)["jsonInterface"]

result = kili.create_project(
input_type=input_type,
Expand Down
6 changes: 3 additions & 3 deletions src/kili/entrypoints/cli/project/describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
import click
from tabulate import tabulate

from kili.domain.project import ProjectId
from kili.entrypoints.cli.common_args import Arguments, Options
from kili.entrypoints.cli.helpers import get_kili_client
from kili.entrypoints.queries.project.helpers import (
get_project_metadata,
get_project_metrics,
)
from kili.services.project import get_project


@click.command(name="describe")
@Arguments.project_id
@Options.api_key
@Options.endpoint
def describe_project(api_key: Optional[str], endpoint: Optional[str], project_id: str):
"""Show project description and analytics.
r"""Show project description and analytics.
\b
!!! Examples
Expand All @@ -43,7 +43,7 @@ def describe_project(api_key: Optional[str], endpoint: Optional[str], project_id
"numberOfOpenQuestions",
"numberOfSolvedQuestions",
]
project = get_project(kili, project_id, query_fields)
project = kili.kili_api_gateway.get_project(ProjectId(project_id), query_fields)
metadata = get_project_metadata(project, kili.graphql_client.endpoint)
dataset_metrics, quality_metrics = get_project_metrics(project)

Expand Down
7 changes: 4 additions & 3 deletions src/kili/entrypoints/cli/project/import_.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
check_exclusive_options,
get_external_id_from_file_path,
)
from kili.services.project import get_project_field


def check_asset_type(key: str, value: str, http_client: Optional[HttpClient]) -> str:
Expand Down Expand Up @@ -83,7 +82,7 @@ def import_assets(
as_frames: bool,
verbose: bool, # pylint: disable=unused-argument
):
"""Add assets into a project.
r"""Add assets into a project.
Files can be paths to files or to folders. You can provide several paths separated by spaces.
Expand Down Expand Up @@ -124,7 +123,9 @@ def import_assets(
For such imports, please use the `append_many_to_dataset` method in the Kili SDK.
"""
kili = get_kili_client(api_key=api_key, api_endpoint=endpoint)
input_type = get_project_field(kili, project_id, "inputType")
input_type = kili.kili_api_gateway.get_project(ProjectId(project_id), ("inputType",))[
"inputType"
]
if input_type not in ("VIDEO_LEGACY", "VIDEO") and (fps is not None or as_frames is True):
illegal_option = "fps and frames are"
if not as_frames:
Expand Down
5 changes: 3 additions & 2 deletions src/kili/entrypoints/cli/project/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import click
from typing_extensions import get_args

from kili.domain.project import ProjectId
from kili.entrypoints.cli.common_args import Arguments, Options
from kili.entrypoints.cli.helpers import get_kili_client
from kili.services import label_import
Expand Down Expand Up @@ -83,7 +84,7 @@ def import_labels(
target_job: Optional[str],
overwrite: bool,
):
"""Import labels or predictions.
r"""Import labels or predictions.
Files can be paths to files or to folders. <br>
You can provide several paths separated by spaces.
Expand Down Expand Up @@ -133,7 +134,7 @@ def import_labels(
kili,
list(files or []),
metadata_file,
project_id,
ProjectId(project_id),
input_format,
target_job,
not verbose,
Expand Down
15 changes: 8 additions & 7 deletions src/kili/entrypoints/cli/project/list_.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from tabulate import tabulate

from kili.adapters.kili_api_gateway.helpers.queries import QueryOptions
from kili.core.graphql.operations.project.queries import ProjectQuery, ProjectWhere
from kili.domain.project import ProjectFilters
from kili.entrypoints.cli.common_args import Options
from kili.entrypoints.cli.helpers import get_kili_client

Expand All @@ -25,7 +25,7 @@
default=100,
)
def list_projects(api_key: Optional[str], endpoint: Optional[str], tablefmt: str, first: int):
"""List your projects.
r"""List your projects.
\b
!!! Examples
Expand All @@ -35,19 +35,20 @@ def list_projects(api_key: Optional[str], endpoint: Optional[str], tablefmt: str
"""
kili = get_kili_client(api_key=api_key, api_endpoint=endpoint)
projects = list(
ProjectQuery(kili.graphql_client, kili.http_client)(
ProjectWhere(),
[
kili.kili_api_gateway.list_projects(
project_filters=ProjectFilters(id=None),
fields=[
"title",
"id",
"description",
"numberOfAssets",
"numberOfRemainingAssets",
"numberOfReviewedAssets",
],
QueryOptions(disable_tqdm=True, first=first),
),
options=QueryOptions(disable_tqdm=True, first=first),
)
)

projects = pd.DataFrame(projects)
projects["progress"] = projects.apply(
lambda x: (
Expand Down
2 changes: 1 addition & 1 deletion src/kili/entrypoints/mutations/asset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def append_many_to_dataset(
if value is not None:
assets = [{**assets[i], key: value[i]} for i in range(nb_data)]
created_asset_ids = import_assets(
self,
self, # pyright: ignore[reportGeneralTypeIssues]
project_id=ProjectId(project_id),
assets=assets,
disable_tqdm=disable_tqdm,
Expand Down
26 changes: 22 additions & 4 deletions src/kili/entrypoints/mutations/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ def upload_plugin(
raise TypeError('"plugin_path is nullish, please provide a value')

return PluginUploader(
self, plugin_path, plugin_name, verbose, self.http_client
self, # pyright: ignore[reportGeneralTypeIssues]
plugin_path,
plugin_name,
verbose,
self.http_client,
).create_plugin()

@typechecked
Expand Down Expand Up @@ -97,7 +101,12 @@ def create_webhook(
>>> kili.create_webhook(webhook_url='https://my-custom-url-publicly-accessible/', plugin_name='my webhook', header='...')
"""
return WebhookUploader(
self, webhook_url, plugin_name, header, verbose, handler_types
self, # pyright: ignore[reportGeneralTypeIssues]
webhook_url,
plugin_name,
header,
verbose,
handler_types,
).create_webhook()

@typechecked
Expand Down Expand Up @@ -131,7 +140,12 @@ def update_webhook(
>>> kili.update_webhook(webhook_url='https://my-custom-url-publicly-accessible/', plugin_name='my webhook', header='...')
"""
return WebhookUploader(
self, new_webhook_url, plugin_name, new_header, verbose, handler_types
self, # pyright: ignore[reportGeneralTypeIssues]
new_webhook_url,
plugin_name,
new_header,
verbose,
handler_types,
).update_webhook()

@typechecked
Expand Down Expand Up @@ -223,5 +237,9 @@ def update_plugin(
raise TypeError('"plugin_name is nullish, please provide a value')

return PluginUploader(
self, plugin_path, plugin_name, verbose, self.http_client
self, # pyright: ignore[reportGeneralTypeIssues]
plugin_path,
plugin_name,
verbose,
self.http_client,
).update_plugin()
6 changes: 5 additions & 1 deletion src/kili/entrypoints/queries/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ def get_plugin_status(
>>> kili.get_plugin_status(plugin_name="my_plugin_name")
"""
return PluginUploader(
self, "", plugin_name, verbose, self.http_client
self, # pyright: ignore[reportGeneralTypeIssues]
"",
plugin_name,
verbose,
self.http_client,
).get_plugin_runner_status()

@typechecked
Expand Down
2 changes: 1 addition & 1 deletion src/kili/presentation/client/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ def is_rectangle(coco_annotation, coco_image, kili_annotation):

try:
export_labels(
self,
self, # pyright: ignore[reportGeneralTypeIssues]
asset_ids=resolved_asset_ids,
project_id=ProjectId(project_id),
export_type="latest",
Expand Down
Loading

0 comments on commit 24f5409

Please sign in to comment.