Utils

Just a set of helpful utility methods (HHAC/IP to str, file helpers, log utils).

Network Utils

File Utils

File utilities that might be useful

chains.utils.file_utils.all_files_in_directory(path)[source]

Recursively list all files under a directory

Parameters:path – the path of the directory to traverse
Returns:a list of all the files contained withint the directory
chains.utils.file_utils.file_dir(file_path)[source]

Root directory for a file_path

Parameters:file_path – a fully qualified file path
Returns:the directory which contains the file
chains.utils.file_utils.relative_dir(file_path, rel_dir)[source]

Relative directory to the file_path

Parameters:file_path – a fully qualified file path
Returns:the relative directory
chains.utils.file_utils.test_utils()[source]

Test the utility methods

Log Utils

Log utilities that might be useful

chains.utils.log_utils.get_logger()[source]

Setup logging output defaults

chains.utils.log_utils.panic(mesg)[source]

Throw a critical message and raise a runtime exceptions

chains.utils.log_utils.test_utils()[source]

Test the utility methods

Data Utils

Data utilities that might be useful

chains.utils.data_utils.make_dict(obj)[source]

This method creates a dictionary out of a non-builtin object

chains.utils.data_utils.is_builtin(obj)[source]
chains.utils.data_utils.get_value(data, key)[source]

Follow the dot notation to get the proper field, then perform the action

Parameters:
  • data – the data as a dictionary (required to be a dictionary)
  • key

    the key (as dot notation) into the data that gives the field (IP.src)

    Returns:
    the value of the field(subfield) if it exist, otherwise None
chains.utils.data_utils.test_utils()[source]

Test the utility methods

Cache

Cache class for key/value pairs

class chains.utils.cache.Cache(max_size=1000, timeout=None)[source]

Bases: object

In process memory cache. Not thread safe. Usage:

cache = Cache(max_size=5, timeout=10) cache.set(‘foo’, ‘bar’) cache.get(‘foo’) >>> bar time.sleep(11) cache.get(‘foo’) >>> None cache.clear()
set(key, value)[source]

Add an item to the cache :param key: item key :param value: the value associated with this key

get(key)[source]

Get an item from the cache :param key: item key

Returns:the value of the item or None if the item isn’t in the cache
clear()[source]

Clear the cache

dump()[source]

Dump the cache (for debugging)

chains.utils.cache.test()[source]

Test for the Cache class