Use config folder abstraction in fermentation controller

This commit is contained in:
Philipp Grathwohl 2022-04-01 15:02:12 +00:00
parent 1b2257f3e2
commit 0c0bda6abe
3 changed files with 21 additions and 18 deletions

View file

@ -3,6 +3,7 @@ import pathlib
import platform import platform
import shutil import shutil
import zipfile import zipfile
import glob
class ConfigFolder: class ConfigFolder:
@ -21,6 +22,13 @@ class ConfigFolder:
def get_recipe_file_by_id(self, recipe_id): def get_recipe_file_by_id(self, recipe_id):
return os.path.join(self._rawPath, 'recipes', "{}.yaml".format(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): def check_for_setup(self):
if self.config_file_exists("config.yaml") is False: if self.config_file_exists("config.yaml") is False:
print("***************************************************") print("***************************************************")

View file

@ -44,7 +44,7 @@ class FermentationController:
destfile = self.cbpi.config_folder.get_file_path("fermenter_data.json") destfile = self.cbpi.config_folder.get_file_path("fermenter_data.json")
json.dump(data,open(destfile,'w'),indent=4, sort_keys=True) 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() self.save()
@ -525,7 +525,7 @@ class FermentationController:
async def savetobook(self, fermenterid): async def savetobook(self, fermenterid):
name = shortuuid.uuid() 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) fermenter=self._find_by_id(fermenterid)
try: try:
brewname = fermenter.brewname brewname = fermenter.brewname

View file

@ -29,27 +29,25 @@ class FermenterRecipeController:
async def create(self, name): async def create(self, name):
id = shortuuid.uuid() 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=[]) data = dict(basic=dict(name=name), steps=[])
with open(path, "w") as file: with open(path, "w") as file:
yaml.dump(data, file) yaml.dump(data, file)
return id return id
async def save(self, name, data): 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) logging.info(data)
with open(path, "w") as file: with open(path, "w") as file:
yaml.dump(data, file, indent=4, sort_keys=True) yaml.dump(data, file, indent=4, sort_keys=True)
async def get_recipes(self): async def get_recipes(self):
path = os.path.join(".", 'config', "fermenterrecipes") fermenter_recipes = self.cbpi.config_folder.get_all_fermenter_recipes()
onlyfiles = [os.path.splitext(f)[0] for f in listdir(path) if isfile(join(path, f)) and f.endswith(".yaml")]
result = [] result = []
for filename in onlyfiles: for filename in fermenter_recipes:
recipe_path = os.path.join(".", 'config', "fermenterrecipes", "%s.yaml" % filename) with open(filename) as file:
with open(recipe_path) as file:
data = yaml.load(file, Loader=yaml.FullLoader) data = yaml.load(file, Loader=yaml.FullLoader)
dataset = data["basic"] dataset = data["basic"]
dataset["file"] = filename dataset["file"] = filename
@ -58,27 +56,24 @@ class FermenterRecipeController:
return result return result
async def get_by_name(self, name): async def get_by_name(self, name):
recipe_path = self.cbpi.config_folder.get_fermenter_recipe_by_id(name)
recipe_path = os.path.join(".", 'config', "fermenterrecipes", "%s.yaml" % name)
with open(recipe_path) as file: with open(recipe_path) as file:
return yaml.load(file, Loader=yaml.FullLoader) return yaml.load(file, Loader=yaml.FullLoader)
async def remove(self, name): 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) os.remove(path)
async def brew(self, recipeid, fermenterid, name): 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) logging.info(recipe_path)
with open(recipe_path) as file: with open(recipe_path) as file:
data = yaml.load(file, Loader=yaml.FullLoader) data = yaml.load(file, Loader=yaml.FullLoader)
await self.cbpi.fermenter.load_recipe(data, fermenterid, name) await self.cbpi.fermenter.load_recipe(data, fermenterid, name)
async def clone(self, id, new_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: with open(recipe_path) as file:
data = yaml.load(file, Loader=yaml.FullLoader) data = yaml.load(file, Loader=yaml.FullLoader)
data["basic"]["name"] = new_name data["basic"]["name"] = new_name