Changeset ea28ca0 in flexoentity
- Timestamp:
- 12/05/25 12:01:08 (6 weeks ago)
- Branches:
- master
- Children:
- 1f5bf2d
- Parents:
- 753855a
- Files:
-
- 8 edited
-
flexoentity/composite_backend.py (modified) (7 diffs)
-
flexoentity/domain_manager.py (modified) (2 diffs)
-
flexoentity/in_memory_backend.py (modified) (1 diff)
-
flexoentity/persistance_backend.py (modified) (1 diff)
-
flexoentity/sqlite_entity_backend.py (modified) (1 diff)
-
tests/conftest.py (modified) (1 diff)
-
tests/test_composite_backend.py (modified) (3 diffs)
-
tests/test_persistance_integrity.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
flexoentity/composite_backend.py
r753855a rea28ca0 11 11 """ 12 12 13 def __init__(self, entity_class, *backends):14 if not issubclass( entity_class, FlexoEntity):13 def __init__(self, authoritative_backend, sync_backends): 14 if not issubclass(authoritative_backend.entity_class, FlexoEntity): 15 15 raise TypeError("entity_class must be a subclass of FlexOEntity") 16 16 17 self. entity_class = entity_class17 self._primary = authoritative_backend 18 18 19 self.read_backend = InMemoryBackend(self.primary.entity_class) 19 20 # Default: create an in-memory backend as the primary backend 20 if not backends: 21 backends = (InMemoryBackend(entity_class),) 21 22 if sync_backends is None: 23 self.sync_backends = [] 24 else: 25 self.sync_backends = sync_backends 22 26 23 27 # Validate all backends 24 for b in backends:25 if getattr(b, "entity_class", None) !=entity_class:28 for b in self.sync_backends: 29 if b.entity_class != self.primary.entity_class: 26 30 raise TypeError( 27 f"Backend {b} does not match entity_class={ entity_class.__name__}"31 f"Backend {b} does not match entity_class={self.entity_class.__name__}" 28 32 ) 29 30 self.backends = list(backends)31 33 32 34 @property 33 35 def primary(self): 34 36 """The backend used for all read operations.""" 35 return self.backends[0] 37 return self._primary 38 39 @primary.setter 40 def primary(self, new_primary): 41 self._primary = new_primary 36 42 37 43 def add_backend(self, backend, clear=False): … … 40 46 If clear=True, backend is wiped before syncing. 41 47 """ 42 if backend.entity_class != self. entity_class:48 if backend.entity_class != self.primary.entity_class: 43 49 raise TypeError("Backend entity_class mismatch") 44 50 … … 50 56 backend.save(entity) 51 57 52 self. backends.append(backend)58 self.sync_backends.append(backend) 53 59 54 60 def remove_backend(self, backend): … … 58 64 if backend is self.primary: 59 65 raise ValueError("Cannot remove the primary backend") 60 self. backends.remove(backend)66 self.sync_backends.remove(backend) 61 67 # --------------------------------------------------------- 62 68 # Write operations propagate to *all* backends … … 64 70 65 71 def save(self, entity): 66 for b in self.backends: 72 self.primary.save(entity) 73 for b in self.sync_backends: 67 74 b.save(entity) 68 75 69 76 def update(self, entity): 70 for b in self. backends:77 for b in self.sync_backends: 71 78 b.update(entity) 72 79 73 80 def delete(self, flexo_id: str): 74 for b in self. backends:81 for b in self.sync_backends: 75 82 b.delete(flexo_id) 76 83 … … 94 101 Useful if secondary backends were empty initially. 95 102 """ 96 primary = self.backends[0]97 103 for entity in primary.load_all(): 98 for b in self. backends[1:]:104 for b in self.sync_backends: 99 105 b.save(entity) 100 106 101 107 def clear(self): 102 for b in self.backends: 108 self.primary.clear() 109 for b in self.sync_backends: 103 110 if hasattr(b, "clear"): 104 111 b.clear() … … 107 114 108 115 def __repr__(self): 109 names = ", ".join(b.__class__.__name__ for b in self. backends)116 names = ", ".join(b.__class__.__name__ for b in self.sync_backends) 110 117 return f"<CompositeBackend [{names}]>" -
flexoentity/domain_manager.py
r753855a rea28ca0 39 39 40 40 self._rebuild_index() 41 42 def refresh(self): 43 self._rebuild_index 41 44 42 45 # ------------------------------------------------------------ … … 128 131 def __repr__(self): 129 132 return f"<DomainManager domains={self.all_domain_ids()}>" 130 -
flexoentity/in_memory_backend.py
r753855a rea28ca0 17 17 raise TypeError("entity_class must be a subclass of FlexOEntity") 18 18 19 s elf.entity_class = entity_class19 super().__init__(entity_class = entity_class) 20 20 self._storage = storage if storage is not None else {} 21 21 -
flexoentity/persistance_backend.py
r753855a rea28ca0 6 6 """ 7 7 8 def save(self, entity_dict: dict) -> None: 8 def __init__(self, entity_class): 9 self._entity_class = entity_class 10 11 @property 12 def entity_class(self): 13 return self._entity_class 14 15 @entity_class.setter 16 def entity_class(self, a_class): 17 self._entity_class = a_class 18 19 def save(self, flexo_entity) -> None: 9 20 raise NotImplementedError 10 21 11 def update(self, entity_dict: dict) -> None:22 def update(self, flexo_entity) -> None: 12 23 raise NotImplementedError 13 24 -
flexoentity/sqlite_entity_backend.py
r753855a rea28ca0 10 10 11 11 def __init__(self, entity_class, conn, table_name): 12 super().__init__(entity_class) 12 13 self.entity_class = entity_class 13 14 self.conn = conn 14 15 self.table = table_name 15 16 self._init_schema() 17 16 18 17 19 def _init_schema(self): -
tests/conftest.py
r753855a rea28ca0 33 33 34 34 @pytest.fixture 35 def sample_domain_manager(): 36 return DomainManager(CompositeBackend(Domain), EntityRegistry()) 35 def sample_domain_manager(backend): 36 return DomainManager(CompositeBackend(authoritative_backend=backend, 37 sync_backends=None), EntityRegistry()) 37 38 38 39 # ───────────────────────────────────────────────────────────── -
tests/test_composite_backend.py
r753855a rea28ca0 8 8 secondary = InMemoryBackend(Domain) 9 9 10 backend = CompositeBackend( Domain, primary, secondary)10 backend = CompositeBackend(authoritative_backend=primary, sync_backends=[secondary]) 11 11 12 12 backend.save(sample_domain) … … 23 23 secondary = InMemoryBackend(Domain) 24 24 25 backend = CompositeBackend( Domain, primary, secondary)25 backend = CompositeBackend(authoritative_backend=primary, sync_backends=[secondary]) 26 26 27 27 primary.save(sample_domain) … … 37 37 secondary = InMemoryBackend(Domain) 38 38 39 backend = CompositeBackend( Domain, primary, secondary)39 backend = CompositeBackend(authoritative_backend=primary, sync_backends=[secondary]) 40 40 41 41 backend.save(sample_domain) -
tests/test_persistance_integrity.py
r753855a rea28ca0 49 49 file = tmp_path / "question.json" 50 50 json_str = approved_domain.to_json() 51 print("JSON", json_str)52 51 file.write_text(json_str) 53 52
Note:
See TracChangeset
for help on using the changeset viewer.
