Skip to content

Commit

Permalink
[ENG-6437] GV: Add new flags to ItemResult class (#139)
Browse files Browse the repository at this point in the history
 added `can_be_root` and `may_contain_root_candidates` properties for storage addons ItemResult
  • Loading branch information
opaduchak authored Nov 4, 2024
1 parent a275e79 commit 91667f1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 32 deletions.
35 changes: 5 additions & 30 deletions addon_imps/storage/dataverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import asyncio
import re
from dataclasses import (
dataclass,
fields,
)
from dataclasses import dataclass

from addon_toolkit.interfaces import storage
from addon_toolkit.interfaces.storage import (
Expand Down Expand Up @@ -154,6 +151,7 @@ def parse_dataverse(data: dict):
item_type=ItemType.FOLDER,
item_name=data["name"],
item_id=f'dataverse/{data["id"]}',
can_be_root=False,
)


Expand All @@ -166,6 +164,8 @@ def parse_mydata(data: dict):
item_id=f"dataverse/{file['entity_id']}",
item_name=file["name"],
item_type=ItemType.FOLDER,
can_be_root=False,
may_contain_root_candidates=True,
)
for file in data["items"]
],
Expand All @@ -174,32 +174,6 @@ def parse_mydata(data: dict):
)


@dataclass(frozen=True, slots=True)
class Dataverse:
name: str
id: str
type: str

@property
def item_result(self) -> ItemResult:
return ItemResult(
item_id=f"{self.type}/{self.id}",
item_name=self.name,
item_type=self.item_type,
)

@property
def item_type(self) -> ItemType:
if self.type == "datafile":
return ItemType.FILE
else:
return ItemType.FOLDER

@classmethod
def from_json(cls, json: dict):
return cls(**{key.name: json.get(key.name, key.default) for key in fields(cls)})


def parse_dataset(data: dict) -> ItemResult:
if data.get("data"):
data = data["data"]
Expand All @@ -214,6 +188,7 @@ def parse_dataset(data: dict) -> ItemResult:
if item["typeName"] == "title"
][0]["value"],
item_type=ItemType.FOLDER,
may_contain_root_candidates=False,
)
except (KeyError, IndexError) as e:
raise ValueError(f"Invalid dataset response: {e=}")
Expand Down
1 change: 1 addition & 0 deletions addon_imps/storage/figshare.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def item_result(self) -> ItemResult:
item_id=f"article/{self.id}",
item_name=self.title,
item_type=ItemType.FOLDER,
may_contain_root_candidates=False,
)

@classmethod
Expand Down
3 changes: 3 additions & 0 deletions addon_imps/storage/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,14 @@ def _parse_github_item(self, item_json: dict, full_name: str) -> storage.ItemRes
item_id=item_id,
item_name=item_json["name"],
item_type=item_type,
may_contain_root_candidates=False,
can_be_root=False,
)

def _parse_github_repo(self, repo_json: dict) -> storage.ItemResult:
return storage.ItemResult(
item_id=repo_json["full_name"] + ":",
item_name=repo_json["name"],
item_type=ItemType.FOLDER,
may_contain_root_candidates=False,
)
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class TestAddonOperationInvocationCreate(APITestCase):
"item_id": "hello",
"item_name": "Hello!?",
"item_type": "FOLDER",
"can_be_root": True,
"may_contain_root_candidates": True,
}
],
"total_count": 1,
Expand Down
10 changes: 9 additions & 1 deletion addon_toolkit/interfaces/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,17 @@ class ItemResult:
item_id: str
item_name: str
item_type: ItemType
can_be_root: bool = None
may_contain_root_candidates: bool = None
item_path: abc.Sequence[typing.Self] | None = None

def __post_init__(self):
"""By default can_be_root and may_contain_root_candidates are bound to item_type"""
if self.can_be_root is None:
self.can_be_root = self.item_type == ItemType.FOLDER
if self.may_contain_root_candidates is None:
self.may_contain_root_candidates = self.item_type == ItemType.FOLDER


@dataclasses.dataclass
class PossibleSingleItemResult:
Expand Down Expand Up @@ -82,7 +91,6 @@ def with_cursor(self, cursor: Cursor) -> typing.Self:


class StorageAddonInterface(BaseAddonInterface, typing.Protocol):

###
# single-item operations:

Expand Down
4 changes: 3 additions & 1 deletion addon_toolkit/json_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ def _build_for_annotation(self, annotation: type) -> dict:
return {"type": "number"}
if _type is dict:
return {"type": "dict"}
if _type is bool:
return {"type": "bool"}
raise exceptions.TypeNotJsonable(_type)


Expand Down Expand Up @@ -220,7 +222,7 @@ def json_for_typed_value(
if value not in _type:
raise exceptions.ValueNotJsonableWithType(value, _type)
return value.name
if _type in (str, int, float): # check str before abc.Collection
if _type in (str, int, float, bool): # check str before abc.Collection
if not isinstance(value, (str, int, float)):
raise exceptions.ValueNotJsonableWithType(value, _type)
assert issubclass(_type, (str, int, float)) # assertion for type-checker
Expand Down

0 comments on commit 91667f1

Please sign in to comment.