Skip to content

Commit

Permalink
Add Label functionallity (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodbare authored Jul 27, 2023
1 parent 30fd8d0 commit a47ab24
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 7 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"cSpell.words": [
"anonymization",
"knowledgeboxes",
"labelsets",
"Serviceaccount",
"tqdm"
]
Expand Down
86 changes: 81 additions & 5 deletions nuclia/sdk/kb.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from typing import List, Optional
from warnings import warn

from nucliadb_models.labels import KnowledgeBoxLabels, Label, LabelSet, LabelSetKind
from nucliadb_models.resource import Resource, ResourceList

from nuclia.data import get_auth
from nuclia.decorators import kb, pretty
from nuclia.lib.kb import NucliaDBClient
from nuclia.sdk.auth import NucliaAuth
from nuclia.sdk.logger import logger
from nuclia.sdk.resource import NucliaResource
Expand All @@ -23,12 +26,85 @@ def __init__(self):
self.resource = NucliaResource()

@kb
def list(self, **kwargs):
ndb = kwargs["ndb"]
def list(self, **kwargs) -> None:
ndb: NucliaDBClient = kwargs["ndb"]
data: ResourceList = ndb.ndb.list_resources(kbid=ndb.kbid)
for resource in data.resources:
print(f"{resource.id} {resource.icon:30} {resource.title}")

@kb
def get_labelset(
self,
*,
labelset: str,
**kwargs,
) -> LabelSet:
ndb: NucliaDBClient = kwargs["ndb"]
return ndb.ndb.get_labelset(kbid=ndb.kbid, labelset=labelset)

@kb
def add_labelset(
self,
*,
labelset: str,
kind: LabelSetKind = LabelSetKind.RESOURCES,
multiple: bool = True,
title: Optional[str] = None,
color: Optional[str] = None,
labels: Optional[List[str]] = None,
**kwargs,
):
ndb: NucliaDBClient = kwargs["ndb"]
if labels is None:
labels_list = []
else:
labels_list = [Label(title=label) for label in labels]

labelset_obj = LabelSet(
title=title if title is not None else labelset,
color=color if color is not None else "blue",
labels=labels_list,
kind=[kind],
multiple=multiple,
)

ndb.ndb.set_labelset(kbid=ndb.kbid, labelset=labelset, content=labelset_obj)

@kb
def list_labelsets(self, **kwargs) -> KnowledgeBoxLabels:
ndb: NucliaDBClient = kwargs["ndb"]
data: KnowledgeBoxLabels = ndb.ndb.get_labelsets(kbid=ndb.kbid)
return data

@kb
def del_labelset(self, *, labelset: str, **kwargs):
ndb: NucliaDBClient = kwargs["ndb"]
ndb.ndb.delete_labelset(kbid=ndb.kbid, labelset=labelset)

@kb
def add_label(
self,
*,
labelset: str,
label: str,
text: Optional[str] = None,
uri: Optional[str] = None,
**kwargs,
):
ndb: NucliaDBClient = kwargs["ndb"]
labelset_obj: LabelSet = ndb.ndb.get_labelset(kbid=ndb.kbid, labelset=labelset)
label_obj = Label(title=label, text=text, uri=uri)
labelset_obj.labels.append(label_obj)
ndb.ndb.set_labelset(kbid=ndb.kbid, labelset=labelset, content=labelset_obj)

@kb
def del_label(self, *, labelset: str, label: str, **kwargs):
ndb: NucliaDBClient = kwargs["ndb"]
labelset_obj: LabelSet = ndb.ndb.get_labelset(kbid=ndb.kbid, labelset=labelset)
label_to_delete = next(x for x in labelset_obj.labels if x.title == label)
labelset_obj.labels.remove(label_to_delete)
ndb.ndb.set_labelset(kbid=ndb.kbid, labelset=labelset, content=labelset_obj)

@kb
@pretty
def get_resource_by_id(self, *, rid: str, **kwargs) -> Resource:
Expand All @@ -37,7 +113,7 @@ def get_resource_by_id(self, *, rid: str, **kwargs) -> Resource:
DeprecationWarning,
)
logger.warning("get_resource_by_slug is deprecated, use resource.get instead")
ndb = kwargs["ndb"]
ndb: NucliaDBClient = kwargs["ndb"]
return ndb.ndb.get_resource_by_id(
kbid=ndb.kbid, rid=rid, query_params={"show": "values"}
)
Expand All @@ -50,7 +126,7 @@ def get_resource_by_slug(self, *, slug: str, **kwargs) -> Resource:
DeprecationWarning,
)
logger.warning("get_resource_by_slug is deprecated, use resource.get instead")
ndb = kwargs["ndb"]
ndb: NucliaDBClient = kwargs["ndb"]
return ndb.ndb.get_resource_by_slug(
kbid=ndb.kbid, slug=slug, query_params={"show": "values"}
)
Expand All @@ -59,5 +135,5 @@ def get_resource_by_slug(self, *, slug: str, **kwargs) -> Resource:
def delete(self, *, rid: str, **kwargs):
warn("delete is deprecated, use resource.delete instead", DeprecationWarning)
logger.warning("delete is deprecated, use resource.delete instead")
ndb = kwargs["ndb"]
ndb: NucliaDBClient = kwargs["ndb"]
ndb.ndb.delete_resource(kbid=ndb.kbid, rid=rid)
25 changes: 25 additions & 0 deletions nuclia/tests/test_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from nuclia.sdk.kb import NucliaKB


def test_labels(testing_config):
nkb = NucliaKB()
nkb.add_label(labelset="labelset1", label="label1")
nkb.add_labelset(labelset="labelset1")
nkb.add_label(labelset="labelset1", label="label1")
nkb.add_label(labelset="labelset1", label="label2")
nkb.del_labelset(labelset="labelset2")
labelset = nkb.get_labelset(labelset="labelset1")
assert len(labelset.labels) == 2

nkb.del_label(labelset="labelset1", label="label2")

labelset = nkb.get_labelset(labelset="labelset1")
assert len(labelset.labels) == 1
assert labelset.labels[0].title == "label1"

labelsets = nkb.list_labelsets()
assert len(labelsets.labelsets) == 1
assert "labelset1" in labelsets.labelsets
nkb.del_labelset(labelset="labelset1")
labelsets = nkb.list_labelsets()
assert len(labelsets.labelsets) == 0
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ pydantic<2.0.0
pyyaml==5.3.1
requests
gnureadline
nucliadb_sdk>=2.17.0
nucliadb_models>=2.17.0
nucliadb_sdk>=2.18.0
nucliadb_models>=2.18.0
tqdm

0 comments on commit a47ab24

Please sign in to comment.