def query_list(pkg): module_is_script = pkg.endswith(".py") scripts = [] # Import the script, or the package and its sub-modules if module_is_script: pkg = op.realpath(pkg) module = load_script(pkg) scripts.append((module.__name__, module)) else: module = importlib.import_module(pkg) for submod in pkgutil.iter_modules(module.__path__): _, module_name, ispkg = submod if ispkg: continue script = importlib.import_module("." + module_name, pkg) scripts.append((module_name, script)) # Find all the scenes scenes = [] for module_name, script in scripts: all_funcs = inspect.getmembers(script, inspect.isfunction) sub_scenes = [] for func in all_funcs: scene_name, func_wrapper = func if not hasattr(func_wrapper, "iam_a_ngl_scene_func"): continue sub_scenes.append( (scene_name, func_wrapper.__doc__, func_wrapper.widgets_specs)) if sub_scenes: scenes.append((module_name, sub_scenes)) return dict(scenes=scenes)
def run(): refgen_opt = os.environ.get("REFGEN", "no") allowed_gen_opt = _refgen_map.keys() if refgen_opt not in allowed_gen_opt: allowed_str = ", ".join(allowed_gen_opt) sys.stderr.write( f"REFGEN environment variable must be any of {allowed_str}\n") sys.exit(1) tests_opts = os.environ.get("TESTS_OPTIONS") allowed_tests_opts = ("dump", ) if tests_opts is not None and tests_opts not in allowed_tests_opts: allowed_str = ", ".join(allowed_tests_opts) sys.stderr.write( f"TESTS_OPTIONS environment variable must be any of {allowed_str}\n" ) sys.exit(1) dump = tests_opts == "dump" if len(sys.argv) not in (3, 4): sys.stderr.write( "Usage: [TESTS_OPTIONS={} REFGEN={}] {} <script_path> <func_name> [<ref_filepath>]" .format("|".join(allowed_tests_opts), "|".join(allowed_gen_opt), op.basename(sys.argv[0]))) sys.exit(1) if len(sys.argv) == 3: script_path, func_name, ref_filepath = sys.argv[1], sys.argv[2], None else: script_path, func_name, ref_filepath = sys.argv[1:4] module = load_script(script_path) func = getattr(module, func_name) # Ensure PySide/Qt is not imported assert not any(k.startswith(("PySide", "Qt")) for k in globals().keys()) if ref_filepath is None: sys.exit(func()) tester = func.tester test_func = _refgen_map[refgen_opt] err = test_func(func_name, tester, ref_filepath, dump) if err: sys.stderr.write(f"{func_name} failed\n") sys.stderr.write("\n".join(err) + "\n") sys.exit(1) print(f"{func_name} passed") sys.exit(0)
def query_scene(pkg, **idict): module_is_script = pkg.endswith(".py") # Get module.func module_name, scene_name = idict["scene"] if module_is_script: module = load_script(pkg) else: import_name = f"{pkg}.{module_name}" module = importlib.import_module(import_name) func = getattr(module, scene_name) # Call user constructing function odict = func(idict, **idict.get("extra_args", {})) scene = odict.pop("scene") scene.set_label(scene_name) # Prepare output data odict["scene"] = scene.dot() if idict.get("fmt") == "dot" else scene.serialize() return odict
def __init__(self, hooks_script): self._module = load_script(hooks_script) if not all(hasattr(self._module, hook) for hook in self._HOOKS): raise NotImplementedError( f"{hooks_script}: the following functions must be implemented: {self._HOOKS}" )