pyats.utils.import_utils package

Submodules

class pyats.utils.import_utils.flex.FlexImporter(module_prefix='')

Bases: object

Flexible module loader, overloading the load() api to be able to load from:
  • arbitrary file directly

  • absolute module name (imported or not)

  • module object

Parameters:

(str) (module_prefix) – only used when the loading is done arbitrarily from file outside of PYTHONPATH.

load(obj)

Load object into module

API to load user supplied object to a python module.

Parameters:

(obj) (job) – Could be a file, a ModuleType, name of a loaded module, or an absolute module path

Return type:

types.ModuleType()

load_module_from_file(target)

load module from file

Loads a target file outside of PYTHONPATH into a python module object. This api allows the loading of arbitrary python files and update the sys.path list with the given module location.

Parameters:

(str) (target) –

Return type:

python module

class pyats.utils.import_utils.legacy.LegacyImporter

Bases: MetaPathFinder

Python import hook, enabling users with legacy imports in their code to continue leveraging legacy imports (eg, translate old import calls into newer import calls)

Example

# translate from legacy.package.path import X

# into from new_package.path import X

Warning

  • black magic -

  • do not tamper with -

classmethod find_spec(name, path=None, target=None)

undocumented, do not use

classmethod install()

undocumented, do not use

classmethod invalidate_caches()

undocumented, do not use

classmethod register(*args, **kwargs)

registers a new import mapping. Accepts a legacy name in string or regex, and maps to a new string/repl (in the case of regex)

Parameters:
  • (str/regex) (legacy) –

  • (str) (current) – name is provided as a regex object)

Examples

LegacyImporter.register(old = ‘new’)

# assuming old_to_new translates old name to new name # and new_to_old translates from new name to old name LegacyImporter.register(old_to_new, new_to_old)

classmethod reverse_translate(name)

translates a new import string to an old import string

classmethod translate(name)

translates an old import string to a new import string

class pyats.utils.import_utils.legacy.LegacyLoader(module_name)

Bases: Loader

Import Loader, ditches the old module creation process and replaces it with actual new module

create_module(spec)

undocumented, do not use

exec_module(legacy_module)

undocumented, do not use

pyats.utils.import_utils.misc.import_from_name(name)

Translate a ‘x.y.z’ style string into ‘from x.y import z’, and return z. Allows users to import using string names instead of calling importer.

Example

>>> obj = import_from_name('imporlib.reload')
Parameters:

(str) (name) –

Return type:

Object imported

Raises:

ImportError

pyats.utils.import_utils.misc.on_import(f)

decorator, invokes given function when it is imported

Example

@on_import def print_msg():

print(‘message to print’)

pyats.utils.import_utils.misc.str_or_list(value)

check_file

translates str/list into list.

pyats.utils.import_utils.misc.translate_host(hostname)

translates a host name or address to a string representation of an IPv4/IPv6 address.

Example

>>> host = translate_host('asg-lnx-1')
Parameters:

(str) (hostname) – representation of an IPv4/v6 address

Return type:

Returns an IPv4/v6 address as a string, such as ‘10.85.87.95’

Raises:

socket.gaierror (getaddrerror – given host name is invalid):