Skip to content

Commit bd55f1f

Browse files
committed
Allowed for custom process classes
Updated processes to track needed kwargs, and to allow for custom process classes to be used
1 parent 42f8fa2 commit bd55f1f

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

ngcsimlib/__init__.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ngcsimlib.configManager import init_config, get_config
99
from ngcsimlib.logger import warn
1010
from pkg_resources import get_distribution
11+
from ngcsimlib.compilers.process import Process, transition
1112

1213
__version__ = get_distribution('ngcsimlib').version ## set software version
1314

@@ -36,17 +37,21 @@ def preload_modules(path=None):
3637
modules = json.load(file, object_hook=lambda d: SimpleNamespace(**d))
3738

3839
for module in modules:
39-
mod = import_module(module.absolute_path)
40-
utils.modules._Loaded_Modules[module.absolute_path] = mod
40+
load_module(module)
4141

42-
for attribute in module.attributes:
43-
atr = getattr(mod, attribute.name)
44-
utils.modules._Loaded_Attributes[attribute.name] = atr
42+
def load_module(module):
43+
mod = import_module(module.absolute_path)
44+
utils.modules._Loaded_Modules[module.absolute_path] = mod
4545

46-
utils.modules._Loaded_Attributes[".".join([module.absolute_path, attribute.name])] = atr
47-
if hasattr(attribute, "keywords"):
48-
for keyword in attribute.keywords:
49-
utils.modules._Loaded_Attributes[keyword] = atr
46+
for attribute in module.attributes:
47+
atr = getattr(mod, attribute.name)
48+
utils.modules._Loaded_Attributes[attribute.name] = atr
49+
50+
utils.modules._Loaded_Attributes[
51+
".".join([module.absolute_path, attribute.name])] = atr
52+
if hasattr(attribute, "keywords"):
53+
for keyword in attribute.keywords:
54+
utils.modules._Loaded_Attributes[keyword] = atr
5055

5156
###### Initialize Config
5257
def configure():

ngcsimlib/compilers/process.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ def __init__(self, name):
1010
self._method = None
1111
self._calls = []
1212
self.name = name
13+
self._needed_args = set([])
1314
self._needed_contexts = set([])
1415

1516
cc = get_current_context()
1617
if cc is not None:
1718
cc.register_process(self)
1819

1920
@staticmethod
20-
def make_process(process_spec):
21-
newProcess = Process(process_spec['name'])
21+
def make_process(process_spec, custom_process_klass=None):
22+
if custom_process_klass is None:
23+
custom_process_klass = Process
24+
newProcess = custom_process_klass(process_spec['name'])
2225

2326
for x in process_spec['calls']:
2427
path = x['path']
@@ -37,21 +40,31 @@ def __rshift__(self, other):
3740
def transition(self, transition_call):
3841
self._calls.append({"path": transition_call.__self__.path, "key": transition_call.resolver_key})
3942
self._needed_contexts.add(infer_context(transition_call.__self__.path))
40-
new_step = compile_component(transition_call)
43+
new_step, new_args = compile_component(transition_call)
44+
45+
for arg in new_args:
46+
self._needed_args.add(arg)
4147
self._method = compose(self._method, new_step)
4248
return self
4349

4450
def execute(self, update_state=False, **kwargs):
4551
if self._method is None:
4652
warn("Attempting to execute a process with no transition steps")
4753
return
54+
for arg in self._needed_args:
55+
if arg not in kwargs.keys():
56+
warn("Missing kwarg", arg, "in kwargs for Process", self.name)
57+
return
4858
state = self.pure(self.get_required_state(include_special_compartments=True), **kwargs)
4959
if update_state:
5060
self.updated_modified_state(state)
5161
return state
5262

5363
def as_obj(self):
54-
return {"name": self.name, "calls": self._calls}
64+
return {"name": self.name, "class": self.__class__.__name__, "calls": self._calls}
65+
66+
def get_required_args(self):
67+
return self._needed_args
5568

5669
def get_required_state(self, include_special_compartments=False):
5770
compound_state = {}

ngcsimlib/compilers/process_compiler/component_compiler.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ def compile(transition_method):
4747
composition = compose(composition, op_compile(conn))
4848

4949
arg_methods = []
50-
50+
needed_args = []
5151
for a in args:
52+
needed_args.append(a)
5253
arg_methods.append((a, __make_get_arg(a)))
5354

5455
for p in parameters:
@@ -69,4 +70,4 @@ def compiled(current_state, **kwargs):
6970

7071
composition = compose(composition, compiled)
7172

72-
return composition
73+
return composition, needed_args

ngcsimlib/context.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,10 +478,10 @@ def _make_op(self, op_spec):
478478
def make_process(self, path_to_process_file):
479479
with open(path_to_process_file, 'r') as file:
480480
process_spec = json.load(file)
481-
482-
all_processes = [Process.make_process(p) for p in process_spec]
483-
for p in all_processes:
484-
self.__setattr__(p.name, p)
481+
for p in process_spec:
482+
klass = load_from_path(p["class"])
483+
process = Process.make_process(p, klass)
484+
self.__setattr__(process.name, process)
485485

486486
@staticmethod
487487
def dynamicCommand(fn):
@@ -584,6 +584,13 @@ def make_modules(self):
584584
modules[module]["attributes"]):
585585
modules[module]["attributes"].append({"name": klass})
586586

587+
jProcesses = self._json_objects['processes']
588+
for process in jProcesses:
589+
mod = process.__class__.__module__
590+
klass = process.__class__.__name__
591+
modules[mod] = {"attributes": []}
592+
modules[mod]["attributes"].append({"name": klass})
593+
587594
_modules = []
588595
for key, value in modules.items():
589596
_modules.append(

0 commit comments

Comments
 (0)