Blog Viewer

How-To: Load XML files into tables

By Erdem posted 08-06-2015 04:26




You want to load locally stored XML files into Table widgets for post-processing.




The Table widget provides a path constructor parameter that loads the XML from a locally stored data file, rather from a remote Device. 


For example, using the Python shell:


1 >>> from jnpr.junos.op.xcvr import XcvrTable
2 >>>
3 >>> xcvrs = XcvrTable(path='/var/tmp/xcvrs/xcvrs_CHI-MX480-re0_20131226145947.xml')
4 >>>
5 >>> xcvrs.get()
6 XcvrTable:/var/tmp/xcvrs/xcvrs_CHI-MX480-re0_20131226145947.xml: 8 items


At this point, you can use the xcvrs tTable as if you had sourced the data directly from the Device.  For further usage of the Table widget, please see the topic page, here.




Using locally stored data is often useful for post-processing automation tasks.  Consider the example where you'd like to collect information on all of your pluggable transceivers in your network.  You could collect that information as a series of XML files as described in the "How-To", here,  If you have a collection of these XML files, you could load them into a collection of tables, as illustrated:


1 >>> from glob import glob
2 >>> files = glob('/var/tmp/xcvrs/*.xml')
3 >>> files
4 ['/var/tmp/xcvrs/xcvrs_CHI-SRX3400-1_20131226150003.xml', '/var/tmp/xcvrs/xcvrs_CHI-MX80-48T-1_20131226145954.xml', '/var/tmp/xcvrs/xcvrs_CHI-MX480-re0_20131226145947.xml','/var/tmp/xcvrs/xcvrs_NYCD-MX240-1_20131226145941.xml']


You can then create a collection of XcvrTable variables, like so:


1 >>> xcvr_tables = [XcvrTable(path=f) for f in files]
2 >>> for t in xcvr_tables: t.get()
3 ...
4 XcvrTable:/var/tmp/xcvrs/xcvrs_CHI-SRX3400-1_20131226150003.xml: 1 items
5 XcvrTable:/var/tmp/xcvrs/xcvrs_CHI-MX80-48T-1_20131226145954.xml: 4 items
6 XcvrTable:/var/tmp/xcvrs/xcvrs_CHI-MX480-re0_20131226145947.xml: 8 items
7 XcvrTable:/var/tmp/xcvrs/xcvrs_NYCD-MX240-1_20131226145941.xml: 14 items


Let's say you'd like to take an accounting for each Xcvr type.  We know that each table item has a field called type that designates this information.


For example, the first Xcvr in the first table has a type:

1 >>> xcvr_tables[0][0].type
2 'SFP-SX'


So if we want to collect off the Xcvrs from all of the tables, we use two items in our Python toolbox: iterator chaining and the Counter:


1 >>> from itertools import chain
2 >>> from collections import Counter


The chain function allows us to iterate through a list of tables in the same way we would iterate through a list of table items.  So by using chain, we can iterate through all table items. 


The Counter widget allows us to create a count by each type of thing in a list.  So if we produce a list of Xcvr types, the Counter widget will count up each of the individual times. 


Putting these two things together looks like this:


1 >>> type_count = Counter([x.type for x in chain(*xcvr_tables)])


We can then examine the type_count variable and see what we've got:


1 >>> type_count
2 Counter({'XFP-10G-SR': 10, 'SFP-SX': 9, 'SFP+-10G-SR': 7, 'SFP-T': 1})