Package: rexutil
rex====[![PyPI Version](https://img.shields.io/pypi/v/rexutil.svg)](https://pypi.python.org/pypi/rexutil)A simple python module which serves as an abstraction to the pythonregular expression (re) module in some use cases.+ [Introduction] (https://github.com/bdastur/rex/blob/master/README.md#introduction)+ [Getting Started] (https://github.com/bdastur/rex/blob/master/README.md#getting-started)+ [Features] (https://github.com/bdastur/rex/blob/master/README.md#features)+ [LR Value Parsing] (https://github.com/bdastur/rex/blob/master/README.md#lrvalue)+ [Multiple LR Value Parsing] (https://github.com/bdastur/rex/blob/master/README.md#mlrvalue)+ [Table formatParsing] (https://github.com/bdastur/rex/blob/master/README.md#tabular)+ [RE Pattern Abstractions] (https://github.com/bdastur/rex/blob/master/README.md#pabstractions)------# Introduction:<a name="introduction"></a>The rex module was born out of necessity. I found myself writing code to parse a lot of command line output for some switches which did not have a good API interface and also in some cases where CLI was the best option to get the data I needed.In a lot of cases the output format was very similar where I could generalize and try to come up with a common API that can be used for any such CLI. I could not find anything that would fit the bill, and rex was born.------# Getting Started:<a name="getting-started"></a>## Installing using pip:```pip install rexutil```## Installing (git clone):```git clone https://github.com/bdastur/rex -b master```## Usage:```import rex```------# Features:<a name="features"></a>The module provides the following main abstractions:## **1. Parsing a LR Value String:**<a name="lrvalue"></a>Consider the output of a CLI "show chassis" below from a switch.```chassis : UCS43Serial no : USC788dcFirmware : 12.01(99)PID : UCS4343UUID : URY888dR7R7R8-090-99Description : Ucs M3 Series Chassis```The LRValue parser will parse this output and return a dictionary:```{'firmware': '12.01(99)','description': 'Ucs M3 Series Chassis','serial_no': 'USC788dc','pid': 'UCS4343','chassis': 'UCS43','uuid': 'URY888dR7R7R8-090-99'}```## **2. Parsing multiple LR Value strings.**<a name="mlrvalue"></a>A similar use case is where you have multiple  blocks of LRValue paris.Consider the example of a CLI to display adapters.```Physical Drive Number 21:Controller: SLOT-4Health: GoodStatus: Unconfigured GoodManufacturer: ATAModel: ST91000640NSPredictive Failure Count: 0Drive Firmware: CC03Coerced Size: 952720 MBType: HDDPhysical Drive Number 22:Controller: SLOT-4Health: GoodStatus: Unconfigured GoodManufacturer: ATAModel: ST91000640NSPredictive Failure Count: 0Drive Firmware: CC03Coerced Size: 952720 MB```In this case the LRValue parser will parse the output andreturn a list of dictionary object```[ { },{ 'coerced_size': '952720 MB','controller': 'SLOT-4','drive_firmware': 'CC03','health': 'Good','manufacturer': 'ATA','model': 'ST91000640NS','physical_drive_number': '','predictive_failure_count': '0','status': 'Unconfigured Good','type': 'HDD'},{ 'coerced_size': '952720 MB','controller': 'SLOT-4','drive_firmware': 'CC03','health': 'Good','manufacturer': 'ATA','model': 'ST91000640NS','physical_drive_number': '','predictive_failure_count': '0','status': 'Unconfigured Good'}]```## **3. Parsing a tabular format string.**<a name="tabular"></a>Very common output format is a tabular format with fields seperated bydelimiters (spaces, |, etc).An example here is the "df" command:```Filesystem     1K-blocks     Used Available Use% Mounted on/dev/md1        95926932 27688272  63359156  31% /udev           132014724        4 132014720   1% /devtmpfs           52809712      448  52809264   1% /runnone                5120        0      5120   0% /run/locknone           132024280        0 132024280   0% /run/shmcgroup         132024280        0 132024280   0% /sys/fs/cgroup/dev/md0          943544    28960    865820   4% /boot/dev/md2       806415824 63998212 701447384   9% /var```Here the taubular string parser will parse the output, and return a listof dictionary objects as below:```[ { '1k-blocks': '95926932','Available': '63359156','Filesystem': '/dev/md1','Mounted_on': '/','usage%': '31%','used': '27688272'},{ '1k-blocks': '132014724','Available': '132014720','Filesystem': 'udev','Mounted_on': '/dev','usage%': '1%','used': '4'},{ '1k-blocks': '52809712','Available': '52809264','Filesystem': 'tmpfs','Mounted_on': '/run','usage%': '1%','used': '448'},{ '1k-blocks': '5120','Available': '5120','Filesystem': 'none','Mounted_on': '/run/lock','usage%': '0%','used': '0'},{ '1k-blocks': '132024280','Available': '132024280','Filesystem': 'none','Mounted_on': '/run/shm','usage%': '0%','used': '0'},{ '1k-blocks': '132024280','Available': '132024280','Filesystem': 'cgroup','Mounted_on': '/sys/fs/cgroup','usage%': '0%','used': '0'},{ '1k-blocks': '943544','Available': '865820','Filesystem': '/dev/md0','Mounted_on': '/boot','usage%': '4%','used': '28960'},{ '1k-blocks': '806415824','Available': '701447384','Filesystem': '/dev/md2','Mounted_on': '/var','usage%': '9%','used': '63998212'}]```## **4. Common (re) Pattern Abstractions**<a name="pabstractions"></a>Another use case where parsing strings is needed is in logs and other program output.Some of the common patterns which can abstract a lot of pain in writing regular expression stringsare below:### 1. IP Address:Here is an example of a log from haproxy:```Nov 16 16:35:06 testhost1 haproxy[37217]: 192.16.41.8:45133 [16/Nov/2015:16:32:04.152] mysql mysql/mysql1 1/0/182236 12736 -- 647/261/261/261/0 0/0Nov 16 16:35:06 testhost1 haproxy[37217]: 192.16.41.8:45100 [16/Nov/2015:16:32:03.525] mysql mysql/mysql1 1/0/182932 13077 -- 647/261/261/261/0 0/0Nov 16 16:35:06 testhost1 haproxy[37217]: 192.16.41.8:45131 [16/Nov/2015:16:32:04.105] mysql mysql/mysql1 1/0/182483 12592 -- 647/261/261/261/0 0/0Nov 16 16:35:07 testhost1 haproxy[37217]: 192.16.41.8:45182 [16/Nov/2015:16:32:05.396] mysql mysql/mysql1 1/0/182246 8352 -- 647/261/261/261/0 0/0Nov 16 16:35:07 testhost1 haproxy[37217]: 192.16.41.4:55572 [16/Nov/2015:16:32:07.711] mysql mysql/mysql1 1/0/180119 16915 -- 647/261/261/261/0 0/0```Consider we want to parse the log and get all the ip addresses and ports. Here is how we can do thatusing the rex reformat pattern API.First we define our search pattern:```# Get the ipaddress and port no from the output.pattern = ".* (ip:<ipaddr>):(d:<port>).*"```Invoke the rex "reformat_pattern() API.```rexpat = rex.reformat_pattern(pattern)```Now we can use this as a pattern to search. In this case the re.finditer to iteratethrough all the matches:```for mobj in re.finditer(rexpat, data):print "IP ADDR: %s, PORT: %s" % \(mobj.group(1), mobj.group(2))```This returns the output:```IP ADDR: 192.16.41.8, PORT: 45133IP ADDR: 192.16.41.8, PORT: 45100IP ADDR: 192.16.41.8, PORT: 45131IP ADDR: 192.16.41.8, PORT: 45182IP ADDR: 192.16.41.4, PORT: 55572```### 2. MAC Addresses:### 3. Timestamps: