Coverage for opt/mealie/lib/python3.12/site-packages/mealie/services/migrations/chowdown.py: 19%

43 statements  

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

1import tempfile 1a

2import zipfile 1a

3from pathlib import Path 1a

4 

5from ._migration_base import BaseMigrator 1a

6from .utils.migration_alias import MigrationAlias 1a

7from .utils.migration_helpers import MigrationReaders, split_by_comma 1a

8 

9 

10class ChowdownMigrator(BaseMigrator): 1a

11 def __init__(self, **kwargs): 1a

12 super().__init__(**kwargs) 

13 

14 self.name = "chowdown" 

15 

16 self.key_aliases = [ 

17 MigrationAlias(key="name", alias="title", func=None), 

18 MigrationAlias(key="recipeIngredient", alias="ingredients", func=None), 

19 MigrationAlias(key="recipeInstructions", alias="directions", func=None), 

20 MigrationAlias(key="tags", alias="tags", func=split_by_comma), 

21 ] 

22 

23 @classmethod 1a

24 def get_zip_base_path(cls, path: Path) -> Path: 1a

25 potential_path = super().get_zip_base_path(path) 

26 if path == potential_path: 

27 return path 

28 

29 # make sure we didn't accidentally open a recipe dir 

30 if (potential_path / "recipe.json").exists(): 

31 return path 

32 else: 

33 return potential_path 

34 

35 def _migrate(self) -> None: 1a

36 with tempfile.TemporaryDirectory() as tmpdir: 

37 with zipfile.ZipFile(self.archive) as zip_file: 

38 zip_file.extractall(tmpdir) 

39 

40 temp_path = self.get_zip_base_path(Path(tmpdir)) 

41 

42 chow_dir = next(temp_path.iterdir()) 

43 image_dir = temp_path.joinpath(chow_dir, "images") 

44 recipe_dir = temp_path.joinpath(chow_dir, "_recipes") 

45 

46 recipes_as_dicts = [y for x in recipe_dir.glob("*.md") if (y := MigrationReaders.yaml(x)) is not None] 

47 

48 recipes = [self.clean_recipe_dictionary(x) for x in recipes_as_dicts] 

49 

50 results = self.import_recipes_to_database(recipes) 

51 

52 recipe_lookup = {r.slug: r for r in recipes} 

53 

54 for slug, recipe_id, status in results: 

55 if status: 

56 try: 

57 r = recipe_lookup.get(slug) 

58 

59 if not r: 

60 continue 

61 

62 if r.image: 

63 cd_image = image_dir.joinpath(r.image) 

64 except StopIteration: 

65 continue 

66 if cd_image: 

67 self.import_image(slug, cd_image, recipe_id)