add --outdir argument, remove directory params from builder
This commit is contained in:
parent
7fb3a1c0e9
commit
98c77cb102
2 changed files with 37 additions and 33 deletions
|
@ -53,37 +53,37 @@ class Builder:
|
|||
return self.load_file(f"{os.path.dirname(__file__)}/{file}")
|
||||
|
||||
def rendertemplate(
|
||||
self, templatepath: str, format: str, file: str, dir: str, args: Any
|
||||
self, templatepath: str, format: str, file: str, outdir: str, args: Any
|
||||
) -> None:
|
||||
template = self.jinjaenv.get_template(templatepath)
|
||||
print(f"rendering {file}")
|
||||
outstr = template.render(args)
|
||||
|
||||
os.makedirs(f"{dir}/out/{format}", exist_ok=True)
|
||||
os.makedirs(f"{outdir}/{format}", exist_ok=True)
|
||||
|
||||
with open(f"{dir}/out/{format}/{file}", "w", encoding="utf-8") as f:
|
||||
with open(f"{outdir}/{format}/{file}", "w", encoding="utf-8") as f:
|
||||
f.write(outstr)
|
||||
self.outfiles.add(file)
|
||||
|
||||
def load(self, dir: str) -> int:
|
||||
if os.path.isfile(dir + "/settings.yaml"):
|
||||
settingsdct = self.load_file(dir + "/settings.yaml")
|
||||
def load(self, outdir: str) -> int:
|
||||
if os.path.isfile("settings.yaml"):
|
||||
settingsdct = self.load_file("settings.yaml")
|
||||
settingsschema = self.load_pkgfile("schemas/settings.json")
|
||||
self.ctx.load_settings(settingsdct, settingsschema)
|
||||
retcode = self.ctx.issues.check()
|
||||
if retcode != 0:
|
||||
return 1
|
||||
|
||||
if os.path.isfile(dir + "/units.yaml"):
|
||||
unitsdct = self.load_file(dir + "/units.yaml")
|
||||
if os.path.isfile("units.yaml"):
|
||||
unitsdct = self.load_file("units.yaml")
|
||||
unitsschema = self.load_pkgfile("schemas/units.json")
|
||||
self.ctx.load_units(unitsdct, unitsschema)
|
||||
retcode = self.ctx.issues.check()
|
||||
if retcode != 0:
|
||||
return 1
|
||||
|
||||
if os.path.isfile(dir + "/ingredients.yaml"):
|
||||
ingredientsdct = self.load_file(dir + "/ingredients.yaml")
|
||||
if os.path.isfile("ingredients.yaml"):
|
||||
ingredientsdct = self.load_file("ingredients.yaml")
|
||||
ingredientsschema = self.load_pkgfile("schemas/ingredients.json")
|
||||
self.ctx.load_ingredients(ingredientsdct, ingredientsschema)
|
||||
retcode = self.ctx.issues.check()
|
||||
|
@ -92,9 +92,9 @@ class Builder:
|
|||
|
||||
return 0
|
||||
|
||||
def run(self, dir: str) -> int:
|
||||
def run(self, outdir: str) -> int:
|
||||
files = []
|
||||
for _, _, filesx in os.walk(dir + "/recipes"):
|
||||
for _, _, filesx in os.walk("recipes"):
|
||||
files = filesx
|
||||
files.sort()
|
||||
|
||||
|
@ -104,7 +104,7 @@ class Builder:
|
|||
if not file.endswith(".yaml"):
|
||||
print(f"unknown extension of {file}")
|
||||
continue
|
||||
recipedct = self.load_file(dir + "/recipes/" + file)
|
||||
recipedct = self.load_file("recipes/" + file)
|
||||
jsonschema.validate(instance=recipedct, schema=recipeschema)
|
||||
recipe = parsing.Recipe.from_dict(self.ctx, recipedct)
|
||||
recipe.srcpath = file
|
||||
|
@ -121,7 +121,7 @@ class Builder:
|
|||
templatepath="index.html",
|
||||
format="html",
|
||||
file="index.html",
|
||||
dir=dir,
|
||||
outdir=outdir,
|
||||
args={"recipes": recipes},
|
||||
)
|
||||
for recipe in recipes:
|
||||
|
@ -129,18 +129,18 @@ class Builder:
|
|||
templatepath="recipe.html",
|
||||
format="html",
|
||||
file=recipe.outpath,
|
||||
dir=dir,
|
||||
outdir=outdir,
|
||||
args={"recipe": recipe},
|
||||
)
|
||||
return 0
|
||||
|
||||
def generate_units(self, dir: str) -> None:
|
||||
def generate_units(self) -> None:
|
||||
def collect_unitnames(rec: parsing.Recipe) -> List[str]:
|
||||
results: List[str] = []
|
||||
for ing in rec.ingredients:
|
||||
results.append(ing.unit.name)
|
||||
return results
|
||||
unitnamelists = self.foreach_recipe(dir, collect_unitnames)
|
||||
unitnamelists = self.foreach_recipe(collect_unitnames)
|
||||
unitnamesset: Set[str] = set()
|
||||
for unitnamelst in unitnamelists:
|
||||
for unitname in unitnamelst:
|
||||
|
@ -152,18 +152,18 @@ class Builder:
|
|||
for name in unitnames:
|
||||
units.append({"name": name})
|
||||
|
||||
file = f"{dir}/units.yaml"
|
||||
file = "units.yaml"
|
||||
with open(file, "w") as f:
|
||||
f.write(yaml.dump(units))
|
||||
print("found units written to", file)
|
||||
|
||||
def generate_ingredients(self, dir: str) -> None:
|
||||
def generate_ingredients(self) -> None:
|
||||
def collect_ingnames(rec: parsing.Recipe) -> List[str]:
|
||||
results: List[str] = []
|
||||
for ing in rec.ingredients:
|
||||
results.append(ing.name)
|
||||
return results
|
||||
ingredientnamelists = self.foreach_recipe(dir, collect_ingnames)
|
||||
ingredientnamelists = self.foreach_recipe(collect_ingnames)
|
||||
ingredientnamesset: Set[str] = set()
|
||||
for ingredientnamelst in ingredientnamelists:
|
||||
for ingredientname in ingredientnamelst:
|
||||
|
@ -175,14 +175,14 @@ class Builder:
|
|||
for name in ingredientnames:
|
||||
ingredients.append({"name": name})
|
||||
|
||||
file = f"{dir}/ingredients.yaml"
|
||||
file = "ingredients.yaml"
|
||||
with open(file, "w") as f:
|
||||
f.write(yaml.dump(ingredients))
|
||||
print("found ingredients written to", file)
|
||||
|
||||
def foreach_recipe(self, dir: str, func: Callable[[parsing.Recipe], T]) -> List[T]:
|
||||
def foreach_recipe(self, func: Callable[[parsing.Recipe], T]) -> List[T]:
|
||||
files = []
|
||||
for _, _, filesx in os.walk(dir + "/recipes"):
|
||||
for _, _, filesx in os.walk("recipes"):
|
||||
files = filesx
|
||||
files.sort()
|
||||
|
||||
|
@ -200,30 +200,31 @@ class Builder:
|
|||
if not file.endswith(".yaml"):
|
||||
print(f"unknown extension of {file}")
|
||||
continue
|
||||
recipedct = self.load_file(dir + "/recipes/" + file)
|
||||
recipedct = self.load_file("recipes/" + file)
|
||||
recipe = parsing.Recipe.from_dict(self.ctx, recipedct)
|
||||
if self.ctx.issues.check() != 0:
|
||||
continue
|
||||
results += foreach_subrecipe(recipe)
|
||||
return results
|
||||
|
||||
def finish(self, dir: str) -> int:
|
||||
def finish(self, outdir: str) -> int:
|
||||
files = set()
|
||||
for _, _, filesx in os.walk(f"{dir}/out/html"):
|
||||
for _, _, filesx in os.walk(f"{outdir}/html"):
|
||||
files = set(filesx)
|
||||
|
||||
# files we did not generate, probably left by a previous run, but not valid anymore
|
||||
extra_files = files - self.outfiles
|
||||
for file in extra_files:
|
||||
print(f"removing obsolete {file}")
|
||||
os.remove(f"{dir}/out/html/{file}")
|
||||
os.remove(f"{outdir}/html/{file}")
|
||||
return 0
|
||||
|
||||
def build(self, path: str) -> int:
|
||||
def build(self, directory: str, outdir: str) -> int:
|
||||
fcs = [self.load, self.run, self.finish]
|
||||
os.chdir(directory)
|
||||
for func in fcs:
|
||||
try:
|
||||
ret = func(path)
|
||||
ret = func(outdir)
|
||||
if ret != 0:
|
||||
return ret
|
||||
except jsonschema.exceptions.ValidationError as e:
|
||||
|
|
|
@ -13,11 +13,13 @@ def main() -> None:
|
|||
|
||||
parser_build = subparsers.add_parser("build")
|
||||
parser_build.add_argument("directory", type=str)
|
||||
parser_build.add_argument("--outdir", type=str, default="out")
|
||||
|
||||
parser_serve = subparsers.add_parser("serve")
|
||||
parser_serve.add_argument("directory", type=str)
|
||||
parser_serve.add_argument("--port", type=int, default=8000)
|
||||
parser_serve.add_argument("--address", type=str, default="127.0.0.1")
|
||||
parser_serve.add_argument("--outdir", type=str, default="out/html")
|
||||
|
||||
parser_generate_units = subparsers.add_parser("generate-units")
|
||||
parser_generate_units.add_argument("directory", type=str)
|
||||
|
@ -32,9 +34,10 @@ def main() -> None:
|
|||
ret = 0
|
||||
builder = comfyrecipes.builder.Builder()
|
||||
if args.subcommand == "build":
|
||||
ret = builder.build(args.directory)
|
||||
ret = builder.build(args.directory, args.outdir)
|
||||
elif args.subcommand == "serve":
|
||||
os.chdir(f"{args.directory}/out/html")
|
||||
os.chdir(args.directory)
|
||||
os.chdir(args.outdir)
|
||||
httpd = socketserver.TCPServer(
|
||||
(args.address, args.port), http.server.SimpleHTTPRequestHandler
|
||||
)
|
||||
|
@ -48,13 +51,13 @@ def main() -> None:
|
|||
print("units.yaml already exists, pass --force if you want to overwrite it", file=sys.stderr)
|
||||
ret = 1
|
||||
else:
|
||||
builder.generate_units(args.directory)
|
||||
builder.generate_units()
|
||||
elif args.subcommand == "generate-ingredients":
|
||||
if not args.force and os.path.isfile(args.directory + "/ingredients.yaml"):
|
||||
print("ingredients.yaml already exists, pass --force if you want to overwrite it", file=sys.stderr)
|
||||
ret = 1
|
||||
else:
|
||||
builder.generate_ingredients(args.directory)
|
||||
builder.generate_ingredients()
|
||||
else:
|
||||
# unhandled, but valid subcommand
|
||||
assert False
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue