| 1 |
""" |
| 2 |
A dictionary-like object of read-only facts about the Junos device. |
| 3 |
|
| 4 |
These facts are accessed as the `facts` attribute of a `Device` object |
| 5 |
instance. For example, if `dev` is an instance of a `Device` object, |
| 6 |
the hostname of the device can be accessed with:: |
| 7 |
|
| 8 |
dev.facts['hostname'] |
| 9 |
|
| 10 |
Force a refresh of all facts with:: |
| 11 |
|
| 12 |
dev.facts_refresh() |
| 13 |
|
| 14 |
Force a refresh of a single fact with:: |
| 15 |
|
| 16 |
dev.facts_refresh(keys='hostname') |
| 17 |
|
| 18 |
Force a refresh of a set of facts with:: |
| 19 |
|
| 20 |
dev.facts_refresh(keys=('hostname','domain','fqdn')) |
| 21 |
|
| 22 |
NOTE: The dictionary key for each available fact is guaranteed to exist. If |
| 23 |
there is a problem gathering the value of a specific fact/key, or if |
| 24 |
the fact is not supported on a given platform, then the fact/key will |
| 25 |
have the value None (the None object, not a string.) |
| 26 |
|
| 27 |
Accessing a dictionary key which does not correspond to an available fact |
| 28 |
will raise a KeyError (the same behavior as accessing a non-existent key |
| 29 |
of a normal dict.) |
| 30 |
|
| 31 |
The following dictionary keys represent the available facts and their meaning: |
| 32 |
|
| 33 |
""" |
| 34 |
|
| 35 |
import sys |
| 36 |
|
| 37 |
import jnpr.junos.facts.current_re |
| 38 |
import jnpr.junos.facts.domain |
| 39 |
import jnpr.junos.facts.ethernet_mac_table |
| 40 |
import jnpr.junos.facts.file_list |
| 41 |
import jnpr.junos.facts.get_chassis_cluster_status |
| 42 |
import jnpr.junos.facts.get_chassis_inventory |
| 43 |
import jnpr.junos.facts.get_route_engine_information |
| 44 |
import jnpr.junos.facts.get_software_information |
| 45 |
import jnpr.junos.facts.get_virtual_chassis_information |
| 46 |
import jnpr.junos.facts.ifd_style |
| 47 |
import jnpr.junos.facts.iri_mapping |
| 48 |
import jnpr.junos.facts.personality |
| 49 |
import jnpr.junos.facts.swver |
| 50 |
import jnpr.junos.facts.is_linux |
| 51 |
import jnpr.junos.facts.vmhost |
| 52 |
|
| 53 |
|
| 54 |
def _build_fact_callbacks_and_doc_strings(): |
| 55 |
""" |
| 56 |
Imports the fact modules and returns callbacks and doc_strings. |
| 57 |
|
| 58 |
:returns: |
| 59 |
A tuple of callbacks and doc_strings. |
| 60 |
callbacks - a dict of the callback function to invoke for each fact. |
| 61 |
doc_strings - a dict of the doc string for each fact. |
| 62 |
|
| 63 |
:raises: |
| 64 |
RuntimeError if more than one module claims to provide the same fact. |
| 65 |
This is an indication of incorrectly written fact module(s). |
| 66 |
In order to remain deterministic, each fact must be |
| 67 |
provided by a single module. |
| 68 |
""" |
| 69 |
callbacks = {} |
| 70 |
doc_strings = {} |
| 71 |
for name, module in sys.modules.items(): |
| 72 |
if name.startswith("jnpr.junos.facts.") and module is not None: |
| 73 |
new_doc_strings = module.provides_facts() |
| 74 |
for key in new_doc_strings: |
| 75 |
if key not in callbacks: |
| 76 |
callbacks[key] = module.get_facts |
| 77 |
doc_strings[key] = new_doc_strings[key] |
| 78 |
else: |
| 79 |
raise RuntimeError( |
| 80 |
"Both the %s module and the %s module " |
| 81 |
"claim to provide the %s fact. Please " |
| 82 |
"report this error." |
| 83 |
% (callbacks[key].__module__, module.__name__, key) |
| 84 |
) |
| 85 |
return (callbacks, doc_strings) |
| 86 |
|
| 87 |
|
| 88 |
# Import all of the fact modules and build the callbacks and doc strings |
| 89 |
(_callbacks, _doc_strings) = _build_fact_callbacks_and_doc_strings() |
| 90 |
|
| 91 |
# In case optimization flag is enabled, it strips of docstring and __doc__ becomes None |
| 92 |
if __doc__ is None: |
| 93 |
__doc__ = "" |
| 94 |
|
| 95 |
# Append the doc string (__doc__) with the documentation for each fact. |
| 96 |
for key in sorted(_doc_strings, key=lambda s: s.lower()): |
| 97 |
__doc__ += ":%s:\n %s\n" % (key, _doc_strings[key]) |