<commit_before>from pprint import pprint
from pyroute2.netlink.rtnl.tcmsg import tcmsg
import io
import sys


f = open(sys.argv[1], 'r')
b = io.BytesIO()

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

b.seek(0)
t = tcmsg(b)
t.decode()
pprint(t)
<commit_msg>scripts: make the decoder script more flexible<commit_after>#!/usr/bin/python
'''
Usage::

    ./decoder.py [module] [data_file]

Sample::

    ./decoder.py tcmsg ./sample_packet_01.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

template = 'pyroute2.netlink.rtnl.{mod}.{mod}'
mod = sys.argv[1]
f = open(sys.argv[2], 'r')
b = io.BytesIO()
s = template.format(mod=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
    while True:
        try:
            b.write(chr(int(a[2:4], 16)))
        except:
            break
        a = a[4:]

b.seek(0)
t = met(b)
t.decode()
pprint(t)

{'fraction_non_alphanumeric': 0.1575178997613365, 'fraction_numerical': 0.018297533810660304, 'mean_word_length': 2.7329376854599405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7086081', 'n_tokens_mistral': 481, 'n_tokens_neox': 467, 'n_words': 126}