mirror of
https://github.com/PiBrewing/craftbeerpi4.git
synced 2024-11-21 22:48:16 +01:00
Use config folder abstraction in fermentation controller
This commit is contained in:
parent
1b2257f3e2
commit
0c0bda6abe
3 changed files with 21 additions and 18 deletions
|
@ -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("***************************************************")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue