File: library-reference.rst

package info (click to toggle)
pysmi 0.3.4-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,080 kB
  • sloc: python: 6,613; makefile: 165
file content (170 lines) | stat: -rw-r--r-- 4,802 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

PySMI library
=============

The *MibCompiler* object is the top-most interface to PySMI library features.
It holds together the otherwise isolated pieces of the compiler infrastructure
and manages the workflow of ASN.1 MIB transformation.

This example showcases some of its features:

.. code-block:: python

   from pysmi.reader import HttpReader
   from pysmi.searcher import StubSearcher
   from pysmi.writer import CallbackWriter
   from pysmi.parser import SmiStarParser
   from pysmi.codegen import JsonCodeGen
   from pysmi.compiler import MibCompiler

   inputMibs = ['IF-MIB', 'IP-MIB']

   httpSources = [('mibs.snmplabs.com', 80, '/asn1/@mib@')]

   # store compiled MIBs by calling this function
   def store_mibs(mibName, jsonDoc, cbCtx):
       print('# MIB module %s' % mibName)
       print(jsonDoc)

   mibCompiler = MibCompiler(
       SmiStarParser(), JsonCodeGen(), CallbackWriter(store_mibs)
   )

   # pull ASN.1 MIBs over HTTP
   mibCompiler.addSources(*[HttpReader(*x) for x in httpSources])

   # never recompile MIBs with ASN.1 MACROs
   mibCompiler.addSearchers(StubSearcher(*JsonCodeGen.baseMibs))

   status = mibCompiler.compile(*inputMibs)

   print(status)

.. toctree::
   :maxdepth: 2

   /pysmi/compiler/mibcompiler
   /pysmi/compiler/mibstatus

MIB sources
-----------

PySMI offers a handful of distinct transport mechanisms for fetching MIBs by
name from specific locations. In all cases MIB module name to file name match
may not be exact -- some name fuzzying can be performed to mitigate
possible changes to MIB file name.

.. toctree::
   :maxdepth: 2

   /pysmi/reader/localfile/filereader
   /pysmi/reader/zipreader/zipreader
   /pysmi/reader/httpclient/httpreader
   /pysmi/reader/ftpclient/ftpreader
   /pysmi/reader/callback/callbackreader

Conditional compilation
-----------------------

There are cases when MIB transformation may or must not be performed.
Such cases include:

* foundation MIBs containing manually implemented pieces or ASN.1 MACRO's
* obsolete MIBs fully reimplemented within modern MIBs
* already transformed MIBs

:ref:`MibCompiler <compiler.MibCompiler>` expects user to supply a
*searcher* object that would allow or skip MIB transformation for particular
name based on whatever reason it is aware of.

In general, *searcher* logic is specific to target format. At the time being,
only `pysnmp <http://snmplabs.com/pysnmp>`_ code generation backend requires
such filtering.

.. toctree::
   :maxdepth: 2

   /pysmi/searcher/pyfile/pyfilesearcher
   /pysmi/searcher/pypackage/pypackagesearcher
   /pysmi/searcher/stub/stubsearcher

Parser configuration
--------------------

MIBs may be written in one of the two major SMI language versions (v1 and v2).
Some MIBs may contain typical errors.

PySMI offers a way to customize the parser to consume either of the major SMI
grammars as well as to recover from well-known errors in MIB files.

.. toctree::
   :maxdepth: 2

   /pysmi/parser/smi/parserfactory
   /pysmi/parser/smi/dialect

Code generators
---------------

Once ASN.1 MIB is parsed up, AST is passed to a code generator which turns
AST into desired representation of the MIB.

.. toctree::
   :maxdepth: 2

   /pysmi/codegen/jsondoc/jsoncodegen
   /pysmi/codegen/pysnmp/pysnmpcodegen
   /pysmi/codegen/null/nullcodegen

Borrow pre-compiled MIBs
------------------------

Some MIBs in circulation appear broken beyond automatic repair. To
handle such cases PySMI introduces the *MIB borrowing*
functionality. When :ref:`MibCompiler <compiler.MibCompiler>`
gives up compiling a MIB, it can try to go out and take a copy of
already transformed MIB to complete the request successfully.

.. toctree::
   :maxdepth: 2

   /pysmi/borrower/anyfile/anyfileborrower
   /pysmi/borrower/pyfile/pyfileborrower

Write compiled MIBs
-------------------

Successfully transformed MIB modules' contents will be passed to *writer*
object given to :ref:`MibCompiler <compiler.MibCompiler>` on instantiation.

.. toctree::
   :maxdepth: 2

   /pysmi/writer/localfile/filewriter
   /pysmi/writer/pyfile/pyfilewriter
   /pysmi/writer/callback/callbackwriter

Examples
--------

The following examples focus on various feature of the PySMI library.

.. toctree::
   :maxdepth: 2

   /examples/download-and-compile-smistar-mibs-into-json.rst
   /examples/download-and-compile-smistar-mibs-into-pysnmp-files.rst
   /examples/compile-smistar-mibs-into-pysnmp-files-if-needed.rst
   /examples/compile-smiv2-mibs-from-text-into-pysnmp-code.rst
   /examples/borrow-precompiled-pysnmp-files-on-failure.rst
   /examples/always-borrow-precompiled-pysnmp-files.rst

In case of any troubles or confusion, try enabling PySMI debugging
and watch the output:

.. code-block:: python

   from pysmi import debug

   debug.setLogger(debug.Debug('all'))