Skip to content

Commit 1904ac9

Browse files
committed
added updating the state from context
1 parent 2eabe1f commit 1904ac9

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

ngcsimlib/context.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
from ngcsimlib.utils import make_unique_path, check_attributes, \
2-
check_serializable, load_from_path, get_compartment_by_name, \
3-
get_context, add_context, get_current_path, get_current_context, \
4-
set_new_context, load_module, GuideList, infer_context, Get_Compartment_Batch
1+
from ngcsimlib.utils import (make_unique_path, check_attributes, \
2+
check_serializable, load_from_path,
3+
get_compartment_by_name, \
4+
get_context, add_context, get_current_path,
5+
get_current_context, \
6+
set_new_context, load_module, GuideList,
7+
infer_context,
8+
Get_Compartment_Batch, Set_Compartment_Batch)
59
from ngcsimlib.logger import warn, info, critical
610
from ngcsimlib import preload_modules
711
from ngcsimlib.compilers import dynamic_compile, wrap_command
@@ -74,7 +78,8 @@ def __init__(self, name, should_validate=None):
7478
self.path = get_current_path() + "/" + str(name)
7579
self._last_context = ""
7680

77-
self._json_objects = {"ops": [], "components": {}, "commands": {}, "processes" : []}
81+
self._json_objects = {"ops": [], "components": {}, "commands": {},
82+
"processes": []}
7883

7984
if should_validate is None:
8085
_base_config = get_config("context")
@@ -204,8 +209,6 @@ def register_component(self, component, *args, **kwargs):
204209
def register_process(self, process):
205210
self._json_objects['processes'].append(process)
206211

207-
208-
209212
def add_component(self, component):
210213
"""
211214
Adds a component to the context if it does not exist already in the
@@ -371,7 +374,6 @@ def load_from_dir(self, directory, custom_folder="/custom"):
371374
self.make_commands(directory + "/commands.json")
372375
self.make_process(directory + "/processes.json")
373376

374-
375377
def make_components(self, path_to_components_file, custom_file_dir=None):
376378
"""
377379
Loads a collection of components from a json file. Follow
@@ -649,9 +651,16 @@ def view_guide(self, guide, skip=None):
649651
guides += klass.guides.__dict__[guide.value]
650652
return guides
651653

652-
def get_current_state(self):
654+
def _get_state_keys(self):
653655
all_keys = []
654656
for comp_name in self.components.keys():
655657
all_keys.extend([key for key in Get_Compartment_Batch().keys()
656658
if self.path + "/" + comp_name in key])
657-
return Get_Compartment_Batch(all_keys)
659+
return all_keys
660+
661+
def get_current_state(self):
662+
return Get_Compartment_Batch(self._get_state_keys())
663+
664+
def update_current_state(self, state):
665+
Set_Compartment_Batch({key: value for key, value in state.items() if key in self._get_state_keys()})
666+

0 commit comments

Comments
 (0)