hutts_verification.utils package

Submodules

hutts_verification.utils.hutts_logger module

Wraps the logic required to setup and use a custom logger while disabling the built-in flask logger.

Example usage:

First import the logger from the hutts_logger module… from hutts_verification.utils.hutts_logger import logger then start logging.

logger.info('logging an example')

There are 5 logging levels, as shown below with their corresponding function call (from lowest to highest level):

  • DEBUG - logger.debug(message)
  • INFO - logger.info(message)
  • WARNING - logger.warning(message)
  • ERROR - logger.error(message)
  • CRITICAL - logger.critical(message)

The default level of the logger will be INFO, unless the flask app is run in debug mode, in which case the logger level will be DEBUG. What this means is that messages lower than INFO, i.e. DEBUG, will not be shown, again this is unless the flask app is run in debug mode.

LOGGING_DEFAULT_LEVEL = 10

Specifies the date format to be used by the various logging formatters.

LOGGING_LOGGER_NAME = 'hutts_logger'

Specifies the default level for logging.

LOGGING_LOG_DATE_FMT = '%Y-%m-%d %H:%M:%S'

Indicates whether or not to log to console.

LOGGING_LOG_TO_CONSOLE = True

Specifies the log format to be used when logging to the console.

LOGGING_LOG_TO_CONSOLE_COLOURS = {'DEBUG': 'bold_cyan', 'ERROR': 'bold_red', 'CRITICAL': 'bold_red,bg_white', 'WARNING': 'bold_yellow', 'INFO': 'bold_white'}

Specifies the secondary colours to be used to indicate the different levels when logging to console. For a more detailed description see the colorlog documentation: https://github.com/borntyping/python-colorlog.

LOGGING_LOG_TO_CONSOLE_FMT = '[%(asctime)s.%(msecs)03d]%(log_color)s[%(levelname)8s]%(reset)s%(message_log_color)s -- (%(filename)s:%(lineno)d) -- %(message)s'

Specifies the colours to be used to indicate the different levels when logging to console. For a more detailed description see the colorlog documentation: https://github.com/borntyping/python-colorlog.

LOGGING_LOG_TO_CONSOLE_SEC_COLOURS = {'message': {'ERROR': 'red', 'CRITICAL': 'bold_red,bg_white'}}

Indicates whether or not to log to a file.

LOGGING_LOG_TO_FILE = True

Specifies the name of the log file.

LOGGING_LOG_TO_FILE_BACKUP_COUNT = 1

Specifies the encoding for the log file.

LOGGING_LOG_TO_FILE_DEFAULT_DIR = 'log/'

Specifies the log format to be used when logging to a file.

LOGGING_LOG_TO_FILE_ENCODING = 'utf8'

A global reference to the custom logger to be used. It is initialised to the default python logger to avoid errors during installation of packages.

LOGGING_LOG_TO_FILE_FILENAME = 'hutts_verification.log'

Specifies the default directory for the log file in the event that one is not specified during setup.

LOGGING_LOG_TO_FILE_FMT = '[%(asctime)s.%(msecs)03d][%(levelname)8s] -- (%(filename)s:%(lineno)d) -- %(message)s'

Specifies the maximum number of bytes for the log file before a rotate (assuming a rotating file is used).

LOGGING_LOG_TO_FILE_MAX_BYTES = 100000

Specifies the number of backups for the log file (assuming a rotating file is used).

disable_flask_logging(app_instance)[source]

This function disables the flask logging, which interferes with the custom logger.

Parameters:(obj) (app_instance) – A reference to the current flask server application.
get_console_handler()[source]

This function is responsible for creating a console log handler with a global format and returning it.

Returns:
  • (obj): A log handler that is responsible for logging to the console.
get_file_handler(log_dir=None)[source]

This function is responsible for creating a file log handler with a global format and returning it.

Returns:
  • (obj): A log handler that is responsible for logging to a file.
prettify_json_message(json_message)[source]

This function is a helper function that is used to prettify a json/dict message obj so that is more readable for humans when it is logged.

Parameters:(dict) (json_message) – A message that is to be prettified before being logged.
Returns:
  • (str): A prettified json message string.
setup_logger()[source]

This function is responsible for creating the custom logger and delegating the creation of its handlers.

hutts_verification.utils.image_handling module

Utility functions to manage image handling from given parameters.

grab_image(path=None, stream=None, url=None, string=None)[source]

This function grabs the image from URL, or image path and applies necessary changes to the grabbed images so that the image is compatible with OpenCV operation.

Parameters:
  • (path) (str) – The path to the image if it resides on disk.
  • (stream) (str) – A stream of text representing an image upload.
  • (url) (str) – URL representing a path to where an image should be fetched.
  • (string) (str) – A base64 encoded string of the image.
Raises:
  • ValueError: If no path, stream, URL or Base64 string was found.
Returns:
  • (obj): Image that is now compatible with OpenCV operations.

hutts_verification.utils.pypath module

Utilities to help solve Python path/directory/file issues regarding files being installed in either the dist-packages or the site-packages folder.

correct_path(path)[source]

This function checks if the given path exists in most known Python package installation directories and returns the corresponding path.

  • path (str || Path): The path that has to be checked.
Returns:
(str): The correct path if it exists, else None.

Module contents