diff --git a/cm/CHANGES.md b/cm/CHANGES.md index b7132c6c1..41c70877d 100644 --- a/cm/CHANGES.md +++ b/cm/CHANGES.md @@ -1,3 +1,13 @@ +## V3.3.4 + - added utils.path2: + add quotes if spaces in path + - added utils.update_dict_with_flat_key: + update dictionary via flat key (x.y.z) + - added utils.get_value_from_dict_with_flat_key + get value from dict via flat key (x.y.z) + - added utils.load_module + universal python module loader + ## V3.3.3 - fixed CM logger issue diff --git a/cm/cmind/__init__.py b/cm/cmind/__init__.py index 441a1eaf1..4bb0bb35e 100644 --- a/cm/cmind/__init__.py +++ b/cm/cmind/__init__.py @@ -2,7 +2,7 @@ # # Written by Grigori Fursin -__version__ = "3.3.3" +__version__ = "3.3.4" from cmind.core import access from cmind.core import x diff --git a/cm/cmind/utils.py b/cm/cmind/utils.py index a631236d0..858e2ed50 100644 --- a/cm/cmind/utils.py +++ b/cm/cmind/utils.py @@ -1947,3 +1947,82 @@ def test_input(i): 'unknown_keys_str': unknown_keys_str} return r + +############################################################################## +def path2(path): + """ + Add quotes if spaces in path + """ + new_path = f'"{path}"' if not path.startswith('"') and ' ' in path else path + + return new_path + +############################################################################## +def update_dict_with_flat_key(key, value, d): + """ + Update dictionary via flat key (x.y.z) + """ + + if '.' in key: + keys = key.split('.') + + new_d = d + + first = True + + for key in keys[:-1]: + if first: + first = False + + if key not in new_d: + new_d[key] = {} + + new_d = new_d[key] + + new_d[keys[-1]] = value + else: + d[key] = value + + return {'return':0} + +############################################################################## +def get_value_from_dict_with_flat_key(key, d): + """ + Get value from dict via flat key (x.y.z) + """ + + if '.' in key: + keys = key.split('.') + new_d = d + for key in keys[:-1]: + if key in new_d: + new_d = new_d[key] + value = new_d.get(keys[-1]) + else: + value = d.get(key) + + return value + +############################################################################## +def load_module(cmind, task_path, sub_module_name): + """ + Universal python module loaders + """ + + import importlib + + sub_module_obj = None + + sub_module_path = os.path.join(task_path, sub_module_name) + if os.path.isfile(sub_module_path): + sub_module_spec = importlib.util.spec_from_file_location(sub_module_name, sub_module_path) + if sub_module_spec == None: + return cmind.prepare_error(1, f"Can\'t load Python module file spec {sub_module_path}") + + try: + sub_module_obj = importlib.util.module_from_spec(sub_module_spec) + sub_module_spec.loader.exec_module(sub_module_obj) + except Exception as e: # pragma: no cover + return cmind.prepare_error(1, f"Can\'t load Python module code {sub_module_path}:\n\n{e}") + + return {'return':0, 'sub_module_obj': sub_module_obj, 'sub_module_path': sub_module_path}