Utils

get_logger(name, DEBUG=False, verbosity=4, handle=['stderr'], with_threads=False)

This function provides common logging facility by creating instances of loggers from python standard logging library.

Parameters:
  • name (str) – Name of the logger
  • DEBUG (bool) – Enables/disables debugging output
  • handle (list) – Changing value of this parameter is not recommended.
Returns:

Instance of logger object

interface_split(interface)

Function for splitting names of interfaces to interface type and number. Example: FastEthernet0/18 -> FastEthernet, 0/18 :param str interface: Interface name. :return: Interface type, interface number

vlan_range_shortener(full_vlan_list)

Function for shortening list of allowed VLANs on trunk interface. Example: [1,2,3,4,5] -> [“1-5”]. Reverse function is vlan_range_expander.

Parameters:full_vlan_list – List of integers representing VLAN numbers.
Returns:List of strings.
vlan_range_expander(all_vlans)

Function for expanding list of allowed VLANs on trunk interface. Example: 1-4096 -> range(1, 4097). Can be used when trying to figure out whether certain VLAN is allowed or not. Reverse function is vlan_range_shortener.

Parameters:all_vlans – Either list ([“1-10”, “15”, “20-30”]) or string (“1-10,15,20-30”) of VLAN range.
Returns:List of VLAN numbers (integers).
int_name_convert(int_name, out_type=None)

Function for converting long interface names to short and vice versa. Example: “GigabitEthernet0/12” -> “Gi0/12” and “Gi0/12” -> “GigabitEthernet0/12”

Parameters:
  • int_name (str) – Interface name
  • out_type (str) – Desired form of interface name, either “long” or “short”. If None, return opposite form
Returns:

Interface name

mac_addr_convert(mac_address='')

Function for providing single format for MAC addresses.

Parameters:mac_address (str) – MAC address to be converted.
Returns:MAC address in format XX:XX:XX:XX:XX:XX.
update_dict(orig_dict, update_dict)

Function for updating dictionary values.

Parameters:
  • orig_dict (dict) – Dictionary to be updated.
  • update_dict (dict) – Dictionary to update from.
Returns:

Updated dictionary.

check_path(path, create_missing=True)

Function for checking path availability, returns path if specified folder exists, False otherwise. Can also create missing folders. Used for handling absolute and relative paths.

Parameters:
  • path (str) – Path of the folder.
  • create_missing (bool) – Whether or not to create missing folders.
Returns:

Bool

Filter

class Filter(required={}, excluded={}, exact_match=True, DEBUG=False)

Bases: object

Object for making filtering of required field easier.

This class finds elements in JSON-like format based on provided values.

Parameters:
  • required (dict) – Dictionary of required keys. Example: {“key1”: “value1”} will return only those dictionaries, which key “key1” contains the value “value1”. You can also use list of required values, such as {“key1”: [“value1”, “value2”]} together with option exact_match=False.
  • excluded (dict) – Dictionary of excluded keys. Example: {“key1”: “value1”} will return only those dictionaries, which key “key1” does not contain the value “value1”. You can also use list of excluded values, such as {“key1”: [“value1”, “value2”]} together with option exact_match=False.
  • exact_match (bool) – Specifies whether the filter value must match exactly, or partially.
  • DEBUG – Enables/disables debugging output.
dict_cleanup(data)

Function for filtering dictionary structures (eg. dictionary where values are also dictionaries).

Parameters:data (dict) – Dictionary containing dicts as values, which you want to filter.
Returns:Filtered dictionary.
list_cleanup(data)

Function for filtering list structures (eg. list of dictionaries).

Parameters:data (list) – List of dictionaries, which you want to filter.
Returns:Filtered list of dictionaries.
universal_cleanup(data=None)

This function calls proper cleanup function base on data type.

Parameters:data – Data to be filtered, either list of dictionaries or dictionary of dictionaries.
Returns:Filtered data

OutputFilter

class OutputFilter(data=None, required=[], excluded=[])

Bases: object

This class helps to minimize dictionary structure by specifying only the desired keys.

Parameters:
  • data – Data to be filtered.
  • required (list) – List of required keys. Returned entries will contain only these specified keys. Example: {“key1”: “value1”, “key2”: “value2”} with required [“key1”] will only return {“key1”: “value1”}.
  • excluded (list) – List of excluded keys. Returned entries will not contain these specified keys. Example: {“key1”: “value1”, “key2”: “value2”} with excluded [“key1”] will only return {“key2”: “value2”}.
get()

After instantiating object, call this function to retrieve filtered data.

Returns:Filtered data.