Coverage for fastblocks / adapters / sitemap / asgi.py: 63%

41 statements  

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

1"""FastBlocks ASGI Sitemap Adapter (Legacy). 

2 

3This adapter now uses FastBlocks native sitemap implementation 

4instead of the external asgi-sitemaps dependency. 

5 

6For new projects, consider using the native adapter directly. 

7""" 

8 

9import typing as t 

10from contextlib import suppress 

11from uuid import UUID 

12 

13 

14# Custom implementations for ACB compatibility 

15class AdapterStatus: 

16 """Custom AdapterStatus for Oneiric compatibility.""" 

17 

18 STABLE = "STABLE" 

19 BETA = "BETA" 

20 ALPHA = "ALPHA" 

21 EXPERIMENTAL = "EXPERIMENTAL" 

22 

23 

24def debug(msg: str) -> None: 

25 """Custom debug function for Oneiric compatibility.""" 

26 print(f"[DEBUG] {msg}") 

27 

28 

29from oneiric.core.resolution import Resolver 

30 

31# Oneiric resolver for dependency injection 

32depends = Resolver() 

33 

34 

35def import_adapter(adapter_name: str) -> None: 

36 """Custom implementation for Oneiric compatibility.""" 

37 return None 

38 

39 

40from ._base import SitemapBase, SitemapBaseSettings 

41from .core import BaseSitemap as NativeSitemap 

42from .core import SitemapApp 

43 

44 

45class SitemapSettings(SitemapBaseSettings): 

46 pass 

47 

48 

49class AsgiSitemap(NativeSitemap[str], SitemapBase): 

50 sitemap: SitemapApp | None = None 

51 

52 def items(self) -> t.Any: 

53 try: 

54 routes_adapter = depends.get_sync("routes") 

55 if routes_adapter and hasattr(routes_adapter, "routes"): 

56 return [r.path for r in routes_adapter.routes] 

57 return [] 

58 except Exception: 

59 return [] 

60 

61 def location(self, item: str) -> str: 

62 return item 

63 

64 def changefreq(self, item: str) -> str: 

65 return t.cast(str, self.config.change_freq) 

66 

67 async def init(self) -> None: 

68 if not self.config.app.domain: 

69 msg = "`domain` must be set in AppSettings" 

70 raise ValueError(msg) 

71 self.sitemap = SitemapApp( 

72 self, 

73 domain=self.config.app.domain, 

74 cache_ttl=getattr(self.config, "cache_ttl", 3600), 

75 ) 

76 

77 

78MODULE_ID = UUID("01937d86-eff0-7410-5786-a01234567890") 

79MODULE_STATUS = AdapterStatus.STABLE 

80 

81with suppress(Exception): 

82 depends.set(AsgiSitemap)