Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AI-1424] darwin-py black linewidth 88 reformatting #722

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 64 additions & 20 deletions darwin/backend_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ def __init__(self, client: "Client", default_team):

@inject_default_team_slug
def register_data(
self, dataset_slug: str, payload: Dict[str, Any], *, team_slug: Optional[str] = None
self,
dataset_slug: str,
payload: Dict[str, Any],
*,
team_slug: Optional[str] = None,
) -> Dict[str, Any]:

payload["dataset_slug"] = dataset_slug
response = self._client._post(
endpoint=f"v2/teams/{team_slug}/items/register_upload",
Expand All @@ -36,11 +39,17 @@ def register_data(
return response

@inject_default_team_slug
def sign_upload(self, dataset_slug: str, upload_id: str, *, team_slug: Optional[str] = None) -> Dict[str, Any]:
return self._client._get(f"v2/teams/{team_slug}/items/uploads/{upload_id}/sign", team_slug=team_slug)
def sign_upload(
self, dataset_slug: str, upload_id: str, *, team_slug: Optional[str] = None
) -> Dict[str, Any]:
return self._client._get(
f"v2/teams/{team_slug}/items/uploads/{upload_id}/sign", team_slug=team_slug
)

@inject_default_team_slug
def confirm_upload(self, dataset_slug: str, upload_id: str, *, team_slug: Optional[str] = None) -> Dict[str, Any]:
def confirm_upload(
self, dataset_slug: str, upload_id: str, *, team_slug: Optional[str] = None
) -> Dict[str, Any]:
return self._client._post(
f"v2/teams/{team_slug}/items/uploads/{upload_id}/confirm",
payload={},
Expand All @@ -49,7 +58,11 @@ def confirm_upload(self, dataset_slug: str, upload_id: str, *, team_slug: Option

@inject_default_team_slug
def fetch_items(
self, dataset_id: int, cursor: Union[Dict[str, Any], List[Tuple[str, Any]]], *, team_slug: Optional[str] = None
self,
dataset_id: int,
cursor: Union[Dict[str, Any], List[Tuple[str, Any]]],
*,
team_slug: Optional[str] = None,
) -> Dict[str, Any]:
"""
Fetch the remote items from the given dataset.
Expand All @@ -75,10 +88,14 @@ def fetch_items(

cursor.append(("dataset_ids[]", dataset_id))

return self._client._get(f"/v2/teams/{team_slug}/items?{parse.urlencode(cursor, True)}", team_slug)
return self._client._get(
f"/v2/teams/{team_slug}/items?{parse.urlencode(cursor, True)}", team_slug
)

@inject_default_team_slug
def archive_items(self, payload: Dict[str, Any], *, team_slug: Optional[str] = None) -> None:
def archive_items(
self, payload: Dict[str, Any], *, team_slug: Optional[str] = None
) -> None:
"""
Archives the item from the given dataset.

Expand All @@ -92,7 +109,9 @@ def archive_items(self, payload: Dict[str, Any], *, team_slug: Optional[str] = N
self._client._post(f"v2/teams/{team_slug}/items/archive", payload, team_slug)

@inject_default_team_slug
def restore_archived_items(self, payload: Dict[str, Any], *, team_slug: Optional[str] = None) -> None:
def restore_archived_items(
self, payload: Dict[str, Any], *, team_slug: Optional[str] = None
) -> None:
"""
Restores the archived item from the given dataset.

Expand All @@ -107,7 +126,12 @@ def restore_archived_items(self, payload: Dict[str, Any], *, team_slug: Optional

@inject_default_team_slug
def move_to_stage(
self, filters: Dict[str, Any], stage_id: str, workflow_id: str, *, team_slug: Optional[str] = None
self,
filters: Dict[str, Any],
stage_id: str,
workflow_id: str,
*,
team_slug: Optional[str] = None,
) -> None:
"""
Moves the given items to the specified stage
Expand All @@ -125,16 +149,22 @@ def move_to_stage(
self._client._post_raw(f"v2/teams/{team_slug}/items/stage", payload, team_slug)

@inject_default_team_slug
def get_dataset(self, id: str, *, team_slug: Optional[str] = None) -> Dict[str, Any]:
def get_dataset(
self, id: str, *, team_slug: Optional[str] = None
) -> Dict[str, Any]:
return self._client._get(f"datasets/{id}", team_slug)

@inject_default_team_slug
def get_workflow(self, id: str, *, team_slug: Optional[str] = None) -> Dict[str, Any]:
def get_workflow(
self, id: str, *, team_slug: Optional[str] = None
) -> Dict[str, Any]:
return self._client._get(f"v2/teams/{team_slug}/workflows/{id}", team_slug)

@inject_default_team_slug
def delete_items(self, filters, *, team_slug: Optional[str] = None):
self._client._delete(f"v2/teams/{team_slug}/items", {"filters": filters}, team_slug)
self._client._delete(
f"v2/teams/{team_slug}/items", {"filters": filters}, team_slug
)

@inject_default_team_slug
def export_dataset(
Expand All @@ -159,27 +189,39 @@ def export_dataset(
payload["format"] = format

if annotation_class_ids:
payload["annotation_filters"] = {"annotation_class_ids": list(map(int, annotation_class_ids))}
payload["annotation_filters"] = {
"annotation_class_ids": list(map(int, annotation_class_ids))
}
if filters is not None:
# Backend assumes default filters only if those are completely missing.
payload["filters"] = filters

return self._client._post(f"v2/teams/{team_slug}/datasets/{dataset_slug}/exports", payload, team_slug)
return self._client._post(
f"v2/teams/{team_slug}/datasets/{dataset_slug}/exports", payload, team_slug
)

def get_exports(self, dataset_slug, *, team_slug: Optional[str] = None):
return self._client._get(f"v2/teams/{team_slug}/datasets/{dataset_slug}/exports", team_slug)
return self._client._get(
f"v2/teams/{team_slug}/datasets/{dataset_slug}/exports", team_slug
)

@inject_default_team_slug
def post_comment(self, item_id, text, x, y, w, h, slot_name, team_slug: Optional[str] = None):
def post_comment(
self, item_id, text, x, y, w, h, slot_name, team_slug: Optional[str] = None
):
payload = {
"bounding_box": {"h": h, "w": w, "x": x, "y": y},
"comments": [{"body": text}],
"slot_name": slot_name,
}
return self._client._post(f"v2/teams/{team_slug}/items/{item_id}/comment_threads", payload, team_slug)
return self._client._post(
f"v2/teams/{team_slug}/items/{item_id}/comment_threads", payload, team_slug
)

@inject_default_team_slug
def import_annotation(self, item_id: ItemId, payload: Dict[str, Any], team_slug: Optional[str] = None) -> None:
def import_annotation(
self, item_id: ItemId, payload: Dict[str, Any], team_slug: Optional[str] = None
) -> None:
"""
Imports the annotation for the item with the given id.

Expand All @@ -192,4 +234,6 @@ def import_annotation(self, item_id: ItemId, payload: Dict[str, Any], team_slug:
`{"annotations": serialized_annotations, "overwrite": "false"}`
"""

return self._client._post_raw(f"v2/teams/{team_slug}/items/{item_id}/import", payload=payload)
return self._client._post_raw(
f"v2/teams/{team_slug}/items/{item_id}/import", payload=payload
)
34 changes: 27 additions & 7 deletions darwin/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,20 @@ def main() -> None:
except Unauthenticated:
f._error("You need to specify a valid API key to do that action.")
except InvalidTeam:
f._error("The team specified is not in the configuration, please authenticate first.")
f._error(
"The team specified is not in the configuration, please authenticate first."
)
except requests.exceptions.ConnectionError:
f._error("Darwin seems unreachable, please try again in a minute or contact support.")
f._error(
"Darwin seems unreachable, please try again in a minute or contact support."
)
except GracefulExit as e:
f._error(e.message)
except Exception: # Catch unhandled exceptions
console = Console()
console.print("An unexpected error occurred, please contact support, and send them the file.")
console.print(
"An unexpected error occurred, please contact support, and send them the file."
)
console.print_exception()

exit(255)
Expand All @@ -67,7 +73,9 @@ def _run(args: Namespace, parser: ArgumentParser) -> None:
api_key = getpass.getpass(prompt="API key: ", stream=None)
api_key = api_key.strip()
if api_key == "":
print("API Key needed, generate one for your team: https://darwin.v7labs.com/?settings=api-keys")
print(
"API Key needed, generate one for your team: https://darwin.v7labs.com/?settings=api-keys"
)
return
f.authenticate(api_key)
print("Authentication succeeded.")
Expand Down Expand Up @@ -121,10 +129,17 @@ def _run(args: Namespace, parser: ArgumentParser) -> None:
f.dataset_report(args.dataset, args.granularity or "day", args.pretty)
elif args.action == "export":
f.export_dataset(
args.dataset, args.include_url_token, args.name, args.class_ids, args.include_authorship, args.version
args.dataset,
args.include_url_token,
args.name,
args.class_ids,
args.include_authorship,
args.version,
)
elif args.action == "files":
f.list_files(args.dataset, args.status, args.path, args.only_filenames, args.sort_by)
f.list_files(
args.dataset, args.status, args.path, args.only_filenames, args.sort_by
)
elif args.action == "releases":
f.dataset_list_releases(args.dataset)
elif args.action == "pull":
Expand Down Expand Up @@ -170,7 +185,12 @@ def _run(args: Namespace, parser: ArgumentParser) -> None:
)
# Annotation schema validation
elif args.command == "validate":
f.validate_schemas(location=args.location, pattern=args.pattern, silent=args.silent, output=args.output)
f.validate_schemas(
location=args.location,
pattern=args.pattern,
silent=args.silent,
output=args.output,
)


if __name__ == "__main__":
Expand Down
Loading
Loading