personal_knowledge_library 3.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of personal_knowledge_library might be problematic. Click here for more details.

Files changed (42) hide show
  1. knowledge/__init__.py +91 -0
  2. knowledge/base/__init__.py +22 -0
  3. knowledge/base/access.py +167 -0
  4. knowledge/base/entity.py +267 -0
  5. knowledge/base/language.py +27 -0
  6. knowledge/base/ontology.py +2734 -0
  7. knowledge/base/search.py +473 -0
  8. knowledge/base/tenant.py +192 -0
  9. knowledge/nel/__init__.py +11 -0
  10. knowledge/nel/base.py +495 -0
  11. knowledge/nel/engine.py +123 -0
  12. knowledge/ontomapping/__init__.py +667 -0
  13. knowledge/ontomapping/manager.py +320 -0
  14. knowledge/public/__init__.py +27 -0
  15. knowledge/public/cache.py +115 -0
  16. knowledge/public/helper.py +373 -0
  17. knowledge/public/relations.py +128 -0
  18. knowledge/public/wikidata.py +1324 -0
  19. knowledge/services/__init__.py +128 -0
  20. knowledge/services/asyncio/__init__.py +7 -0
  21. knowledge/services/asyncio/base.py +458 -0
  22. knowledge/services/asyncio/graph.py +1420 -0
  23. knowledge/services/asyncio/group.py +450 -0
  24. knowledge/services/asyncio/search.py +439 -0
  25. knowledge/services/asyncio/users.py +270 -0
  26. knowledge/services/base.py +533 -0
  27. knowledge/services/graph.py +1897 -0
  28. knowledge/services/group.py +819 -0
  29. knowledge/services/helper.py +142 -0
  30. knowledge/services/ontology.py +1234 -0
  31. knowledge/services/search.py +488 -0
  32. knowledge/services/session.py +444 -0
  33. knowledge/services/tenant.py +281 -0
  34. knowledge/services/users.py +445 -0
  35. knowledge/utils/__init__.py +10 -0
  36. knowledge/utils/graph.py +417 -0
  37. knowledge/utils/wikidata.py +197 -0
  38. knowledge/utils/wikipedia.py +175 -0
  39. personal_knowledge_library-3.0.0.dist-info/LICENSE +201 -0
  40. personal_knowledge_library-3.0.0.dist-info/METADATA +1163 -0
  41. personal_knowledge_library-3.0.0.dist-info/RECORD +42 -0
  42. personal_knowledge_library-3.0.0.dist-info/WHEEL +4 -0
@@ -0,0 +1,450 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright © 2024 Wacom. All rights reserved.
3
+ import asyncio
4
+ import urllib.parse
5
+ from typing import List, Any, Optional, Dict
6
+
7
+ import orjson
8
+
9
+ from knowledge.base.access import GroupAccessRight
10
+ from knowledge.base.ontology import NAME_TAG
11
+ from knowledge.services import (
12
+ GROUP_USER_RIGHTS_TAG,
13
+ JOIN_KEY_PARAM,
14
+ USER_TO_ADD_PARAM,
15
+ USER_TO_REMOVE_PARAM,
16
+ FORCE_PARAM,
17
+ APPLICATION_JSON_HEADER,
18
+ USER_AGENT_HEADER_FLAG,
19
+ )
20
+ from knowledge.services.asyncio.base import AsyncServiceAPIClient, handle_error
21
+ from knowledge.services.graph import AUTHORIZATION_HEADER_FLAG, CONTENT_TYPE_HEADER_FLAG
22
+ from knowledge.services.group import Group, GroupManagementService, GroupInfo
23
+
24
+ # -------------------------------------- Constant flags ----------------------------------------------------------------
25
+ from knowledge.services.users import FORCE_TAG, LIMIT_TAG, OFFSET_TAG
26
+
27
+
28
+ class AsyncGroupManagementService(AsyncServiceAPIClient):
29
+ """
30
+ Group Management Service API
31
+ -----------------------------
32
+ The service is managing groups.
33
+
34
+ Functionality:
35
+ - List all groups
36
+ - Create group
37
+ - Assign users to group
38
+ - Share entities with group
39
+
40
+ Parameters
41
+ ----------
42
+ service_url: str
43
+ URL of the service
44
+ service_endpoint: str
45
+ Base endpoint
46
+ """
47
+
48
+ GROUP_ENDPOINT: str = "group"
49
+ """"Endpoint for all group related functionality."""
50
+
51
+ def __init__(
52
+ self,
53
+ application_name: str,
54
+ service_url: str = AsyncServiceAPIClient.SERVICE_URL,
55
+ service_endpoint: str = "graph/v1",
56
+ ):
57
+ super().__init__(application_name=application_name, service_url=service_url, service_endpoint=service_endpoint)
58
+
59
+ # ------------------------------------------ Groups handling ------------------------------------------------------
60
+
61
+ async def create_group(
62
+ self, name: str, rights: GroupAccessRight = GroupAccessRight(read=True), auth_key: Optional[str] = None
63
+ ) -> Group:
64
+ """
65
+ Creates a group.
66
+
67
+ Parameters
68
+ ----------
69
+ auth_key: str
70
+ User key.
71
+ name: str
72
+ Name of the tenant
73
+ rights: GroupAccessRight
74
+ Access rights
75
+ auth_key: Optional[str]
76
+ If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
77
+
78
+ Returns
79
+ -------
80
+ group: Group
81
+ Instance of the group.
82
+
83
+ Raises
84
+ ------
85
+ WacomServiceException
86
+ If the tenant service returns an error code.
87
+ """
88
+ if auth_key is None:
89
+ auth_key, _ = await self.handle_token()
90
+ url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}"
91
+ headers: Dict[str, str] = {
92
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
93
+ CONTENT_TYPE_HEADER_FLAG: APPLICATION_JSON_HEADER,
94
+ USER_AGENT_HEADER_FLAG: self.user_agent,
95
+ }
96
+ payload: Dict[str, str] = {NAME_TAG: name, GROUP_USER_RIGHTS_TAG: rights.to_list()}
97
+ async with AsyncServiceAPIClient.__async_session__() as session:
98
+ async with session.post(url, headers=headers, json=payload, verify_ssl=self.verify_calls) as response:
99
+ if not response.ok:
100
+ raise await handle_error("Creation of group failed.", response, payload=payload, headers=headers)
101
+ group: Dict[str, Any] = await response.json(loads=orjson.loads)
102
+ await asyncio.sleep(0.25 if self.use_graceful_shutdown else 0.0)
103
+ return Group.parse(group)
104
+
105
+ async def update_group(
106
+ self, group_id: str, name: str, rights: GroupAccessRight = GroupAccessRight, auth_key: Optional[str] = None
107
+ ):
108
+ """
109
+ Updates a group.
110
+
111
+ Parameters
112
+ ----------
113
+ auth_key: str
114
+ User key.
115
+ group_id: str
116
+ ID of the group.
117
+ name: str
118
+ Name of the tenant
119
+ rights: GroupAccessRight
120
+ Access rights
121
+ auth_key: Optional[str]
122
+ If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
123
+
124
+ Raises
125
+ ------
126
+ WacomServiceException
127
+ If the tenant service returns an error code.
128
+ """
129
+ if auth_key is None:
130
+ auth_key, _ = await self.handle_token()
131
+ url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}"
132
+ headers: Dict[str, str] = {
133
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
134
+ CONTENT_TYPE_HEADER_FLAG: APPLICATION_JSON_HEADER,
135
+ USER_AGENT_HEADER_FLAG: self.user_agent,
136
+ }
137
+ payload: Dict[str, str] = {NAME_TAG: name, GROUP_USER_RIGHTS_TAG: rights.to_list()}
138
+ async with AsyncServiceAPIClient.__async_session__() as session:
139
+ async with session.patch(url, headers=headers, json=payload, verify_ssl=self.verify_calls) as response:
140
+ if not response.ok:
141
+ raise await handle_error("Update of group failed.", response, payload=payload, headers=headers)
142
+ await asyncio.sleep(0.25 if self.use_graceful_shutdown else 0.0)
143
+
144
+ async def delete_group(self, group_id: str, force: bool = False, auth_key: Optional[str] = None):
145
+ """
146
+ Delete a group.
147
+
148
+ Parameters
149
+ ----------
150
+ group_id: str
151
+ ID of the group.
152
+ force: bool (Default = False)
153
+ If True, the group will be deleted even if it is not empty.
154
+ auth_key: Optional[str]
155
+ If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
156
+
157
+ Raises
158
+ ------
159
+ WacomServiceException
160
+ If the tenant service returns an error code.
161
+ """
162
+ if auth_key is None:
163
+ auth_key, _ = await self.handle_token()
164
+ url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}"
165
+ headers: Dict[str, str] = {
166
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
167
+ USER_AGENT_HEADER_FLAG: self.user_agent,
168
+ }
169
+ params: Dict[str, str] = {FORCE_TAG: str(force).lower()}
170
+ async with AsyncServiceAPIClient.__async_session__() as session:
171
+ async with session.delete(url, headers=headers, params=params, verify_ssl=self.verify_calls) as response:
172
+ if not response.ok:
173
+ raise await handle_error("Deletion of group failed.", response, headers=headers)
174
+ await asyncio.sleep(0.25 if self.use_graceful_shutdown else 0.0)
175
+
176
+ async def listing_groups(
177
+ self, admin: bool = False, limit: int = 20, offset: int = 0, auth_key: Optional[str] = None
178
+ ) -> List[Group]:
179
+ """
180
+ Listing all groups configured for this instance.
181
+
182
+ Parameters
183
+ ----------
184
+ admin: bool (default:= False)
185
+ Uses admin privilege to show all groups of the tenant.
186
+ Requires user to have the role: TenantAdmin
187
+ limit: int (default:= 20)
188
+ Maximum number of groups to return.
189
+ offset: int (default:= 0)
190
+ Offset of the first group to return.
191
+ auth_key: Optional[str]
192
+ If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
193
+
194
+ Returns
195
+ -------
196
+ user: List[Groups]
197
+ List of groups.
198
+
199
+ Raises
200
+ ------
201
+ WacomServiceException
202
+ If the tenant service returns an error code.
203
+ """
204
+ if auth_key is None:
205
+ auth_key, _ = await self.handle_token()
206
+ url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}"
207
+ params: Dict[str, int] = {LIMIT_TAG: str(limit), OFFSET_TAG: str(offset)}
208
+ if admin:
209
+ url += "/admin"
210
+ headers: Dict[str, str] = {
211
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
212
+ USER_AGENT_HEADER_FLAG: self.user_agent,
213
+ }
214
+ async with AsyncServiceAPIClient.__async_session__() as session:
215
+ async with session.get(url, headers=headers, params=params, verify_ssl=self.verify_calls) as response:
216
+ if response.ok:
217
+ groups: List[Dict[str, Any]] = await response.json(loads=orjson.loads)
218
+ else:
219
+ raise await handle_error("Listing of group failed.", response, headers=headers)
220
+ await asyncio.sleep(0.25 if self.use_graceful_shutdown else 0.0)
221
+ return [Group.parse(g) for g in groups]
222
+
223
+ async def group(self, group_id: str, auth_key: Optional[str] = None) -> GroupInfo:
224
+ """Get a group.
225
+
226
+ Parameters
227
+ ----------
228
+ group_id: str
229
+ Group ID
230
+ auth_key: Optional[str]
231
+ If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
232
+
233
+ Returns
234
+ -------
235
+ group: GroupInfo
236
+ Instance of the group information.
237
+
238
+ Raises
239
+ ------
240
+ WacomServiceException
241
+ If the tenant service returns an error code.
242
+ """
243
+ if auth_key is None:
244
+ auth_key, _ = await self.handle_token()
245
+ url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}"
246
+ headers: Dict[str, str] = {
247
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
248
+ USER_AGENT_HEADER_FLAG: self.user_agent,
249
+ }
250
+ async with AsyncServiceAPIClient.__async_session__() as session:
251
+ async with session.get(url, headers=headers, verify_ssl=self.verify_calls) as response:
252
+ if response.ok:
253
+ group: Dict[str, Any] = await response.json(loads=orjson.loads)
254
+ else:
255
+ raise await handle_error("Getting of group information failed.", response, headers=headers)
256
+ return GroupInfo.parse(group)
257
+
258
+ async def join_group(self, group_id: str, join_key: str, auth_key: Optional[str] = None):
259
+ """User joining a group with his auth token.
260
+
261
+ Parameters
262
+ ----------
263
+ group_id: str
264
+ Group ID
265
+ join_key: str
266
+ Key which is used to join the group.
267
+ auth_key: Optional[str]
268
+ If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
269
+
270
+ Raises
271
+ ------
272
+ WacomServiceException
273
+ If the tenant service returns an error code.
274
+ """
275
+ if auth_key is None:
276
+ auth_key, _ = await self.handle_token()
277
+ url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}/join"
278
+ headers: Dict[str, str] = {
279
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
280
+ USER_AGENT_HEADER_FLAG: self.user_agent,
281
+ }
282
+ params: Dict[str, str] = {
283
+ JOIN_KEY_PARAM: join_key,
284
+ }
285
+ async with AsyncServiceAPIClient.__async_session__() as session:
286
+ async with session.post(url, headers=headers, params=params, verify_ssl=self.verify_calls) as response:
287
+ if not response.ok:
288
+ raise await handle_error("Joining of group failed.", response, headers=headers, parameters=params)
289
+ await asyncio.sleep(0.25 if self.use_graceful_shutdown else 0.0)
290
+
291
+ async def leave_group(self, group_id: str, auth_key: Optional[str] = None):
292
+ """User leaving a group with his auth token.
293
+
294
+ Parameters
295
+ ----------
296
+ group_id: str
297
+ Group ID
298
+ auth_key: Optional[str]
299
+ If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
300
+
301
+ Raises
302
+ ------
303
+ WacomServiceException
304
+ If the tenant service returns an error code.
305
+ """
306
+ if auth_key is None:
307
+ auth_key, _ = await self.handle_token()
308
+ url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}/leave"
309
+ headers: Dict[str, str] = {
310
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
311
+ USER_AGENT_HEADER_FLAG: self.user_agent,
312
+ }
313
+ async with AsyncServiceAPIClient.__async_session__() as session:
314
+ async with session.post(url, headers=headers, verify_ssl=self.verify_calls) as response:
315
+ if not response.ok:
316
+ raise await handle_error("Leaving of group failed.", response, headers=headers)
317
+ await asyncio.sleep(0.25 if self.use_graceful_shutdown else 0.0)
318
+
319
+ async def add_user_to_group(self, group_id: str, user_id: str, auth_key: Optional[str] = None):
320
+ """Adding a user to group.
321
+
322
+ Parameters
323
+ ----------
324
+ group_id: str
325
+ Group ID
326
+ user_id: str
327
+ User who is added to the group
328
+ auth_key: Optional[str]
329
+ If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
330
+
331
+ Raises
332
+ ------
333
+ WacomServiceException
334
+ If the tenant service returns an error code.
335
+ """
336
+ if auth_key is None:
337
+ auth_key, _ = await self.handle_token()
338
+ url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}/user/add"
339
+ headers: Dict[str, str] = {
340
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
341
+ USER_AGENT_HEADER_FLAG: self.user_agent,
342
+ }
343
+ params: Dict[str, str] = {
344
+ USER_TO_ADD_PARAM: user_id,
345
+ }
346
+ async with AsyncServiceAPIClient.__async_session__() as session:
347
+ async with session.post(url, headers=headers, params=params, verify_ssl=self.verify_calls) as response:
348
+ if not response.ok:
349
+ raise await handle_error(
350
+ "Adding of user to group failed.", response, headers=headers, parameters=params
351
+ )
352
+ await asyncio.sleep(0.25 if self.use_graceful_shutdown else 0.0)
353
+
354
+ async def remove_user_from_group(
355
+ self, group_id: str, user_id: str, force: bool = False, auth_key: Optional[str] = None
356
+ ):
357
+ """Remove a user from group.
358
+
359
+ Parameters
360
+ ----------
361
+ group_id: str
362
+ Group ID
363
+ user_id: str
364
+ User who is remove from the group
365
+ force: bool
366
+ If true remove user and entities owned by the user if any
367
+ auth_key: Optional[str]
368
+ If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
369
+
370
+ Raises
371
+ ------
372
+ WacomServiceException
373
+ If the tenant service returns an error code.
374
+ """
375
+ if auth_key is None:
376
+ auth_key, _ = await self.handle_token()
377
+ url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}/user/remove"
378
+ headers: Dict[str, str] = {
379
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
380
+ USER_AGENT_HEADER_FLAG: self.user_agent,
381
+ }
382
+ params: Dict[str, str] = {USER_TO_REMOVE_PARAM: user_id, FORCE_PARAM: str(force)}
383
+ async with AsyncServiceAPIClient.__async_session__() as session:
384
+ async with session.post(url, headers=headers, params=params, verify_ssl=self.verify_calls) as response:
385
+ if not response.ok:
386
+ raise await handle_error(
387
+ "Removing of user from group failed.", response, headers=headers, parameters=params
388
+ )
389
+ await asyncio.sleep(0.25 if self.use_graceful_shutdown else 0.0)
390
+
391
+ async def add_entity_to_group(self, group_id: str, entity_uri: str, auth_key: Optional[str] = None):
392
+ """Adding an entity to group.
393
+
394
+ Parameters
395
+ ----------
396
+ group_id: str
397
+ Group ID
398
+ entity_uri: str
399
+ Entities URI
400
+ auth_key: Optional[str]
401
+ If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
402
+ Raises
403
+ ------
404
+ WacomServiceException
405
+ If the tenant service returns an error code.
406
+ """
407
+ if auth_key is None:
408
+ auth_key, _ = await self.handle_token()
409
+ uri: str = urllib.parse.quote(entity_uri)
410
+ url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}/entity/{uri}/add"
411
+ headers: Dict[str, str] = {
412
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
413
+ USER_AGENT_HEADER_FLAG: self.user_agent,
414
+ }
415
+ async with AsyncServiceAPIClient.__async_session__() as session:
416
+ async with session.post(url, headers=headers, verify_ssl=self.verify_calls) as response:
417
+ if not response.ok:
418
+ raise await handle_error("Adding of entity to group failed.", response, headers=headers)
419
+ await asyncio.sleep(0.25 if self.use_graceful_shutdown else 0.0)
420
+
421
+ async def remove_entity_to_group(self, group_id: str, entity_uri: str, auth_key: Optional[str] = None):
422
+ """Remove an entity from group.
423
+
424
+ Parameters
425
+ ----------
426
+ group_id: str
427
+ Group ID
428
+ entity_uri: str
429
+ URI of entity
430
+ auth_key: Optional[str]
431
+ If the auth key is set the logged-in user (if any) will be ignored and the auth key will be used.
432
+
433
+ Raises
434
+ ------
435
+ WacomServiceException
436
+ If the tenant service returns an error code.
437
+ """
438
+ if auth_key is None:
439
+ auth_key, _ = await self.handle_token()
440
+ uri: str = urllib.parse.quote(entity_uri)
441
+ url: str = f"{self.service_base_url}{GroupManagementService.GROUP_ENDPOINT}/{group_id}/entity/{uri}/remove"
442
+ headers: Dict[str, str] = {
443
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {auth_key}",
444
+ USER_AGENT_HEADER_FLAG: self.user_agent,
445
+ }
446
+ async with AsyncServiceAPIClient.__async_session__() as session:
447
+ async with session.post(url, headers=headers, verify_ssl=self.verify_calls) as response:
448
+ if not response.ok:
449
+ raise await handle_error("Removing of entity from group failed.", response, headers=headers)
450
+ await asyncio.sleep(0.25 if self.use_graceful_shutdown else 0.0)