Changeset df459f7 in flexoentity


Ignore:
Timestamp:
11/29/25 14:55:33 (7 weeks ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
master
Children:
918474d
Parents:
4a79b76
Message:

use FlexoCollection for domain manager

Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • flexoentity/__init__.py

    r4a79b76 rdf459f7  
    1515from .flexo_collection import FlexoCollection
    1616from .domain import Domain
     17from .domain_manager import DomainManager, DuplicateDomainError
    1718from .flexo_signature import FlexoSignature, CertificateReference
    1819from .signing_backends import get_signing_backend
     
    2425    "EntityType",
    2526    "Domain",
     27    "DomainManager",
     28    "DuplicateDomainError",
    2629    "EntityState",
    2730    "FlexoCollection",
  • flexoentity/flexo_collection.py

    r4a79b76 rdf459f7  
    55A minimal collection for FlexOEntities.
    66
    7 - No domain rules
    8 - No uniqueness validation (last write wins)
    9 - No state or lifecycle logic
    10 - No typing constraints
    11 
    12 The goal is to provide a Smalltalk-like Collection object with a simple
    13 protocol for storing, retrieving, iterating, and serializing FlexOEntities.
     7- Default key = entity.flexo_id
     8- Optional override key=... for special cases (e.g., domains)
     9- Smalltalk-like protocol preserved
     10- Dict-based: fast lookup
     11- Backwards compatible API
    1412"""
    15 
    1613
    1714from .flexo_entity import FlexoEntity
     
    2118class FlexoCollection:
    2219    """
    23     A minimal collection of FlexOEntities, keyed by FlexOID.
     20    A minimal collection of FlexOEntities, keyed by FlexOID by default.
    2421
    2522    Examples:
    2623        coll = FlexoCollection()
    27         coll.add(entity)
    28         entity = coll.get(some_id)
     24        coll.add(entity)                      # uses entity.flexo_id as key
     25        coll.add(domain, key=domain.domain_id) # override key if needed
     26        ent = coll.get(some_id)
    2927        for e in coll:
    3028            ...
     
    3230
    3331    def __init__(self, items=None):
     32        # internal dict-based storage
    3433        self._items = {}
    3534
     
    3837                self.add(it)
    3938
    40     def add(self, entity: FlexoEntity):
    41         """Add or replace an entity by its FlexOID. Overwrites on duplicate IDs."""
    42         self._items[entity.flexo_id] = entity
     39    # ------------------------------------------------------------------
     40    # Core API (unchanged externally, enhanced internally)
     41    # ------------------------------------------------------------------
     42
     43    def add(self, entity: FlexoEntity, key=None):
     44        """
     45        Add or replace an entity.
     46
     47        - If key is None → use entity.flexo_id
     48        - If key is given → use caller-provided key
     49        """
     50        if key is None:
     51            try:
     52                key = entity.flexo_id
     53            except AttributeError:
     54                raise TypeError(
     55                    "Items must have .flexo_id or explicitly use add(entity, key=...)."
     56                )
     57
     58        self._items[key] = entity  # overwrite is intentional & preserved
    4359
    4460    def remove(self, oid: FlexOID):
    45         """Remove an entity by ID, ignoring if missing."""
     61        """Remove an entity by ID, ignoring if missing (API preserved)."""
    4662        self._items.pop(oid, None)
     63
     64    def clear(self):
     65        self._items = {}
    4766
    4867    def get(self, oid: FlexOID):
     
    6079        return iter(self._items.values())
    6180
     81    # ------------------------------------------------------------------
     82    # Additional public helpers (unchanged)
     83    # ------------------------------------------------------------------
     84
    6285    def entities(self):
    63         """Return all entities."""
     86        """Return all entities as a list."""
    6487        return list(self._items.values())
    6588
    6689    def ids(self):
    67         """Return all IDs."""
     90        """Return all stored keys."""
    6891        return list(self._items.keys())
    6992
    7093    # ------------------------------------------------------------------
    71     # Smalltalk-inspired interface
     94    # Smalltalk-inspired interface (unchanged)
    7295    # ------------------------------------------------------------------
    7396
     
    77100
    78101    def at_put(self, oid, entity):
    79         """Smalltalk-style setter."""
     102        """Smalltalk-style setter (kept)."""
    80103        self._items[oid] = entity
    81104
     
    90113
    91114    # ------------------------------------------------------------------
    92     # Serialization helpers
     115    # Serialization helpers (unchanged)
    93116    # ------------------------------------------------------------------
    94117
    95118    def to_dict_list(self):
    96         """Serialize entities to a list of dicts."""
     119        """Serialize all entities to list of dicts."""
    97120        return [e.to_dict() for e in self._items.values()]
    98121
     
    102125        Deserialize a list of dicts into a FlexoCollection.
    103126
    104         Uses FlexoEntity.from_dict to dispatch to the correct subclass.
     127        Uses FlexoEntity.from_dict() to dispatch to the correct subclass.
    105128        """
    106129        c = cls()
     
    110133        return c
    111134
     135    # ------------------------------------------------------------------
     136
    112137    def __repr__(self) -> str:
    113138        return f"<FlexoCollection size={len(self._items)}>"
  • tests/conftest.py

    r4a79b76 rdf459f7  
    33from pathlib import Path
    44from datetime import datetime
    5 from flexoentity import Domain, FlexoSignature
     5from flexoentity import Domain, FlexoSignature, DomainManager
    66from flexoentity import get_signing_backend, CertificateReference
    77
     
    2828
    2929
     30@pytest.fixture
     31def sample_domain_manager():
     32    return DomainManager()
     33
    3034# ─────────────────────────────────────────────────────────────
    3135# Basic test data directory + PEM test files
    3236# ─────────────────────────────────────────────────────────────
     37
    3338
    3439@pytest.fixture(scope="session")
Note: See TracChangeset for help on using the changeset viewer.