diff --git a/cbpi/configFolder.py b/cbpi/configFolder.py index 5641c92..8485442 100644 --- a/cbpi/configFolder.py +++ b/cbpi/configFolder.py @@ -3,6 +3,7 @@ import pathlib import platform import shutil import zipfile +import glob class ConfigFolder: @@ -21,6 +22,13 @@ class ConfigFolder: def get_recipe_file_by_id(self, recipe_id): return os.path.join(self._rawPath, 'recipes', "{}.yaml".format(recipe_id)) + def get_fermenter_recipe_by_id(self, recipe_id): + return os.path.join(self._rawPath, 'fermenterrecipes', "{}.yaml".format(recipe_id)) + + def get_all_fermenter_recipes(self): + fermenter_recipes_folder = os.path.join(self._rawPath, 'fermenterrecipes', '*.yaml') + return glob.glob(fermenter_recipes_folder) + def check_for_setup(self): if self.config_file_exists("config.yaml") is False: print("***************************************************") diff --git a/cbpi/controller/fermentation_controller.py b/cbpi/controller/fermentation_controller.py index 6e29a58..c869795 100644 --- a/cbpi/controller/fermentation_controller.py +++ b/cbpi/controller/fermentation_controller.py @@ -44,9 +44,9 @@ class FermentationController: destfile = self.cbpi.config_folder.get_file_path("fermenter_data.json") json.dump(data,open(destfile,'w'),indent=4, sort_keys=True) - pathlib.Path(os.path.join(".", 'config/fermenterrecipes')).mkdir(parents=True, exist_ok=True) + pathlib.Path(self.cbpi.config_folder.get_file_path("fermenterrecipes")).mkdir(parents=True, exist_ok=True) - async def shutdown(self, app=None, fermenterid=None): + async def shutdown(self, app=None, fermenterid=None): self.save() if (fermenterid == None): for fermenter in self.data: @@ -525,7 +525,7 @@ class FermentationController: async def savetobook(self, fermenterid): name = shortuuid.uuid() - path = os.path.join(".", 'config', "fermenterrecipes", "{}.yaml".format(name)) + path = self.cbpi.config_folder.get_fermenter_recipe_by_id(name) fermenter=self._find_by_id(fermenterid) try: brewname = fermenter.brewname diff --git a/cbpi/controller/fermenter_recipe_controller.py b/cbpi/controller/fermenter_recipe_controller.py index d0de8b5..fe1f7fb 100644 --- a/cbpi/controller/fermenter_recipe_controller.py +++ b/cbpi/controller/fermenter_recipe_controller.py @@ -29,56 +29,51 @@ class FermenterRecipeController: async def create(self, name): id = shortuuid.uuid() - path = os.path.join(".", 'config', "fermenterrecipes", "{}.yaml".format(id)) + path = self.cbpi.config_folder.get_fermenter_recipe_by_id(id) data = dict(basic=dict(name=name), steps=[]) with open(path, "w") as file: yaml.dump(data, file) return id async def save(self, name, data): - path = os.path.join(".", 'config', "fermenterrecipes", "{}.yaml".format(name)) + path = self.cbpi.config_folder.get_fermenter_recipe_by_id(name) logging.info(data) with open(path, "w") as file: yaml.dump(data, file, indent=4, sort_keys=True) async def get_recipes(self): - path = os.path.join(".", 'config', "fermenterrecipes") - onlyfiles = [os.path.splitext(f)[0] for f in listdir(path) if isfile(join(path, f)) and f.endswith(".yaml")] + fermenter_recipes = self.cbpi.config_folder.get_all_fermenter_recipes() result = [] - for filename in onlyfiles: - recipe_path = os.path.join(".", 'config', "fermenterrecipes", "%s.yaml" % filename) - with open(recipe_path) as file: + for filename in fermenter_recipes: + with open(filename) as file: data = yaml.load(file, Loader=yaml.FullLoader) dataset = data["basic"] dataset["file"] = filename result.append(dataset) logging.info(result) return result - + async def get_by_name(self, name): - - recipe_path = os.path.join(".", 'config', "fermenterrecipes", "%s.yaml" % name) + recipe_path = self.cbpi.config_folder.get_fermenter_recipe_by_id(name) with open(recipe_path) as file: return yaml.load(file, Loader=yaml.FullLoader) - async def remove(self, name): - path = os.path.join(".", 'config', "fermenterrecipes", "{}.yaml".format(name)) + path = self.cbpi.config_folder.get_fermenter_recipe_by_id(name) os.remove(path) - async def brew(self, recipeid, fermenterid, name): + recipe_path = self.cbpi.config_folder.get_fermenter_recipe_by_id(recipeid) - recipe_path = os.path.join(".", 'config', "fermenterrecipes", "%s.yaml" % recipeid) logging.info(recipe_path) with open(recipe_path) as file: data = yaml.load(file, Loader=yaml.FullLoader) await self.cbpi.fermenter.load_recipe(data, fermenterid, name) async def clone(self, id, new_name): - recipe_path = os.path.join(".", 'config', "fermenterrecipes", "%s.yaml" % id) + recipe_path = self.cbpi.config_folder.get_fermenter_recipe_by_id(id) with open(recipe_path) as file: data = yaml.load(file, Loader=yaml.FullLoader) data["basic"]["name"] = new_name