#!/usr/bin/python
'''
Usage::

    ./decoder.py [module] [data_file]

Sample::

    ./decoder.py pyroute2.netlink.rtnl.tcmsg.tcmsg ./sample_packet_01.data
    ./decoder.py pyroute2.netlink.nl80211.nl80211cmd ./nl80211.data

Module is a name within rtnl hierarchy. File should be a
binary data in the escaped string format (see samples).
'''
import io
import sys
from pprint import pprint
from importlib import import_module

mod = sys.argv[1]
f = open(sys.argv[2], 'r')
b = io.BytesIO()
s = mod.split('.')
package = '.'.join(s[:-1])
module = s[-1]
m = import_module(package)
met = getattr(m, module)


for a in f.readlines():
    if a[0] == '#':
        continue
    if a[0] == '.':
        break
    while True:
        if a[0] == ' ':
            a = a[1:]
            continue
        try:
            b.write(chr(int(a[2:4], 16)))
        except:
            break
        a = a[4:]

b.seek(0)
data = b.getvalue()

offset = 0
inbox = []
while offset < len(data):
    msg = met(data[offset:])
    msg.decode()
    pprint(msg)
    offset += msg['header']['length']

@@ -338,18 +338,8 @@
 '''%0A
-import io%0A
 impo
@@ -345,16 +345,16 @@
 ort sys%0A
+
 from ppr
@@ -406,16 +406,54 @@
 t_module
+%0Afrom pyroute2.common import load_dump
 %0A%0Amod = 
@@ -495,25 +495,8 @@
 r')%0A
-b = io.BytesIO()%0A
 s = 
@@ -610,322 +610,26 @@
 )%0A%0A%0A
-for a in f.readlines():%0A    if a%5B0%5D == '#':%0A        continue%0A    if a%5B0%5D == '.':%0A        break%0A    while True:%0A        if a%5B0%5D == ' ':%0A            a = a%5B1:%5D%0A            continue%0A        try:%0A            b.write(chr(int(a%5B2:4%5D, 16)))%0A        except:%0A            break%0A        a = a%5B4:%5D%0A%0Ab.seek(0)%0Adata = b.getvalue(
+data = load_dump(f
 )%0A%0Ao

use `pyroute2.load_dump()`