Пример #1
0
def log(msg, options=None):
    """! @brief Write message into log file if any, or to standard output if verbose mode is on.
    @param msg String to log.
    @param options User options.
    """
    try:
        import sys
        ## If provided, set function variables according to user options
        if options is not None:
            # Keep log filename and verbose mode in function variables
            setattr(log, "log_filename", options.log_filename)
            setattr(log, "verbose", options.verbose)
            # Initialize log file
            if log.log_filename is not None:
                log_file = open_file(log.log_filename, 'w+')
                log_file.close()
        ## Prepare message to log: add end of line to message
        msg += EOL
        ## Depending on options, log into file or standard output
        if hasattr(log, "log_filename") and log.log_filename is not None:
            # Open log file
            log_file = open_file(log.log_filename, 'a')
            # Write message into log file
            log_file.write(msg)
            # Close log file
            log_file.close()
        # If no log filename has been specified, check if verbose mode has been set by user
        elif hasattr(log, "verbose") and log.verbose:
            sys.stdout.write(msg)
    except IOError as exception:
        raise Error("Cannot write into log file '%s'." % options.log_filename,
                    exception)
Пример #2
0
def open_file(filename, mode, encoding=ENCODING):
    """! @brief Open file in specified mode (automatically decode file in unicode).
    @param filename Full path to file to open.
    @param mode Read or write mode.
    @param encoding Encoding mode. Default value is 'utf-8'.
    @return File handler.
    """
    try:
        try:
            return open(filename, mode, encoding=encoding)
        except TypeError:
            import codecs
            return codecs.open(filename, mode, encoding=encoding)
    except IOError as exception:
        raise Error("Cannot open file.", exception)
Пример #3
0
 def setUp(self):
     # Instantiate an Error object
     self.error = Error("This is an error.")