mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-02-24 16:53:12 -05:00
refactor seeders to move file management to class level
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import json
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from logging import Logger
|
from logging import Logger
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -11,6 +12,8 @@ class AbstractSeeder(ABC):
|
|||||||
Abstract class for seeding data.
|
Abstract class for seeding data.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
resources = Path(__file__).parent / "resources"
|
||||||
|
|
||||||
def __init__(self, db: AllRepositories, logger: Logger | None = None):
|
def __init__(self, db: AllRepositories, logger: Logger | None = None):
|
||||||
"""
|
"""
|
||||||
Initialize the abstract seeder.
|
Initialize the abstract seeder.
|
||||||
@@ -19,7 +22,14 @@ class AbstractSeeder(ABC):
|
|||||||
"""
|
"""
|
||||||
self.repos = db
|
self.repos = db
|
||||||
self.logger = logger or get_logger("Data Seeder")
|
self.logger = logger or get_logger("Data Seeder")
|
||||||
self.resources = Path(__file__).parent / "resources"
|
|
||||||
|
@classmethod
|
||||||
|
@abstractmethod
|
||||||
|
def get_file(self, locale: str | None = None) -> Path: ...
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def load_file(self, file: Path) -> dict[str, dict]:
|
||||||
|
return json.loads(file.read_text(encoding="utf-8"))
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def seed(self, locale: str | None = None) -> None: ...
|
def seed(self, locale: str | None = None) -> None: ...
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import json
|
|
||||||
import pathlib
|
import pathlib
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
from functools import cached_property
|
from functools import cached_property
|
||||||
@@ -21,9 +20,10 @@ class MultiPurposeLabelSeeder(AbstractSeeder):
|
|||||||
def service(self):
|
def service(self):
|
||||||
return MultiPurposeLabelService(self.repos)
|
return MultiPurposeLabelService(self.repos)
|
||||||
|
|
||||||
def get_file(self, locale: str | None = None) -> pathlib.Path:
|
@classmethod
|
||||||
|
def get_file(cls, locale: str | None = None) -> pathlib.Path:
|
||||||
# Get the labels from the foods seed file now
|
# Get the labels from the foods seed file now
|
||||||
locale_path = self.resources / "foods" / "locales" / f"{locale}.json"
|
locale_path = cls.resources / "foods" / "locales" / f"{locale}.json"
|
||||||
return locale_path if locale_path.exists() else foods.en_US
|
return locale_path if locale_path.exists() else foods.en_US
|
||||||
|
|
||||||
def get_all_labels(self) -> list[MultiPurposeLabelOut]:
|
def get_all_labels(self) -> list[MultiPurposeLabelOut]:
|
||||||
@@ -34,7 +34,7 @@ class MultiPurposeLabelSeeder(AbstractSeeder):
|
|||||||
|
|
||||||
current_label_names = {label.name for label in self.get_all_labels()}
|
current_label_names = {label.name for label in self.get_all_labels()}
|
||||||
# load from the foods locale file and remove any empty strings
|
# load from the foods locale file and remove any empty strings
|
||||||
seed_label_names = set(filter(None, json.loads(file.read_text(encoding="utf-8")).keys())) # type: set[str]
|
seed_label_names = set(filter(None, self.load_file(file).keys())) # type: set[str]
|
||||||
# only seed new labels
|
# only seed new labels
|
||||||
to_seed_labels = seed_label_names - current_label_names
|
to_seed_labels = seed_label_names - current_label_names
|
||||||
for label in to_seed_labels:
|
for label in to_seed_labels:
|
||||||
@@ -53,8 +53,9 @@ class MultiPurposeLabelSeeder(AbstractSeeder):
|
|||||||
|
|
||||||
|
|
||||||
class IngredientUnitsSeeder(AbstractSeeder):
|
class IngredientUnitsSeeder(AbstractSeeder):
|
||||||
def get_file(self, locale: str | None = None) -> pathlib.Path:
|
@classmethod
|
||||||
locale_path = self.resources / "units" / "locales" / f"{locale}.json"
|
def get_file(cls, locale: str | None = None) -> pathlib.Path:
|
||||||
|
locale_path = cls.resources / "units" / "locales" / f"{locale}.json"
|
||||||
return locale_path if locale_path.exists() else units.en_US
|
return locale_path if locale_path.exists() else units.en_US
|
||||||
|
|
||||||
def get_all_units(self) -> list[IngredientUnit]:
|
def get_all_units(self) -> list[IngredientUnit]:
|
||||||
@@ -64,7 +65,7 @@ class IngredientUnitsSeeder(AbstractSeeder):
|
|||||||
file = self.get_file(locale)
|
file = self.get_file(locale)
|
||||||
|
|
||||||
seen_unit_names = {unit.name for unit in self.get_all_units()}
|
seen_unit_names = {unit.name for unit in self.get_all_units()}
|
||||||
for unit in json.loads(file.read_text(encoding="utf-8")).values():
|
for unit in self.load_file(file).values():
|
||||||
if unit["name"] in seen_unit_names:
|
if unit["name"] in seen_unit_names:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -88,8 +89,9 @@ class IngredientUnitsSeeder(AbstractSeeder):
|
|||||||
|
|
||||||
|
|
||||||
class IngredientFoodsSeeder(AbstractSeeder):
|
class IngredientFoodsSeeder(AbstractSeeder):
|
||||||
def get_file(self, locale: str | None = None) -> pathlib.Path:
|
@classmethod
|
||||||
locale_path = self.resources / "foods" / "locales" / f"{locale}.json"
|
def get_file(cls, locale: str | None = None) -> pathlib.Path:
|
||||||
|
locale_path = cls.resources / "foods" / "locales" / f"{locale}.json"
|
||||||
return locale_path if locale_path.exists() else foods.en_US
|
return locale_path if locale_path.exists() else foods.en_US
|
||||||
|
|
||||||
def get_label(self, value: str) -> MultiPurposeLabelOut | None:
|
def get_label(self, value: str) -> MultiPurposeLabelOut | None:
|
||||||
@@ -103,7 +105,7 @@ class IngredientFoodsSeeder(AbstractSeeder):
|
|||||||
|
|
||||||
# get all current unique foods
|
# get all current unique foods
|
||||||
seen_foods_names = {food.name for food in self.get_all_foods()}
|
seen_foods_names = {food.name for food in self.get_all_foods()}
|
||||||
for label, values in json.loads(file.read_text(encoding="utf-8")).items():
|
for label, values in self.load_file(file).items():
|
||||||
label_out = self.get_label(label)
|
label_out = self.get_label(label)
|
||||||
|
|
||||||
for food_name, attributes in values["foods"].items():
|
for food_name, attributes in values["foods"].items():
|
||||||
|
|||||||
Reference in New Issue
Block a user