diff --git a/projects/vdk-core/src/vdk/internal/builtin_plugins/run/noop_datajob.py b/projects/vdk-core/src/vdk/internal/builtin_plugins/run/datajob_initializer.py similarity index 81% rename from projects/vdk-core/src/vdk/internal/builtin_plugins/run/noop_datajob.py rename to projects/vdk-core/src/vdk/internal/builtin_plugins/run/datajob_initializer.py index c3d8e23097..5eb986ad13 100644 --- a/projects/vdk-core/src/vdk/internal/builtin_plugins/run/noop_datajob.py +++ b/projects/vdk-core/src/vdk/internal/builtin_plugins/run/datajob_initializer.py @@ -41,27 +41,22 @@ def initialize_job(self, context: JobContext): context.step_builder.add_step(step) -class NoOpStepDataJob: +class InitialisedJobInput: """ - DataJob that exposes methods to enable running each phase of the DataJob lifecycle - independently and get access to an initialised IJobInput object after the run_and_return_job_input() step - - Sample usage: - datajob_directory = Path(abspath(file_relative_path(__file__, '../'))) - datajob = NoOpStepDataJob(datajob_directory) - try: - datajob.initialize_job() - vdk_job_input = datajob.run_and_return_job_input() - - ... use vdk_job_input object to interact with SuperCollider - finally: - datajob.finalize_job() + A contextmanager that executes all the necessary "setup" logic needed to return an initialised IJobInput + + Sample usage:: + + datajob_directory = Path(abspath(file_relative_path(__file__, '../'))) + with InitialisedJobInput(datajob_directory) as vdk_job_input: + #... use vdk_job_input object to interact with SuperCollider """ def __init__( self, data_job_directory: pathlib.Path | None, name: str | None = None, + job_args: dict | None = None, ): if data_job_directory is None and name is None: raise ValueError( @@ -70,6 +65,9 @@ def __init__( ) self._name = data_job_directory.name if name is None else name self._data_job_directory = data_job_directory + if job_args is None: + job_args = {} + self._job_args = job_args plugin_registry = PluginRegistry() plugin_registry.add_hook_specs(InternalHookSpecs) @@ -94,10 +92,7 @@ def __init__( self._job_context = None - def initialize_job(self, args: dict = None): - if args is None: - args = {} - + def __enter__(self) -> IJobInput: self._core_context.plugin_registry.load_plugin_with_hooks_impl( NoOpStepDataJobHookImplPlugin(), NoOpStepDataJobHookImplPlugin.__name__ ) @@ -108,16 +103,15 @@ def initialize_job(self, args: dict = None): name=self._name, job_directory=self._data_job_directory, core_context=self._core_context, - job_args=JobArguments(args), + job_args=JobArguments(self._job_args), templates=TemplatesImpl( job_name=self._name, core_context=self._core_context ), ) + # We need to call both the initialize_job and run_job hooks to get a full initialized JobInput self._plugin_hook.initialize_job(context=self._job_context) - - def run_and_return_job_input(self) -> IJobInput: self._plugin_hook.run_job(context=self._job_context) return self._job_context.job_input - def finalize_job(self): + def __exit__(self, exc_type, exc_value, exc_traceback): self._plugin_hook.finalize_job(context=self._job_context)