Coverage for fastblocks / main.py: 32%

79 statements  

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

1import typing as t 

2from contextlib import suppress 

3from pathlib import Path 

4 

5# Oneiric imports 

6from oneiric.adapters.bootstrap import ( 

7 register_adapter_metadata, 

8 register_builtin_adapters, 

9) 

10from oneiric.core.resolution import Resolver, register_pkg 

11 

12# Create resolver instance for dependency resolution 

13_resolver = Resolver() 

14 

15 

16async def _get_dependency(name: str) -> t.Any: 

17 """Get dependency using Oneiric resolver.""" 

18 return await _resolver.resolve(name) 

19 

20 

21# Use Path(__file__).parent.parent as root_path 

22root_path = Path(__file__).parent.parent 

23 

24_app_instance = None 

25_logger_instance = None 

26 

27 

28def _check_dev_mode() -> None: 

29 """Check if running in development mode and raise error if so.""" 

30 current_dir = Path.cwd() 

31 fastblocks_pkg_dir = Path(__file__).parent 

32 fastblocks_root_dir = fastblocks_pkg_dir.parent 

33 is_dev_mode = current_dir == fastblocks_pkg_dir or ( 

34 current_dir == fastblocks_root_dir and (current_dir / "pyproject.toml").exists() 

35 ) 

36 if is_dev_mode: 

37 msg = ( 

38 "FastBlocks cannot be run from its own package directory. " 

39 "Please run from your application directory." 

40 ) 

41 raise RuntimeError( 

42 msg, 

43 ) 

44 

45 

46async def _handle_registration() -> None: 

47 """Handle adapter registration using Oneiric.""" 

48 # Oneiric: Register builtin adapters 

49 try: 

50 await register_builtin_adapters() 

51 except Exception as e: 

52 msg = f"Failed to register builtin adapters: {e}" 

53 raise RuntimeError(msg) from e 

54 

55 

56async def _handle_adapter_registration() -> None: 

57 """Handle adapter registration using Oneiric.""" 

58 # Oneiric: Register adapter metadata 

59 with suppress(Exception): 

60 await register_adapter_metadata(root_path) 

61 

62 

63async def _get_app_instance() -> t.Any: 

64 """Get the app instance.""" 

65 try: 

66 return await _get_dependency("app") 

67 except Exception as e: 

68 msg = f"Failed to get app adapter: {e}. Make sure adapters are properly registered and configured." 

69 raise RuntimeError( 

70 msg, 

71 ) from e 

72 

73 

74async def _get_logger_instance() -> t.Any: 

75 """Get the logger instance.""" 

76 try: 

77 return await _get_dependency("logger") 

78 except Exception as e: 

79 import logging 

80 

81 logger_instance = logging.getLogger("fastblocks") 

82 logger_instance.warning( 

83 f"Failed to get logger adapter, using fallback: {e}", 

84 ) 

85 return logger_instance 

86 

87 

88async def get_app() -> t.Any: 

89 global _app_instance, _logger_instance 

90 if _app_instance is None: 

91 _check_dev_mode() 

92 

93 try: 

94 register_pkg() 

95 except Exception as e: 

96 msg = f"Failed to register FastBlocks adapters: {e}" 

97 raise RuntimeError(msg) from e 

98 

99 await _handle_registration() 

100 await _handle_adapter_registration() 

101 

102 _app_instance = await _get_app_instance() 

103 _logger_instance = await _get_logger_instance() 

104 

105 return _app_instance 

106 

107 

108def get_logger() -> t.Any: 

109 return _logger_instance 

110 

111 

112class LazyApp: 

113 def __getattr__(self, name: str) -> t.Any: 

114 if _app_instance is None: 

115 msg = "FastBlocks app has not finished initializing" 

116 raise AttributeError(msg) 

117 return getattr(_app_instance, name) 

118 

119 async def __call__(self, scope: t.Any, receive: t.Any, send: t.Any) -> None: 

120 app = await get_app() 

121 await app(scope, receive, send) 

122 

123 

124class LazyLogger: 

125 def __getattr__(self, name: str) -> t.Any: 

126 if _logger_instance is None: 

127 msg = "FastBlocks logger has not finished initializing" 

128 raise AttributeError(msg) 

129 return getattr(_logger_instance, name) 

130 

131 def __call__(self, *args: t.Any, **kwargs: t.Any) -> t.Any: 

132 logger = _logger_instance 

133 if callable(logger): 

134 return logger(*args, **kwargs) 

135 return logger 

136 

137 

138app = LazyApp() 

139logger = LazyLogger()