Révision 69d17e8b modules/dos_input.py

b/modules/dos_input.py
47 47
import logging
48 48
from configparser import ConfigParser, NoSectionError, NoOptionError, \
49 49
    MissingSectionHeaderError, DuplicateOptionError
50
from utilities import try_command
50 51

  
51 52
logger = logging.getLogger('DockOnSurf')
52 53

  
......
66 67
unexp_error = "An unexpected error occurred"
67 68

  
68 69

  
69
def try_command(command, expct_error_types: list, *args, **kwargs):
70
    """Try to run a command and record exceptions (expected and not) on a log.
71
    
72
    @param command: method or function, the command to be executed.
73
    @param expct_error_types: tuple of tuples, every inner tuple is supposed to
74
    contain an exception type (eg. ValueError, TypeError, etc.) to be caught and
75
    a message to print in the log and on the screen explaining the exception.
76
    Error types that are not allow to be called with a custom message as only
77
    error argument are not supported.
78
    The outer tuple encloses all couples of error types and their relative
79
    messages.
80
    *args and **kwargs: arguments and keyword-arguments of the command to be
81
    executed.
82
    When trying to run 'command' with its args and kwargs, if an exception
83
    present on the 'error_types' occurs, its relative error message is recorded
84
    on the log and a same type exception is raised with the custom message.
85
    """
86

  
87
    err = False
88
    try:
89
        return_val = command(*args, **kwargs)
90
    except Exception as e:
91
        for expct_err in expct_error_types:
92
            if isinstance(e, expct_err[0]):
93
                logger.error(expct_err[1])
94
                err = expct_err[0](expct_err[1])
95
                break
96
        else:
97
            logger.exception(unexp_error)
98
            err = e
99
    else:
100
        err = False
101
        return return_val
102
    finally:
103
        if isinstance(err, BaseException):
104
            raise err
105

  
106

  
107 70
def str2lst(cmplx_str):  # TODO: enable deeper level of nested lists
108 71
    """Converts a string of integers, and groups of them, to a list.
109 72

  

Formats disponibles : Unified diff