Skip to content

Commit

Permalink
v3.2.4 (#1329)
Browse files Browse the repository at this point in the history
- CMX: improved logging
- CMX: improved error handling (show module path and line number)
  • Loading branch information
ctuning-admin authored Oct 17, 2024
2 parents 71e1720 + c7bd26b commit f75478e
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 17 deletions.
4 changes: 4 additions & 0 deletions cm/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## V3.2.4
- CMX: improved logging
- CMX: improved error handling (show module path and line number)

## V3.2.3
- added --new_branch to `cm pull repo` and `cm checkout repo`
- fixed a bug in `cm show repo` (removed dependency on cm4mlops
Expand Down
3 changes: 2 additions & 1 deletion cm/cmind/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#
# Written by Grigori Fursin

__version__ = "3.2.3"
__version__ = "3.2.4"

from cmind.core import access
from cmind.core import x
from cmind.core import error
from cmind.core import errorx
from cmind.core import halt
from cmind.core import CM
2 changes: 1 addition & 1 deletion cm/cmind/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def runx(argv = None):
r = cm.x(argv, out='con')

if r['return']>0 and (cm.output is None or cm.output == 'con'):
cm.error(r)
cm.errorx(r)

sys.exit(r['return'])

Expand Down
1 change: 1 addition & 0 deletions cm/cmind/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self, config_file = None):
"flag_help2": "help",

"error_prefix": "CM error:",
"error_prefix2": "CMX detected an issue",
"info_cli": "cm {action} {automation} {artifact(s)} {flags} @input.yaml @input.json",
"info_clix": "cmx {action} {automation} {artifact(s)} {CMX control flags (-)} {CMX automation flags (--)}",

Expand Down
139 changes: 127 additions & 12 deletions cm/cmind/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,116 @@ def error(self, r):

return r

############################################################
def errorx(self, r):
"""
If r['return']>0: print CM error and raise error if in debugging mode
Args:
r (dict): output from CM function with "return" and "error"
Returns:
(dict): r
"""

import os

if r['return']>0:
if self.debug:
raise Exception(r['error'])

module_path = r.get('module_path', '')
lineno = r.get('lineno', '')

message = ''

if not self.logger == None or (module_path != '' and lineno != ''):
call_stack = self.state.get('call_stack', [])

if not self.logger == None:

self.log(f"x error call stack: {call_stack}", "debug")
self.log(f"x error: {r}", "debug")

sys.stderr.write('='*60 + '\n')

if not self.logger == None:
sys.stderr.write('CMX call stack:\n')

for cs in call_stack:
sys.stderr.write(f' * {cs}\n')

message += '\n'
else:
message += '\n'

message += self.cfg['error_prefix2']

if module_path != '' and lineno !='':
message += f' in {module_path} ({lineno}):\n\n'
else:
message += ': '

message += r['error'] + '\n'

sys.stderr.write(message)

return r

############################################################
def prepare_error(self, returncode, error):
"""
Prepare error dictionary with the module and line number of an error
Args:
returncode (int): CMX returncode
error (str): error message
Returns:
(dict): r
return (int)
error (str)
module_path (str): path to module
lineno (int): line number
"""

from inspect import getframeinfo, stack

caller = getframeinfo(stack()[1][0])

return {'return': returncode,
'error': error,
'module_path': caller.filename,
'lineno': caller.lineno}

############################################################
def embed_error(self, r):
"""
Embed module and line number to an error
Args:
r (dict): CM return dict
Returns:
(dict): r
return (int)
error (str)
module_path (str): path to module
lineno (int): line number
"""

from inspect import getframeinfo, stack

caller = getframeinfo(stack()[1][0])

r['module_path'] = caller.filename
r['lineno'] = caller.lineno

return r

############################################################
def halt(self, r):
"""
Expand Down Expand Up @@ -849,18 +959,6 @@ def x(self, i, out = None):
meta = r)

if r['return'] >0:
if r['return'] > 32:
print ('')
print ('CM Error Call Stack:')

call_stack = self.state['call_stack']

for cs in call_stack:
print (f' {cs}')

self.log(f"x error call stack: {call_stack}", "debug")
self.log(f"x error: {r}", "debug")

if use_raise:
raise Exception(r['error'])

Expand Down Expand Up @@ -1518,6 +1616,23 @@ def error(i):

return cm.error(i)

############################################################
def errorx(i):
"""
Automatically initialize CM and print error if needed
without the need to initialize and customize CM class.
Useful for Python automation scripts.
See CM.error function for more details.
"""

global cm

if cm is None:
cm=CM()

return cm.errorx(i)

############################################################
def halt(i):
"""
Expand Down
7 changes: 4 additions & 3 deletions cm/cmind/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1928,7 +1928,7 @@ def convert_dictionary(d, key, sub = True):
return dd

##############################################################################
def test_input(i, module):
def test_input(i):
"""
Test if input has keys and report them as error
"""
Expand All @@ -1939,9 +1939,10 @@ def test_input(i, module):
unknown_keys = i.keys()
unknown_keys_str = ', '.join(unknown_keys)

x = '' if len(unknown_keys) == 1 else 's'

r = {'return': 1,
'error': 'unknown input key(s) "{}" in module {}'.format(unknown_keys_str, module),
'module': module,
'error': f'unknown input key{x}: {unknown_keys_str}',
'unknown_keys': unknown_keys,
'unknown_keys_str': unknown_keys_str}

Expand Down

0 comments on commit f75478e

Please sign in to comment.