mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-17 22:11:36 +00:00
IFCPATCH_RECIPE_DIR to allow loading out of tree recipes
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
import sys
|
||||||
import ifcopenshell
|
import ifcopenshell
|
||||||
import logging
|
import logging
|
||||||
import typing
|
import typing
|
||||||
@@ -86,8 +87,13 @@ def execute(args: ArgumentsDict) -> Union[ifcopenshell.file, str]:
|
|||||||
if "log" in args:
|
if "log" in args:
|
||||||
logging.basicConfig(filename=args["log"], filemode="a", level=logging.DEBUG)
|
logging.basicConfig(filename=args["log"], filemode="a", level=logging.DEBUG)
|
||||||
logger = logging.getLogger("IFCPatch")
|
logger = logging.getLogger("IFCPatch")
|
||||||
recipes = getattr(__import__("ifcpatch.recipes.{}".format(args["recipe"])), "recipes")
|
if recipe_dir := os.environ.get("IFCPATCH_RECIPE_DIR"):
|
||||||
recipe = getattr(recipes, args["recipe"])
|
spec = importlib.util.spec_from_file_location(args["recipe"], os.path.join(recipe_dir, args["recipe"] + ".py"))
|
||||||
|
recipe = importlib.util.module_from_spec(spec)
|
||||||
|
sys.modules[args["recipe"]] = recipe
|
||||||
|
spec.loader.exec_module(recipe)
|
||||||
|
else:
|
||||||
|
recipe = importlib.import_module(f"ifcpatch.recipes.{args['recipe']}")
|
||||||
|
|
||||||
# Ensure file or input is provided.
|
# Ensure file or input is provided.
|
||||||
input_argument = get_patch_input_argument_use(args["recipe"])
|
input_argument = get_patch_input_argument_use(args["recipe"])
|
||||||
@@ -97,7 +103,7 @@ def execute(args: ArgumentsDict) -> Union[ifcopenshell.file, str]:
|
|||||||
elif "file" not in args: # SUPPORTED, IGNORED.
|
elif "file" not in args: # SUPPORTED, IGNORED.
|
||||||
raise ValueError(f"Recipe {args['recipe']} is requiring 'file' argument to be provided.")
|
raise ValueError(f"Recipe {args['recipe']} is requiring 'file' argument to be provided.")
|
||||||
|
|
||||||
arguments = args.get("arguments", [])
|
arguments = args.get("arguments", None) or []
|
||||||
if recipe.Patcher.__init__.__doc__ is not None:
|
if recipe.Patcher.__init__.__doc__ is not None:
|
||||||
patcher = recipe.Patcher(args.get("input"), args.get("file"), logger, *arguments)
|
patcher = recipe.Patcher(args.get("input"), args.get("file"), logger, *arguments)
|
||||||
else:
|
else:
|
||||||
@@ -108,9 +114,7 @@ def execute(args: ArgumentsDict) -> Union[ifcopenshell.file, str]:
|
|||||||
|
|
||||||
|
|
||||||
def get_patch_input_argument_use(recipe: str) -> Literal["REQUIRED", "SUPPORTED", "IGNORED"]:
|
def get_patch_input_argument_use(recipe: str) -> Literal["REQUIRED", "SUPPORTED", "IGNORED"]:
|
||||||
import importlib
|
recipe_module = sys.modules[recipe]
|
||||||
|
|
||||||
recipe_module = importlib.import_module(f"ifcpatch.recipes.{recipe}")
|
|
||||||
return getattr(recipe_module.Patcher, "input_argument", "IGNORED")
|
return getattr(recipe_module.Patcher, "input_argument", "IGNORED")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user