tokio.connectors.common module

Common methods and classes used by connectors

class tokio.connectors.common.CacheableDict(input_file=None)[source]

Bases: dict

Generic class to support connectors that are dicts that can be cached as JSON

When deriving from this class, the child object will have to define its own load_native() method to be invoked when input_file is not JSON.

__init__(input_file=None)[source]

Either initialize as empty or load from cache

Parameters:input_file (str) – Path to either a JSON file representing the dict or a native file that will be parsed into a JSON-compatible format
_save_cache(output, **kwargs)[source]

Generates serialized representation of self

Parameters:output – Object with a .write() method into which the serialized form of self will be passed
load(input_file=None)[source]

Wrapper around the filetype-specific loader.

Infer the type of input being given, dispatch the correct loading function, and populate keys/values.

Parameters:input_file (str or None) – The input file to load. If not specified, uses whatever self.input_file is
load_json(input_file=None)[source]

Loads input from serialized JSON

Load the serialized format of this object, encoded as a json dictionary. This is the converse of the save_cache() method.

Parameters:input_file (str or None) – The input file to load. If not specified, uses whatever self.input_file is
load_native(input_file=None)[source]

Parse an uncached, native object

This is a stub that should be overloaded on derived classes.

Parameters:input_file (str or None) – The input file to load. If not specified, uses whatever self.input_file is
save_cache(output_file=None, **kwargs)[source]

Serializes self into a JSON output.

Save the dictionary in a JSON file. This output can be read back in using load_json().

Parameters:
  • output_file (str or None) – Path to file to which json should be written. If None, write to stdout. Default is None.
  • kwargs (dict) – Additional arguments to be passed to json.dumps()
class tokio.connectors.common.SubprocessOutputDict(cache_file=None, from_string=None, silent_errors=False)[source]

Bases: dict

Generic class to support connectors that parse the output of a subprocess

When deriving from this class, the child object will have to

  1. Define subprocess_cmd after initializing this parent object
  2. Define self.__repr__ (if necessary)
  3. Define its own self.load_str
  4. Define any introspective analysis methods
_load_subprocess(*args)[source]

Run a subprocess and pass its stdout to a self-initializing parser

load(cache_file=None)[source]

Load based on initialization state of object

Parameters:cache_file (str or None) – The cached input file to load. If not specified, uses whatever self.cache_file is
load_cache(cache_file=None)[source]

Load subprocess output from a cached text file

Parameters:cache_file (str or None) – The cached input file to load. If not specified, uses whatever self.cache_file is
load_str(input_str)[source]

Load subprocess output from a string

Parameters:input_str (str) – The text that came from the subprocess’s stdout and should be parsed by this method.
save_cache(output_file=None)[source]

Serialize subprocess output to a text file

Parameters:output_file (str) – Path to a file to which the output cache should be written. If None, write to stdout.
class tokio.connectors.common.SubprocessOutputList(cache_file=None, from_string=None, silent_errors=False)[source]

Bases: list

Generic class to support connectors that parse the output of a subprocess

When deriving from this class, the child object will have to

  1. Define subprocess_cmd after initializing this parent object
  2. Define self.__repr__ (if necessary)
  3. Define its own self.load_str
  4. Define any introspective analysis methods
_load_subprocess(*args)[source]

Run a subprocess and pass its stdout to a self-initializing parser

load(cache_file=None)[source]

Load based on initialization state of object

Parameters:cache_file (str or None) – The cached input file to load. If not specified, uses whatever self.cache_file is
load_cache(cache_file=None)[source]

Load subprocess output from a cached text file

Parameters:cache_file (str or None) – The cached input file to load. If not specified, uses whatever self.cache_file is
load_str(input_str)[source]

Load subprocess output from a string

Parameters:input_str (str) – The text that came from the subprocess’s stdout and should be parsed by this method.
save_cache(output_file=None)[source]

Serialize subprocess output to a text file

Parameters:output_file (str) – Path to a file to which the output cache should be written. If None, write to stdout.
tokio.connectors.common.walk_file_collection(input_source)[source]

Walk all member files of an input source.

Iterator that visits every member of an input source (either directory or tarfile) and yields its file name, last modify time, and a file handle to its contents.

Parameters:

input_source (str) – A path to either a directory containing files or a tarfile containing files.

Yields:

tuple – Attributes for a member of input_source with the following data:

  • str: fully qualified path corresponding to its name
  • float: last modification time expressed as seconds since epoch
  • file: handle to access the member’s contents