|
| 1 | +import uuid |
| 2 | + |
| 3 | +import orjson |
| 4 | +import structlog |
| 5 | +from py_semantic_taxonomy.adapters.routers import request_dto as req |
| 6 | +from py_semantic_taxonomy.domain import entities |
| 7 | +from py_semantic_taxonomy.domain.constants import XKOS, AssociationKind |
| 8 | +from py_semantic_taxonomy.domain.url_utils import get_full_api_path |
| 9 | + |
| 10 | +from pyst_client.simple.classes.api_base import APIBase |
| 11 | +from pyst_client.simple.classes.concept import Concept |
| 12 | +from pyst_client.simple.classes.correspondence import Correspondence |
| 13 | +from pyst_client.simple.client import get_write_client |
| 14 | +from pyst_client.simple.settings import can_write, settings |
| 15 | + |
| 16 | +logger = structlog.get_logger("pyst-client") |
| 17 | + |
| 18 | + |
| 19 | +class Association(entities.Association, APIBase): |
| 20 | + def open_new_tab(self) -> None: |
| 21 | + raise NotImplementedError |
| 22 | + |
| 23 | + @property |
| 24 | + def api_path(self) -> str: |
| 25 | + return get_full_api_path("association", iri=self.id_) |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def get_one( |
| 29 | + cls, |
| 30 | + iri: str, |
| 31 | + timeout: int = 5, |
| 32 | + ) -> "Association": |
| 33 | + return cls.from_json_ld( |
| 34 | + orjson.loads(cls._get_one("association", iri, timeout=timeout).text) |
| 35 | + ) |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def get_many( |
| 39 | + cls, |
| 40 | + correspondence: Correspondence | str | None = None, |
| 41 | + source_concept: Concept | str | None = None, |
| 42 | + target_concept: Concept | str | None = None, |
| 43 | + kind: AssociationKind | None = None, |
| 44 | + timeout: int = 5, |
| 45 | + ) -> list["Concept"]: |
| 46 | + params = { |
| 47 | + "correspondence_iri": ( |
| 48 | + correspondence.id_ |
| 49 | + if isinstance(correspondence, Correspondence) |
| 50 | + else correspondence |
| 51 | + ), |
| 52 | + "source_concept_iri": ( |
| 53 | + source_concept.id_ |
| 54 | + if isinstance(source_concept, Concept) |
| 55 | + else source_concept |
| 56 | + ), |
| 57 | + "target_concept_iri": ( |
| 58 | + target_concept.id_ |
| 59 | + if isinstance(target_concept, Concept) |
| 60 | + else target_concept |
| 61 | + ), |
| 62 | + "kind": kind or AssociationKind.simple, |
| 63 | + } |
| 64 | + return [ |
| 65 | + cls.from_json_ld(obj) |
| 66 | + for obj in orjson.loads( |
| 67 | + cls._get_many( |
| 68 | + "association_all", |
| 69 | + params={ |
| 70 | + key: value for key, value in params.items() if value is not None |
| 71 | + }, |
| 72 | + timeout=timeout, |
| 73 | + ).text |
| 74 | + ) |
| 75 | + ] |
| 76 | + |
| 77 | + @classmethod |
| 78 | + @can_write |
| 79 | + def create( |
| 80 | + cls, |
| 81 | + *, |
| 82 | + correspondence: Correspondence, |
| 83 | + source_concepts: list[Concept | dict], |
| 84 | + target_concepts: list[Concept | dict], |
| 85 | + extra: dict | None = None, |
| 86 | + id_: str | None = None, |
| 87 | + ) -> "Concept": |
| 88 | + if id_ is None: |
| 89 | + id_ = "{}{}{}/{}".format( |
| 90 | + settings.creation_base_url, |
| 91 | + "" if settings.creation_base_url.endswith("/") else "/", |
| 92 | + correspondence.notations[0]["@value"], |
| 93 | + uuid.uuid4().hex, |
| 94 | + ) |
| 95 | + extra = extra or {} |
| 96 | + extra[f"{XKOS}Correspondence"] = correspondence.id_ |
| 97 | + assoc = cls( |
| 98 | + id_=id_, |
| 99 | + types=[f"{XKOS}ConceptAssociation"], |
| 100 | + source_concepts=[ |
| 101 | + ({"@id": obj.id_} if isinstance(obj, Concept) else obj) |
| 102 | + for obj in source_concepts |
| 103 | + ], |
| 104 | + target_concepts=[ |
| 105 | + ({"@id": obj.id_} if isinstance(obj, Concept) else obj) |
| 106 | + for obj in target_concepts |
| 107 | + ], |
| 108 | + kind=( |
| 109 | + AssociationKind.simple |
| 110 | + if len(source_concepts) == 1 |
| 111 | + else AssociationKind.conditional |
| 112 | + ), |
| 113 | + extra=extra or {}, |
| 114 | + ) |
| 115 | + req.Association(**assoc.to_json_ld()) |
| 116 | + return assoc |
| 117 | + |
| 118 | + def save(self) -> None: |
| 119 | + client = get_write_client() |
| 120 | + client.post( |
| 121 | + self.api_path, |
| 122 | + data=self.json(), |
| 123 | + ) |
| 124 | + # Add association to correspondence |
| 125 | + if f"{XKOS}Correspondence" in self.extra: |
| 126 | + client.post( |
| 127 | + get_full_api_path("made_of"), |
| 128 | + data=orjson.dumps( |
| 129 | + entities.MadeOf( |
| 130 | + id_=self.extra[f"{XKOS}Correspondence"], |
| 131 | + made_ofs=[{"@id": self.id_}], |
| 132 | + ).to_json_ld() |
| 133 | + ), |
| 134 | + ) |
| 135 | + else: |
| 136 | + logger.warning( |
| 137 | + "Unable to automatically add this association to a `Correspondence` object" |
| 138 | + ) |
| 139 | + |
| 140 | + def delete(self) -> None: |
| 141 | + client = get_write_client() |
| 142 | + return client.delete( |
| 143 | + self.api_path, |
| 144 | + ) |
| 145 | + if f"{XKOS}Correspondence" in self.extra: |
| 146 | + client.request( |
| 147 | + url=get_full_api_path("made_of"), |
| 148 | + method="DELETE", |
| 149 | + content=orjson.dumps( |
| 150 | + entities.MadeOf( |
| 151 | + id_=self.extra[f"{XKOS}Correspondence"], |
| 152 | + made_ofs=[{"@id": self.id_}], |
| 153 | + ).to_json_ld() |
| 154 | + ), |
| 155 | + ) |
| 156 | + else: |
| 157 | + logger.warning( |
| 158 | + "Unable to automatically remove this association from a `Correspondence` object" |
| 159 | + ) |
0 commit comments