Coverage for fastblocks / adapters / oneiric_helper.py: 78%

9 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-02 20:00 -0800

1"""Oneiric compatibility helpers for FastBlocks adapters. 

2 

3Provides utility functions for registering components with Oneiric's Resolver system. 

4Oneiric requires wrapping objects in Candidate instances with domain, key, and factory. 

5 

6Author: lesleslie <les@wedgwoodwebworks.com> 

7Created: 2025-12-31 

8""" 

9 

10import typing as t 

11 

12from oneiric.core.resolution import Candidate, CandidateSource, Resolver 

13 

14 

15def register_candidate( 

16 resolver: Resolver, 

17 domain: str, 

18 key: str, 

19 factory: t.Callable[..., t.Any], 

20 metadata: dict[str, t.Any] | None = None, 

21) -> bool: 

22 """Register a Oneiric Candidate with the resolver. 

23 

24 Oneiric's Resolver.register() requires Candidate objects, not plain objects. 

25 This helper wraps objects in proper Candidate instances with: 

26 - domain: The domain namespace (e.g., "fastblocks") 

27 - key: The lookup key (e.g., "templates") 

28 - factory: A callable that returns the actual object 

29 - metadata: Optional metadata dictionary 

30 

31 Args: 

32 resolver: Oneiric Resolver instance 

33 domain: Candidate domain (e.g., "fastblocks") 

34 key: Candidate key (e.g., "templates") 

35 factory: Factory function that creates/returns the object 

36 metadata: Optional metadata dictionary 

37 

38 Returns: 

39 True if registration succeeded, False if it failed gracefully 

40 

41 Example: 

42 >>> from oneiric.core.resolution import Resolver 

43 >>> depends = Resolver() 

44 >>> templates_instance = Templates() 

45 >>> register_candidate( 

46 ... depends, 

47 ... domain="fastblocks", 

48 ... key="templates", 

49 ... factory=lambda: templates_instance, 

50 ... metadata={"class": "Templates"}, 

51 ... ) 

52 """ 

53 try: 

54 candidate = Candidate( 

55 domain=domain, 

56 key=key, 

57 factory=factory, 

58 source=CandidateSource.LOCAL_PKG, 

59 metadata=metadata or {}, 

60 ) 

61 resolver.register(candidate) 

62 return True 

63 except Exception: 

64 # Graceful degradation if registration fails 

65 return False