python-kanka 2.4.0__py3-none-any.whl → 2.5.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.
kanka/_version.py CHANGED
@@ -1,3 +1,3 @@
1
1
  """Version information for python-kanka."""
2
2
 
3
- __version__ = "2.4.0"
3
+ __version__ = "2.5.0"
kanka/client.py CHANGED
@@ -321,19 +321,29 @@ class KankaClient:
321
321
  response = self._request("GET", f"entities/{entity_id}")
322
322
  return response["data"] # type: ignore[no-any-return]
323
323
 
324
- def entities(self, **filters) -> list[dict[str, Any]]:
324
+ def entities(
325
+ self, page: int = 1, limit: int = 15, **filters
326
+ ) -> list[dict[str, Any]]:
325
327
  """Access the /entities endpoint with filters.
326
328
 
327
329
  This endpoint provides a unified way to query entities across all types
328
330
  with various filtering options.
329
331
 
330
332
  Args:
333
+ page: Page number (default: 1)
334
+ limit: Number of results per page (default: 15)
331
335
  **filters: Filter parameters like types, name, is_private, tags
332
336
 
333
337
  Returns:
334
338
  List of entity data
339
+
340
+ Note:
341
+ Pagination information is available after calling entities():
342
+ - client.last_entities_meta: Contains total count, current page, etc.
343
+ - client.last_entities_links: Contains URLs for first, last, prev, next
344
+ - client.entities_has_next_page: True if more pages are available
335
345
  """
336
- params: dict[str, int | str] = {}
346
+ params: dict[str, int | str] = {"page": page, "limit": limit}
337
347
 
338
348
  # Handle special filters
339
349
  if "types" in filters and isinstance(filters["types"], list):
@@ -355,6 +365,8 @@ class KankaClient:
355
365
  params[key] = filters[key]
356
366
 
357
367
  response = self._request("GET", "entities", params=params)
368
+ self._last_entities_meta = response.get("meta", {})
369
+ self._last_entities_links = response.get("links", {})
358
370
  return response["data"] # type: ignore[no-any-return]
359
371
 
360
372
  def _parse_rate_limit_headers(self, response) -> float | None:
@@ -692,3 +704,27 @@ class KankaClient:
692
704
  Dict[str, Any]: Links for pagination including first, last, prev, next URLs
693
705
  """
694
706
  return getattr(self, "_last_search_links", {})
707
+
708
+ @property
709
+ def last_entities_meta(self) -> dict[str, Any]:
710
+ """Get metadata from the last entities() call.
711
+
712
+ Returns:
713
+ Dict[str, Any]: Pagination metadata including current_page, from, to,
714
+ last_page, per_page, total
715
+ """
716
+ return getattr(self, "_last_entities_meta", {})
717
+
718
+ @property
719
+ def last_entities_links(self) -> dict[str, Any]:
720
+ """Get pagination links from the last entities() call.
721
+
722
+ Returns:
723
+ Dict[str, Any]: Links for pagination including first, last, prev, next URLs
724
+ """
725
+ return getattr(self, "_last_entities_links", {})
726
+
727
+ @property
728
+ def entities_has_next_page(self) -> bool:
729
+ """Check if more pages are available from the last entities() call."""
730
+ return bool(getattr(self, "_last_entities_links", {}).get("next"))
kanka/managers.py CHANGED
@@ -1,10 +1,12 @@
1
1
  """Entity managers for Kanka API."""
2
2
 
3
+ import contextlib
3
4
  import hashlib
4
5
  import re
5
6
  from pathlib import Path
6
7
  from typing import TYPE_CHECKING, Any, List, TypeVar # noqa: UP035
7
8
 
9
+ from .exceptions import NotFoundError
8
10
  from .models.base import Entity, Post
9
11
  from .models.common import EntityAsset, EntityImageInfo
10
12
 
@@ -420,6 +422,22 @@ class EntityManager[T: Entity]:
420
422
  return m.group(1), m.group(2)
421
423
  return None
422
424
 
425
+ @staticmethod
426
+ def _extract_gallery_uuid(url: str | None) -> str | None:
427
+ """Extract gallery image UUID from a CDN URL.
428
+
429
+ The UUID is the filename stem in the URL path, e.g.
430
+ ``https://cdn-ugc.kanka.io/campaigns/123/a1b2-...-c3d4.webp``
431
+ yields ``a1b2-...-c3d4``.
432
+ """
433
+ if not url:
434
+ return None
435
+ m = re.search(
436
+ r"/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.\w+",
437
+ url,
438
+ )
439
+ return m.group(1) if m else None
440
+
423
441
  @staticmethod
424
442
  def _rewrite_image_srcs(html: str, url_map: dict[str, str]) -> str:
425
443
  """Rewrite <img src="key"> tags, replacing keys with CDN URLs."""
@@ -480,18 +498,22 @@ class EntityManager[T: Entity]:
480
498
  for placeholder, file_path in images.items():
481
499
  file_path = Path(file_path)
482
500
  file_hash = self._compute_file_hash(file_path)
483
- used_names.add(placeholder)
501
+ # Truncate to match what _format_managed_asset_name stores
502
+ lookup_name = placeholder[:32]
503
+ used_names.add(lookup_name)
484
504
 
485
- if placeholder in managed:
486
- old_hash, old_asset = managed[placeholder]
505
+ if lookup_name in managed:
506
+ old_hash, old_asset = managed[lookup_name]
487
507
  if old_hash == file_hash:
488
508
  # Same file, reuse URL
489
509
  if old_asset.url:
490
510
  url_map[placeholder] = old_asset.url
491
511
  continue
492
512
  else:
493
- # Changed file, delete old and upload new
494
- self.delete_asset(entity_id, old_asset.id)
513
+ # Changed file, delete old asset and its gallery image
514
+ self.delete_asset(
515
+ entity_id, old_asset.id, delete_gallery_image=True
516
+ )
495
517
 
496
518
  managed_name = self._format_managed_asset_name(placeholder, file_hash)
497
519
  asset = self.create_file_asset(entity_id, file_path, name=managed_name)
@@ -501,7 +523,7 @@ class EntityManager[T: Entity]:
501
523
  # Orphan cleanup: delete managed assets not in current images dict
502
524
  for name_part, (_, old_asset) in managed.items():
503
525
  if name_part not in used_names:
504
- self.delete_asset(entity_id, old_asset.id)
526
+ self.delete_asset(entity_id, old_asset.id, delete_gallery_image=True)
505
527
 
506
528
  if url_map:
507
529
  return self._rewrite_image_srcs(entry, url_map)
@@ -860,19 +882,39 @@ class EntityManager[T: Entity]:
860
882
  response = self.client._request("POST", endpoint, json=data)
861
883
  return EntityAsset(**response["data"])
862
884
 
863
- def delete_asset(self, entity_or_id: T | int, asset_id: int) -> bool:
885
+ def delete_asset(
886
+ self,
887
+ entity_or_id: T | int,
888
+ asset_id: int,
889
+ *,
890
+ delete_gallery_image: bool = False,
891
+ ) -> bool:
864
892
  """Delete an asset from an entity.
865
893
 
866
894
  Args:
867
895
  entity_or_id: The entity or its entity_id
868
896
  asset_id: The asset ID
897
+ delete_gallery_image: If True, also delete the underlying gallery
898
+ image. This prevents orphaned images from accumulating in the
899
+ campaign gallery.
869
900
 
870
901
  Returns:
871
902
  True if successful
872
903
  """
873
904
  entity_id = self._extract_entity_id(entity_or_id)
905
+
906
+ gallery_uuid: str | None = None
907
+ if delete_gallery_image:
908
+ asset = self.get_asset(entity_id, asset_id)
909
+ gallery_uuid = self._extract_gallery_uuid(asset.url)
910
+
874
911
  url = f"entities/{entity_id}/entity_assets/{asset_id}"
875
912
  self.client._request("DELETE", url)
913
+
914
+ if gallery_uuid:
915
+ with contextlib.suppress(NotFoundError):
916
+ self.client.gallery_delete(gallery_uuid)
917
+
876
918
  return True
877
919
 
878
920
  @property
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-kanka
3
- Version: 2.4.0
3
+ Version: 2.5.0
4
4
  Summary: Python client for the Kanka API
5
5
  Project-URL: Homepage, https://github.com/ervwalter/python-kanka
6
6
  Project-URL: Documentation, https://github.com/ervwalter/python-kanka#readme
@@ -1,15 +1,15 @@
1
1
  kanka/__init__.py,sha256=S_Pl6xBtHOpUGr3eOEkq4QEMtByZx4kEWYoQfv3LLrQ,2102
2
- kanka/_version.py,sha256=30224eiQWh6j76ENYkrUAokZWDfKTM2XhHv0cH0kYdo,67
3
- kanka/client.py,sha256=AVLFcrLCwMfof-oTuUuzfNFe10k4Ai2JCkkncfv6YYY,23598
2
+ kanka/_version.py,sha256=CbCNgca30mRPFdjB04QfU2j2_o36aDbjiakPVgp44gE,67
3
+ kanka/client.py,sha256=r4Q0IyF0LwD8VPHB5dV2fhfLTS5tLcwUJqcEWoWoHxU,25095
4
4
  kanka/exceptions.py,sha256=A4P7T7VO_Rp5IYldvFaecE3z4N7WYDgCDojYPu_yDzk,3871
5
- kanka/managers.py,sha256=O3ENwnbWtOqGnuLuDP0xFdlxvABAtDI2SBRUJay5s5s,32801
5
+ kanka/managers.py,sha256=QQN4HX8CDyx8zLvJ2Bvh8f-h7xGy8E7ClY4CnYMlK5E,34225
6
6
  kanka/py.typed,sha256=mZgbv7X8oKfyMzYIbodUlHrC15905jgP-ZbYviEIL18,86
7
7
  kanka/types.py,sha256=onIdSW-3rh6Bqv2VTvx6INFa7tkaQxqg6gJkiAh8YGA,262
8
8
  kanka/models/__init__.py,sha256=IFdzawDK_lhS50puRunnZZalh2S5DOPU96ASHeFITUw,1329
9
9
  kanka/models/base.py,sha256=GOt9I2eNb-R_2oD0fbU0LE3y9gWL0x3_2SQ9I5yuq3Y,4037
10
10
  kanka/models/common.py,sha256=0RF33RupotK0mWjvWc9oIdQZF7ko5FNc_GG5wUIZOOE,6682
11
11
  kanka/models/entities.py,sha256=-2ztBZn8-K8czQyd9vPnEtn4eS666v9firmk7UPDT5A,8885
12
- python_kanka-2.4.0.dist-info/METADATA,sha256=Pv30M3vFabZVtTY-6lLWpLPLxbghL6tLGDnbhbKPvV0,3249
13
- python_kanka-2.4.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
14
- python_kanka-2.4.0.dist-info/licenses/LICENSE,sha256=37op2CRJ_dU2qGU-iJjczcP-ljtJjpKAgBdKFaiF2GY,1067
15
- python_kanka-2.4.0.dist-info/RECORD,,
12
+ python_kanka-2.5.0.dist-info/METADATA,sha256=-RkLCqgQEN4QtIMsePgKYupX2ZWvvZMXpo2T_-fDnpI,3249
13
+ python_kanka-2.5.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
14
+ python_kanka-2.5.0.dist-info/licenses/LICENSE,sha256=37op2CRJ_dU2qGU-iJjczcP-ljtJjpKAgBdKFaiF2GY,1067
15
+ python_kanka-2.5.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.28.0
2
+ Generator: hatchling 1.29.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any