Coverage for opt/mealie/lib/python3.12/site-packages/mealie/pkgs/i18n/provider_factory.py: 78%

34 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-11-25 17:29 +0000

1from dataclasses import dataclass, field 1a

2from functools import cached_property 1a

3from pathlib import Path 1a

4 

5from .json_provider import JsonProvider 1a

6 

7 

8@dataclass(slots=True) 1a

9class InUseProvider: 1a

10 provider: JsonProvider 1a

11 locks: int 1a

12 

13 

14@dataclass 1a

15class ProviderFactory: 1a

16 directory: Path 1a

17 fallback_locale: str = "en-US" 1a

18 filename_format = "{locale}.{format}" 1a

19 

20 _store: dict[str, InUseProvider] = field(default_factory=dict) 1a

21 

22 @property 1a

23 def fallback_file(self) -> Path: 1a

24 return self.directory / self.filename_format.format(locale=self.fallback_locale, format="json") 1bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567

25 

26 @cached_property 1a

27 def supported_locales(self) -> list[str]: 1a

28 return [path.stem for path in self.directory.glob(self.filename_format.format(locale="*", format="json"))] 

29 

30 def _load(self, locale: str) -> JsonProvider: 1a

31 filename = self.filename_format.format(locale=locale, format="json") 18bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567

32 path = self.directory / filename 18bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567

33 

34 return JsonProvider(path) if path.exists() else JsonProvider(self.fallback_file) 18bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567

35 

36 def release(self, locale) -> None: 1a

37 if locale in self._store: 

38 self._store[locale].locks -= 1 

39 if self._store[locale].locks == 0: 

40 del self._store[locale] 

41 

42 def get(self, locale: str) -> JsonProvider: 1a

43 if locale in self._store: 28 b c d 9 e f ! # $ g h i j k l m n o p % q r s ' ( ) t u v * + , - . / : w ; = ? @ x [ ] ^ _ ` { | } ~ abbbcbdbebfby gbhbibjbz A B kblbmbC nbobpbqbD E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 rbsbtb2 3 4 ub5 6 vbwb7

44 self._store[locale].locks += 1 2b c d 9 e f ! # $ g h i j k l m n o p % q r s ' ( ) t u v * + , - . / : w ; = ? @ x [ ] ^ _ ` { | } ~ abbbcbdbebfby gbhbibjbz A B kblbmbC nbobpbqbD E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 rbsbtb2 3 4 ub5 6 vbwb7

45 else: 

46 self._store[locale] = InUseProvider(provider=self._load(locale), locks=1) 18bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567

47 

48 return self._store[locale].provider 28 b c d 9 e f ! # $ g h i j k l m n o p % q r s ' ( ) t u v * + , - . / : w ; = ? @ x [ ] ^ _ ` { | } ~ abbbcbdbebfby gbhbibjbz A B kblbmbC nbobpbqbD E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 rbsbtb2 3 4 ub5 6 vbwb7