Package: templatio
About templatiotemplatio is a Python 3 utility that usesTextFSMandJinja2to
convert text files based on input and output templates.It can be used by developers as a module or by users as a script.It supportsTextFSM syntaxfor input templates andJinja2 syntaxfor output templates.templatio is Free Software, released under theApache License, Version 2.0.templatio was written by Marco Bellaccini (marco.bellaccini(at!)gmail.com) after a fine lunch.QuickStartSuppose that we want to convert a simple formatted text file
to a nice html file.Here is the input text file.infile.txt:Name:JohnSurname:DoeCountry:AtlantisFirst, we need to create an input template usingTextFSM syntax.intempl.txt:ValueName(\S+)ValueSurname(\S+)ValueCountry(\S+)Start^Name:${Name}^Surname:${Surname}^Country:${Country}Then, we need to write an output template usingJinja2 syntax.outtempl.html:<!DOCTYPE html><html><head><title>Hello</title></head><body><p>Hi, I'm {{ Name }} {{ Surname }} and I come from {{ Country }}.</p></body></html>Now, we can convert the file by running templatio as a script:templatio intempl.txt outtempl.html infile.txt outfile.htmlAnd here is what we get.outfile.html:<!DOCTYPE html><html><head><title>Hello</title></head><body><p>Hi, I'm John Doe and I come from Atlantis.</p></body></html>It’s also possible to use templatio as a Python module:importtemplatioinData="""Name: John
Surname: Doe
Country: Atlantis"""inTemplate="""Value Name (\S+)
Value Surname (\S+)
Value Country (\S+)

Start
  ^Name: ${Name}^Surname: ${Surname}^Country: ${Country}"""outTemplate="""<!DOCTYPE html>
<html>
  <head>
    <title>Hello</title>
  </head>
  <body>
    <p>Hi, I'm {{ Name }} {{ Surname }} and I come from {{ Country }}.</p>
  </body>
</html>"""outData=templatio.parseInToOut(inTemplate,outTemplate,inData)Let’s get moreHere is a slightly more complex example, that gives you an idea of how you
can leverageTextFSMandJinja2templates to
perform advanced conversions.Assume that we want to generate a json drive usage report from the output of
thedfcommand on a system with 2 drives.Json objects associated with the drives should have analarmvalue set
totrueif disk usage is over 80%.Here are the input and template files.infile.txt:Filesystem1K-blocksUsedAvailableUse%Mountedonudev2014208020142080%/devtmpfs40510058483992522%/run/dev/sda11644735648930161069914832%//dev/sda2101732489301693442396%/mnt/footmpfs2025484222424180306011%/dev/shmtmpfs5120451161%/run/locktmpfs2025484020254840%/sys/fs/cgrouptmpfs405096564050401%/run/user/1000intempl.txt:ValueDrive1(\S+)ValueDrive1Usage(\d+)ValueDrive2(\S+)ValueDrive2Usage(\d+)# start stateStart# after parsing drive1 data, switch to Drive1parsed state^/dev/${Drive1}.*${Drive1Usage}%->Drive1parsed# drive 1 parsed stateDrive1parsed^/dev/${Drive2}.*${Drive2Usage}%outtempl.json:{%macrocheckusage(usage)-%}{%ifusage>80-%}true{%else%}false{%endif%}{%-endmacro-%}{"drives":{"drive1":{"name":"{{ Drive1 }}","usage":"{{ Drive1Usage }}","alarm":{{checkusage(Drive1Usage|int)}}},"drive2":{"name":"{{ Drive2 }}","usage":"{{ Drive2Usage }}","alarm":{{checkusage(Drive2Usage|int)}}}}}We run templatio (in this example, we use it as a script):templatio intempl.txt outtempl.json infile.txt report.jsonAnd we get this nice result.report.json:{"drives":{"drive1":{"name":"sda1","usage":"32","alarm":false},"drive2":{"name":"sda2","usage":"96","alarm":true}}}Much more!These were just toy examples: bothTextFSMandJinja2have powerful
template syntaxes.After reading their documentation,
you’ll be able to perform really cool conversions!