Skip to main content

Convert text files based on input and output templates

Project description

https://travis-ci.org/marcobellaccini/templatio.svg?branch=master

About templatio

templatio is a Python 3 utility that uses TextFSM and Jinja2 to 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 supports TextFSM syntax for input templates and Jinja2 syntax for output templates.

templatio is Free Software, released under the Apache License, Version 2.0.

templatio was written by Marco Bellaccini (marco.bellaccini(at!)gmail.com) after a fine lunch.

QuickStart

Suppose that we want to convert a simple formatted text file to a nice html file.

Here is the input text file.

infile.txt:

Name: John
Surname: Doe
Country: Atlantis

First, we need to create an input template using TextFSM syntax.

intempl.txt:

Value Name (\S+)
Value Surname (\S+)
Value Country (\S+)

Start
  ^Name: ${Name}
  ^Surname: ${Surname}
  ^Country: ${Country}

Then, we need to write an output template using Jinja2 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.html

And 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:

import templatio

inData = """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 more

Here is a slightly more complex example, that gives you an idea of how you can leverage TextFSM and Jinja2 templates to perform advanced conversions.

Assume that we want to generate a json drive usage report from the output of the df command on a system with 2 drives.

Json objects associated with the drives should have an alarm value set to true if disk usage is over 80%.

Here are the input and template files.

infile.txt:

Filesystem     1K-blocks    Used Available Use% Mounted on
udev             2014208       0   2014208   0% /dev
tmpfs             405100    5848    399252   2% /run
/dev/sda1       16447356 4893016  10699148  32% /
/dev/sda2        1017324  893016    934423  96% /mnt/foo
tmpfs            2025484  222424   1803060  11% /dev/shm
tmpfs               5120       4      5116   1% /run/lock
tmpfs            2025484       0   2025484   0% /sys/fs/cgroup
tmpfs             405096      56    405040   1% /run/user/1000

intempl.txt:

Value Drive1 (\S+)
Value Drive1Usage (\d+)
Value Drive2 (\S+)
Value Drive2Usage (\d+)

# start state
Start
  # after parsing drive1 data, switch to Drive1parsed state
  ^/dev/${Drive1} .* ${Drive1Usage}% -> Drive1parsed

# drive 1 parsed state
Drive1parsed
  ^/dev/${Drive2} .* ${Drive2Usage}%

outtempl.json:

{% macro checkusage(usage) -%}
    {% if usage > 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.json

And 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: both TextFSM and Jinja2 have powerful template syntaxes.

After reading their documentation, you’ll be able to perform really cool conversions!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

templatio-0.1.1.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

templatio-0.1.1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file templatio-0.1.1.tar.gz.

File metadata

  • Download URL: templatio-0.1.1.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for templatio-0.1.1.tar.gz
Algorithm Hash digest
SHA256 15125e2a2aebf477fa52c70d2fa17b985fc92e40265a70d445761a1b2f8e7929
MD5 6944266b2416e058d112b8c56cfc99f0
BLAKE2b-256 f71b82a129b731a3ff9e68bf93709e5a95763ea192b1bb9e10ef73dbda3c8961

See more details on using hashes here.

File details

Details for the file templatio-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for templatio-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e63502bfa1c0ace362ab7e0dfc599fe0fa91fbd4567e6b3c679160ec9444f491
MD5 8ece6414085354e5245f9e4b019ef99c
BLAKE2b-256 aa4767061eb79a70b42b6d143f3a4a381cfee0876fa3b8b70fa361e8f07cfbf4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page