Package: ttp
Template Text ParserTTP is a Python library for semi-structured text parsing using templates.Why?To save ones time on transforming raw text into structured data and beyond.How?Regexes, regexes everywhere... but, dynamically formed out of TTP templates with added capabilities to simplify the  process of getting desired outcome.What?In essence TTP can help to:Prepare, sort and load text data for parsingParse text using regexes dynamically derived out of templatesProcess matches on the fly using broad set of built-in or custom functionsCombine match results in a structure with arbitrary hierarchyTransform results in desired format to ease consumption by humans or machinesReturn results to various destinations for storage or further processingReferencedocumentationfor more information.TTPNetworktocode Slack channelCollection ofTTP TemplatesExample - as simple as it can beSimple interfaces configuration parsing exampleCodefromttpimportttpimportpprintdata="""interface Loopback0description Router-id-loopbackip address 192.168.0.113/24!interface Vlan778description CPE_Acces_Vlanip address 2002::fd37/124ip vrf CPE1!"""template="""interface {{ interface }}ip address {{ ip }}/{{ mask }}description {{ description }}ip vrf {{ vrf }}"""parser=ttp(data,template)parser.parse()pprint.pprint(parser.result(),width=100)# prints:# [[[{'description': 'Router-id-loopback',#     'interface': 'Loopback0',#     'ip': '192.168.0.113',#     'mask': '24'},#    {'description': 'CPE_Acces_Vlan',#     'interface': 'Vlan778',#     'ip': '2002::fd37',#     'mask': '124',#     'vrf': 'CPE1'}]]]Example - a bit more complicatedFor this example lets say we want to parse BGP peerings output, but combine state with configuration data, at the end we want to get pretty looking text table printed to screen.Codetemplate="""<doc>This template first parses "show bgp vrf CUST-1 vpnv4 unicast summary" commandsoutput, forming results for "bgp_state" dictionary, where peer ip is a key.Following that, "show run | section bgp" output parsed by group "bgp_cfg". Thatgroup uses nested groups to form results structure, including absolute path"/bgp_peers*" with path formatter to produce a list of peers under "bgp_peers"path.For each peer "hostname" and local bgp "local_asn" added using previous matches.Additionally, group lookup function used to lookup peer state from "bgp_state"group results, adding found data to peer results.Finally, "bgp_peers" section of results passed via "tabulate_outputter" tofrom and print this table to terminal:hostname           local_asn    vrf_name    peer_ip    peer_asn    uptime    state    description    afi    rpl_in           rpl_out-----------------  -----------  ----------  ---------  ----------  --------  -------  -------------  -----  ---------------  ---------------ucs-core-switch-1  65100        CUST-1      192.0.2.1  65101       00:12:33  300      peer-1         ipv4   RPL-1-IMPORT-v4  RPL-1-EXPORT-V4ucs-core-switch-1  65100        CUST-1      192.0.2.2  65102       03:55:01  idle     peer-2         ipv4   RPL-2-IMPORT-V6  RPL-2-EXPORT-V6Run this script with "python filename.py"</doc><vars>hostname="gethostname"chain_1 = ["set('vrf_name')","lookup('peer_ip', group='bgp_state', update=True)"]</vars><group name="bgp_state.{{ peer }}" input="bgp_state">{{ peer }}  4 65101      20      21       43    0    0 {{ uptime }} {{ state }}</group><group name="bgp_cfg" input="bgp_config">router bgp {{ asn | record(asn) }}<group name="vrfs.{{ vrf_name }}" record="vrf_name">vrf {{ vrf_name }}<group name="/bgp_peers*" chain="chain_1">neighbor {{ peer_ip }}{{ local_asn | set(asn) }}{{ hostname | set(hostname) }}remote-as {{ peer_asn }}description {{ description }}address-family {{ afi }} unicastroute-map {{ rpl_in }} inroute-map {{ rpl_out }} out</group></group></group><outputname="tabulate_outputter"format="tabulate"path="bgp_peers"returner="terminal"headers="hostname, local_asn, vrf_name, peer_ip, peer_asn, uptime, state, description, afi, rpl_in, rpl_out"/>"""data_bgp_state="""ucs-core-switch-1#show bgp vrf CUST-1 vpnv4 unicast summaryNeighbor   V    AS MsgRcvd MsgSent   TblVer  InQ OutQ Up/Down  State/PfxRcd192.0.2.1  4 65101      32      54       42    0    0 00:12:33       300192.0.2.2  4 65101      11      45       99    0    0 03:55:01       idle"""data_bgp_config="""ucs-core-switch-1#show run | section bgprouter bgp 65100vrf CUST-1neighbor 192.0.2.1remote-as 65101description peer-1address-family ipv4 unicastroute-map RPL-1-IMPORT-v4 inroute-map RPL-1-EXPORT-V4 outneighbor 192.0.2.2remote-as 65102description peer-2address-family ipv4 unicastroute-map RPL-2-IMPORT-V6 inroute-map RPL-2-EXPORT-V6 out"""fromttpimportttpparser=ttp()parser.add_template(template)parser.add_input(data=data_bgp_state,input_name="bgp_state")parser.add_input(data=data_bgp_config,input_name="bgp_config")parser.parse()ContributionsFeel free to submit an issue, report a bug or ask a question, feature requests are welcomed. OrbuyAuthor a coffeeAdditional resourcesList of additional resources:Sandbox to test TTP templates -http://textfsm.nornir.tech/bytbotnzVideos on TTP -https://pynet.twb-tech.com/videos/ttp/ttp.htmlbyKirk Byers