Skip to content

Commit 212d825

Browse files
authored
CMX 3.3.4 (#1348)
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
2 parents 03eb0b2 + ab6bd41 commit 212d825

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

cm/CHANGES.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## V3.3.4
2+
- added utils.path2:
3+
add quotes if spaces in path
4+
- added utils.update_dict_with_flat_key:
5+
update dictionary via flat key (x.y.z)
6+
- added utils.get_value_from_dict_with_flat_key
7+
get value from dict via flat key (x.y.z)
8+
- added utils.load_module
9+
universal python module loader
10+
111
## V3.3.3
212
- fixed CM logger issue
313

cm/cmind/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Written by Grigori Fursin
44

5-
__version__ = "3.3.3"
5+
__version__ = "3.3.4"
66

77
from cmind.core import access
88
from cmind.core import x

cm/cmind/utils.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,3 +1947,82 @@ def test_input(i):
19471947
'unknown_keys_str': unknown_keys_str}
19481948

19491949
return r
1950+
1951+
##############################################################################
1952+
def path2(path):
1953+
"""
1954+
Add quotes if spaces in path
1955+
"""
1956+
new_path = f'"{path}"' if not path.startswith('"') and ' ' in path else path
1957+
1958+
return new_path
1959+
1960+
##############################################################################
1961+
def update_dict_with_flat_key(key, value, d):
1962+
"""
1963+
Update dictionary via flat key (x.y.z)
1964+
"""
1965+
1966+
if '.' in key:
1967+
keys = key.split('.')
1968+
1969+
new_d = d
1970+
1971+
first = True
1972+
1973+
for key in keys[:-1]:
1974+
if first:
1975+
first = False
1976+
1977+
if key not in new_d:
1978+
new_d[key] = {}
1979+
1980+
new_d = new_d[key]
1981+
1982+
new_d[keys[-1]] = value
1983+
else:
1984+
d[key] = value
1985+
1986+
return {'return':0}
1987+
1988+
##############################################################################
1989+
def get_value_from_dict_with_flat_key(key, d):
1990+
"""
1991+
Get value from dict via flat key (x.y.z)
1992+
"""
1993+
1994+
if '.' in key:
1995+
keys = key.split('.')
1996+
new_d = d
1997+
for key in keys[:-1]:
1998+
if key in new_d:
1999+
new_d = new_d[key]
2000+
value = new_d.get(keys[-1])
2001+
else:
2002+
value = d.get(key)
2003+
2004+
return value
2005+
2006+
##############################################################################
2007+
def load_module(cmind, task_path, sub_module_name):
2008+
"""
2009+
Universal python module loaders
2010+
"""
2011+
2012+
import importlib
2013+
2014+
sub_module_obj = None
2015+
2016+
sub_module_path = os.path.join(task_path, sub_module_name)
2017+
if os.path.isfile(sub_module_path):
2018+
sub_module_spec = importlib.util.spec_from_file_location(sub_module_name, sub_module_path)
2019+
if sub_module_spec == None:
2020+
return cmind.prepare_error(1, f"Can\'t load Python module file spec {sub_module_path}")
2021+
2022+
try:
2023+
sub_module_obj = importlib.util.module_from_spec(sub_module_spec)
2024+
sub_module_spec.loader.exec_module(sub_module_obj)
2025+
except Exception as e: # pragma: no cover
2026+
return cmind.prepare_error(1, f"Can\'t load Python module code {sub_module_path}:\n\n{e}")
2027+
2028+
return {'return':0, 'sub_module_obj': sub_module_obj, 'sub_module_path': sub_module_path}

0 commit comments

Comments
 (0)