common Module

Module to store shared code.

Functions and classes are put here when they are meant to be reused in different parts of the program. Some of them were built just for internal use, but others (listed bellow) can also be incorporated in user code as well (for example, in the crawler code).

class common.EchoHandler(configurationsDictionary={}, loggingFileName='', defaultLoggingLevel='INFO')

Manage logging and printing of messages.

Provides a unified and common infrastructure for information output in different parts of the program, besides abstracting low level details about output operations.

defaultConfig = {'loggingfilemode': 'w', 'logging': True, 'verbose': False, 'loggingpath': '.'}

Default configuration values. If no local value has been specified for an option in an instance, the value defined here for that option is used instead.

mandatoryConfig = {'loggingfilemode': None, 'logging': None, 'verbose': None, 'loggingpath': None}

Mandatory configuration values. If a value for an option here is None, the handler respects the instance local configuration value for that option (or the default configuration value if no local value has been specified). Otherwise, the value specified here overrides any local value.

__init__(configurationsDictionary={}, loggingFileName='', defaultLoggingLevel='INFO')

Constructor.

Root logger configuration is done here using logging.basicConfig(). This means that loggingFileName and defaultLoggingLevel parameters are defined by the first module in an import hierarchy that instantiate an EchoHandler object, as subsequent calls to logging.basicConfig() have no effect. So, for example, when running the client, it is the first module to instantiate an EchoHandler object. The client module imports the crawler module, but as crawler is the second module in the hierarchy, it will use the root logger configuration defined in client, despite any local settings of loggingFileName and defaultLoggingLevel.

Args:
  • configurationsDictionary (dict): Holds values for the three configuration options supported: verbose (bool), logging (bool) and loggingpath (str).
  • loggingFileName (str): Name of the file used to save logging messages.
  • defaultLoggingLevel (str): Level at which the root logger must be set. Supports any of the level names defined in Python’s built-in logging module.

See also

Python’s built-in logging module documentation.

_extractConfig(configurationsDictionary)

Extract and store configurations.

The configurations are extracted from configurationsDictionary and stored in separate instance variables. Each configuration has a default value, defined in defaultConfig, so it is possible to use EchoHandler without specifying any of them. The values of mandatoryConfig are also checked here and, if set, override the values given in configurationsDictionary, as well as default values.

out(message, loggingLevel='', mode='both')

Log and/or print a message.

Args:
  • message (str): The message to be logged and/or printed.
  • loggingLevel (str): Level to use when logging the message. Supports any of the level names defined in Python’s built-in logging module. If the level is bellow the default, no message is emited. If it is not specified, the default level is used.
  • mode (str): Control wether the message should be only logged, only printed or both. The corresponding accepted values are: “logonly”, “printonly” and “both”. This gives a fine-grained control, at the code level, over the output destination of the message.
common.str2bool(stringToConvert)

Convert a string to a boolean.

Args:
  • stringToConvert (str): The following string values are accepted as True: “true”, “t”, “yes”, “y”, “on”, “1”. The following string values are accepted as False: “false”, “f”, “no”, “n”, “off”, “0”. Input string evaluation is always case insensitive.
Returns:
True or False, depending on input value.
Raises:
TypeError: If input string is not one of the accepted values.

Table Of Contents