-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from hasty-ai/kp-add-tags
Add image tags
- Loading branch information
Showing
8 changed files
with
345 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
from collections import OrderedDict | ||
|
||
from .exception import LimitExceededException, ValidationException | ||
from .hasty_object import HastyObject | ||
from .tag_class import TagClass | ||
|
||
|
||
C_TAGS_LIMIT = 100 | ||
|
||
|
||
class Tag(HastyObject): | ||
endpoint = '/v1/projects/{project_id}/images/{image_id}/tags' | ||
|
||
def __repr__(self): | ||
return self.get__repr__(OrderedDict({"id": self._id, "tag_class_id": self._tag_class_id, | ||
"tag_class_name": self._tag_class_name, "image_id": self._image_id})) | ||
|
||
@property | ||
def id(self): | ||
""" | ||
:type: string | ||
""" | ||
return self._id | ||
|
||
@property | ||
def tag_class_id(self): | ||
""" | ||
:type: string | ||
""" | ||
return self._tag_class_id | ||
|
||
@property | ||
def project_id(self): | ||
""" | ||
:type: string | ||
""" | ||
return self._project_id | ||
|
||
@property | ||
def image_id(self): | ||
""" | ||
:type: string | ||
""" | ||
return self._image_id | ||
|
||
@property | ||
def tag_class_name(self): | ||
""" | ||
:type: string | ||
""" | ||
return self._tag_class_name | ||
|
||
def _init_properties(self): | ||
self._id = None | ||
self._tag_class_id = None | ||
self._tag_class_name = None | ||
self._project_id = None | ||
self._image_id = None | ||
|
||
def _set_prop_values(self, data): | ||
if "id" in data: | ||
self._id = data["id"] | ||
if "name" in data: | ||
self._name = data["name"] | ||
if "project_id" in data: | ||
self._project_id = data["project_id"] | ||
if "norder" in data: | ||
self._norder = data["norder"] | ||
|
||
@staticmethod | ||
def _batch_create(requester, project_id, image_id, tags): | ||
if len(tags) == 0: | ||
return [] | ||
data = [] | ||
if len(tags) > C_TAGS_LIMIT: | ||
raise LimitExceededException.max_tags_per_batch(len(tags)) | ||
for tag in tags: | ||
if isinstance(tag, TagClass): | ||
data.append({"tag_class_id": tag.id}) | ||
elif "tag_class_id" not in tag: | ||
raise ValidationException(f"tag_class_id property should be defined") | ||
else: | ||
data.append({"tag_class_id": tag["tag_class_id"]}) | ||
res = requester.post(Tag.endpoint.format(project_id=project_id, image_id=image_id), json_data=data) | ||
new_tags = [] | ||
for tag in res: | ||
new_tags.append(Tag(requester, tag, {"project_id": project_id, "image_id": image_id})) | ||
return new_tags | ||
|
||
@staticmethod | ||
def _batch_delete(requester, project_id, image_id, tags): | ||
data = [] | ||
for tag in tags: | ||
if isinstance(tag, TagClass): | ||
data.append({"tag_class_id": tag.id}) | ||
elif isinstance(tag, Tag): | ||
data.append({"tag_id": tag.id}) | ||
elif "tag_class_id" not in tag and "tag_id" not in tag: | ||
raise ValidationException(f"tag_class_id or tag_id property should be defined") | ||
else: | ||
data.append({"tag_id": tag.get("tag_id"), | ||
"tag_class_id": tag.get("tag_class_id")}) | ||
requester.delete(Tag.endpoint.format(project_id=project_id, image_id=image_id), json_data=data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from collections import OrderedDict | ||
|
||
from .hasty_object import HastyObject | ||
|
||
|
||
class TagClass(HastyObject): | ||
endpoint = '/v1/projects/{project_id}/tag_classes' | ||
endpoint_class = '/v1/projects/{project_id}/tag_classes/{tag_class_id}' | ||
|
||
def __repr__(self): | ||
return self.get__repr__(OrderedDict({"id": self._id, "name": self._name, "color": self._color, | ||
"type": self._class_type, "norder": self._norder})) | ||
|
||
@property | ||
def id(self): | ||
""" | ||
:type: string | ||
""" | ||
return self._id | ||
|
||
@property | ||
def name(self): | ||
""" | ||
:type: string | ||
""" | ||
return self._name | ||
|
||
@property | ||
def project_id(self): | ||
""" | ||
:type: string | ||
""" | ||
return self._project_id | ||
|
||
@property | ||
def norder(self): | ||
""" | ||
:type: float | ||
""" | ||
return self._norder | ||
|
||
def _init_properties(self): | ||
self._id = None | ||
self._name = None | ||
self._project_id = None | ||
self._norder = None | ||
|
||
def _set_prop_values(self, data): | ||
if "id" in data: | ||
self._id = data["id"] | ||
if "name" in data: | ||
self._name = data["name"] | ||
if "project_id" in data: | ||
self._project_id = data["project_id"] | ||
if "norder" in data: | ||
self._norder = data["norder"] | ||
|
||
@staticmethod | ||
def _create(requester, project_id, name, norder=None): | ||
res = requester.post(TagClass.endpoint.format(project_id=project_id), | ||
json_data={"name": name, | ||
"norder": norder}) | ||
return TagClass(requester, res, {"project_id": project_id}) | ||
|
||
def edit(self, name, norder=None): | ||
""" | ||
Edit tag class properties | ||
Arguments: | ||
name (str): Taп class name | ||
norder (float, optional): Order in the Hasty tool | ||
""" | ||
self._requester.put(TagClass.endpoint_class.format(project_id=self.project_id, tag_class_id=self.id), | ||
json_data={"name": name, | ||
"norder": norder}) | ||
self._name = name | ||
self._norder = norder | ||
|
||
def delete(self): | ||
""" | ||
Deletes tag class | ||
""" | ||
self._requester.delete(TagClass.endpoint_class.format(project_id=self.project_id, tag_class_id=self.id)) | ||
|
Oops, something went wrong.