18 from enum
import IntEnum
19 from typing
import Optional, Dict, Any
20 from abc
import abstractmethod, ABC
26 from ..networking
import networkManager, NetworkRequestError
29 class EntityTagType(IntEnum):
42 def entityTagType(self) -> EntityTagType:
45 def _getTagId(self, tagName: str) -> Optional[int]:
48 "type": self.entityTagType.value,
49 "project_id": self.projectId
52 response = networkManager.get(
"tag", parameters)
53 if response.hasFailed():
54 raise NetworkRequestError(response,
"Failed to check existing tags")
56 tags = response.getJson(dict).get(
"data")
57 if not isinstance(tags, list):
58 raise NetworkRequestError(response, f
"Field \"data\" from tag response must be dict, but got {type(tags)} instead")
63 if not isinstance(tags[0], dict):
64 raise NetworkRequestError(response, f
"Tag object from response must be dict, but got {type(tags[0])} instead")
66 tagId = tags[0].get(
"id")
67 if not isinstance(tagId, int):
68 raise NetworkRequestError(response, f
"Tag object from response must have field id of type int, but got {type(tagId)} instead")
73 def addTag(self, tag: str, color: Optional[str] =
None) ->
None:
75 Add a tag to this entity
82 a hexadecimal color code for the new tag\n
83 if tag already exists in project, this will be ignored\n
84 if left empty and tag does not already exist, a random color will be picked
89 if tag name or color are invalid
91 if request to add tag failed
94 if re.match(
r"^[a-z0-9-]{1,30}$", tag)
is None:
95 raise ValueError(
">> [Coretex] Tag has to be alphanumeric")
98 color = f
"#{random.randint(0, 0xFFFFFF):06x}"
100 if re.match(
r"^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})$", color)
is None:
101 raise ValueError(
">> [Coretex] Tag color has to follow hexadecimal color code")
103 tags: Dict[str, Any] = {}
105 tagId = self._getTagId(tag)
106 if tagId
is not None:
107 tags[
"existing"] = [tagId]
115 "entity_id": self.id,
116 "type": self.entityTagType.value,
120 response = networkManager.post(
"tag/entity", parameters)
121 if response.hasFailed():
122 raise NetworkRequestError(response,
"Failed to create tag")
124 def removeTag(self, tag: str) ->
None:
126 Remove tag with provided name from the entity
136 if tag removal request failed
139 tagId = self._getTagId(tag)
141 logging.error(f
">> [Coretex] Tag \"{tag}\" not found on entity id {self.id}")
145 "entity_id": self.id,
147 "type": self.entityTagType.value
150 response = networkManager.post(
"tag/remove", parameters)
151 if response.hasFailed():
152 raise NetworkRequestError(response,
"Failed to remove tag")