|
1 | 1 | from ngcsimlib.utils import make_unique_path, check_attributes, \ |
2 | 2 | check_serializable, load_from_path, get_compartment_by_name, \ |
3 | 3 | get_context, add_context, get_current_path, get_current_context, \ |
4 | | - set_new_context, load_module, is_pre_loaded, GuideList |
| 4 | + set_new_context, load_module, GuideList, infer_context |
5 | 5 | from ngcsimlib.logger import warn, info, critical |
6 | 6 | from ngcsimlib import preload_modules |
7 | 7 | from ngcsimlib.compilers import dynamic_compile, wrap_command |
| 8 | +from ngcsimlib.compilers.process import Process |
8 | 9 | from ngcsimlib.component import Component |
9 | 10 | from ngcsimlib.configManager import get_config |
10 | 11 | import json, os, shutil, copy |
@@ -73,7 +74,7 @@ def __init__(self, name, should_validate=None): |
73 | 74 | self.path = get_current_path() + "/" + str(name) |
74 | 75 | self._last_context = "" |
75 | 76 |
|
76 | | - self._json_objects = {"ops": [], "components": {}, "commands": {}} |
| 77 | + self._json_objects = {"ops": [], "components": {}, "commands": {}, "processes" : []} |
77 | 78 |
|
78 | 79 | if should_validate is None: |
79 | 80 | _base_config = get_config("context") |
@@ -200,6 +201,11 @@ def register_component(self, component, *args, **kwargs): |
200 | 201 | "kwargs": _kwargs} |
201 | 202 | self._json_objects['components'][c_path] = obj |
202 | 203 |
|
| 204 | + def register_process(self, process): |
| 205 | + self._json_objects['processes'].append(process) |
| 206 | + |
| 207 | + |
| 208 | + |
203 | 209 | def add_component(self, component): |
204 | 210 | """ |
205 | 211 | Adds a component to the context if it does not exist already in the |
@@ -278,6 +284,12 @@ def save_to_json(self, directory, model_name=None, custom_save=True, |
278 | 284 | with open(path + "/commands.json", 'w') as fp: |
279 | 285 | json.dump(self._json_objects['commands'], fp, indent=4) |
280 | 286 |
|
| 287 | + with open(path + "/processes.json", 'w') as fp: |
| 288 | + objs = [] |
| 289 | + for process in self._json_objects['processes']: |
| 290 | + objs.append(process.as_obj()) |
| 291 | + json.dump(objs, fp, indent=4) |
| 292 | + |
281 | 293 | with open(path + "/components.json", 'w') as fp: |
282 | 294 | hyperparameters = {} |
283 | 295 | _components = copy.deepcopy(self._json_objects['components']) |
@@ -349,14 +361,16 @@ def load_from_dir(self, directory, custom_folder="/custom"): |
349 | 361 | components. (Default: `/custom`) |
350 | 362 | """ |
351 | 363 |
|
352 | | - if os.path.isfile(directory + "/modules.json") and not is_pre_loaded(): |
| 364 | + if os.path.isfile(directory + "/modules.json"): |
353 | 365 | info("No modules file loaded, loading from model directory") |
354 | 366 | preload_modules(path=directory + "/modules.json") |
355 | 367 |
|
356 | 368 | self.make_components(directory + "/components.json", |
357 | 369 | directory + custom_folder) |
358 | 370 | self.make_ops(directory + "/ops.json") |
359 | 371 | self.make_commands(directory + "/commands.json") |
| 372 | + self.make_process(directory + "/processes.json") |
| 373 | + |
360 | 374 |
|
361 | 375 | def make_components(self, path_to_components_file, custom_file_dir=None): |
362 | 376 | """ |
@@ -445,18 +459,28 @@ def _make_op(self, op_spec): |
445 | 459 | _sources.append(self._make_op(s)) |
446 | 460 | else: |
447 | 461 | _sources.append( |
448 | | - get_compartment_by_name(get_current_context(), s)) |
| 462 | + get_compartment_by_name(infer_context(s, trailing_path=2), |
| 463 | + "/".join(s.split("/")[-2:]))) |
449 | 464 |
|
450 | 465 | obj = klass(*_sources) |
451 | 466 |
|
452 | 467 | if op_spec['destination'] is None: |
453 | 468 | return obj |
454 | 469 |
|
455 | 470 | else: |
456 | | - dest = get_compartment_by_name(get_current_context(), |
457 | | - op_spec['destination']) |
| 471 | + d = op_spec['destination'] |
| 472 | + dest = get_compartment_by_name(infer_context(d, trailing_path=2), |
| 473 | + "/".join(d.split("/")[-2:])) |
458 | 474 | dest << obj |
459 | 475 |
|
| 476 | + def make_process(self, path_to_process_file): |
| 477 | + with open(path_to_process_file, 'r') as file: |
| 478 | + process_spec = json.load(file) |
| 479 | + |
| 480 | + all_processes = [Process.make_process(p) for p in process_spec] |
| 481 | + for p in all_processes: |
| 482 | + self.add_command(p.pure, p.name) |
| 483 | + |
460 | 484 | @staticmethod |
461 | 485 | def dynamicCommand(fn): |
462 | 486 | """ |
|
0 commit comments