Let the logs flow
Project description
aina
Aina is a general-purpose stream processing framework. It includes a simple but powerful templating system which powers a versitle command line utility.
NOTE: This is new code. Master is in flux and docs are lacking, but it is in a point where it could be useful to someone. If it is useful to you, help us get to 1.0.0. You can start by reading the contributing guide at https://github.com/ilovetux/aina/CONTRIBUTING.rst.
Free software: GNU General Public License v3
Documentation: https://aina.readthedocs.io.
Features
Simple, Powerful templating system
Command line utilities
All the power of Python
No hacks or magic
Approachable source code
Tested
TODO: Web UI
TODO: Many default use cases covered
TODO: –no-overwrite option
Installing
You can install the latest stable version with the following command:
$ pip install aina
Alternately, to clone the latest development version:
$ git clone https://github.com/ilovetux/aina $ cd aina # Optional $ python setup.py test $ pip install .
Concepts
The built-in templating engine is very simple, it consists of a namespace and a template. The template is rendered within the context of the namespace.
Rendering involves two stages:
scanning the template for strings matching the pattern {%<Source>%} where <Source> is Python source code which is executed (exec) within the context of the namespace. During execution, stdout is captured. After execution, {%<Source>%} is replaced with a string containing the output.
scanning the remaining output for strings matching the pattern {{<Expression>}} where <Expression> is a Python expression which is replaced with the value to which it evaluates (eval)
As an example, let’s look at the following template:
{% me = "iLoveTux" name = "Bill" age = 35 %} hello {{name}}: I heard that you just turned {{str(age)}}. Congratulations! Sincerely: {% print(me) %}
If this were rendered, the output would be as follows:
Hello Bill, I heard that you just turned 35. Congratulations! Sincerely: iLoveTux
This concept is applied to a variety of use cases and embodied in the form of command line utilities which cover a number of common use cases.
Usage
Aina can be used directly from within Python, like so:
from aina import render namespace = {"foo": "bar"} template = "The value of foo is {{foo}}" result = render(template, namespace)
This usage has first-class support, but a much handier solution is to use the provided CLI.
The command line utility, aina, can be run in two modes:
Streaming mode: Data is streamed through and used to populate templates
Document mode: Render files src and write the results to dst
Streaming mode
Streaming mode runs in the following manner:
Accept a list of filenames (wildcards are accepted), which defaults to stdin
At this point any expressions passed to –begins are executed
The files specified are processed in order
Any expressions passed to –begin-files are executed
The data from the current file is read line-by-line
Any statements passed to –tests are evaluated
Iff all tests pass, the following process is performed.
Any expressions passed to –begin-lines are executed
Any templates are rendered through the python logging system
Any expressions passed to –end-lines are executed
Any expressions passed to –end-files are executed
Any expressions passed to –ends are executed
Below are a few examples. See the documentation for more details:
# Like grep $ aina stream --test "'error' in line.lower()" --template "{{line}}" *.log # Like wc -l $ aina stream --end-files "print(fnr, filename)" *.log # Like wc -wl $ aina stream --begins "words=0" --begin-lines "words += nf" --end-files "print(words, fnr, filename)" # Find all numbers "\d+" for each line $ aina stream --begins "import re" --begin-lines "print(re.findall(r'\d+', line))" *.log # Run an XPath $ aina stream --begins "from lxml import etree" --begin-lines "tree = etree.fromstring(line)" --templates "{{"\n".join(tree.xpath("./*"))}}"
Please see the documentation for more as well as trying:
$ aina stream --help
Important Note:
If anything passed to any of the hooks is determined to exist by os.path.exists then it will be read and executed as if that text was passed in on the CLI. This is useful for quickly solving character escaping issues.
Document mode
Document mode renders one or more files and/or directories src to another location dst. It is used like this:
$ aina doc <src> <dst>
There are options to control behavior, but the gist of it is:
if src is a file
if dst is a filename, src is rendered and written to dst
if dst is a directory, src is rendered and written to a file in dst with the same basename as src
if src is a directory
dst must be a directory and every file in src is rendered into a file in dst with the same basename as the file from src
If –recursive is specified, the subdirectories will be reproduced in dst
Some important notes:
If –interval is passed an integer value, the program will sleep for that many seconds and check for changes to your templates (based on the file’s mtime) in which case they will be re-rendered
Use Cases
Streaming mode is great for processing incoming log files with tail –follow=name or for ad-hoc analysis of text files.
Document mode is incredibly useful for a powerful configuration templating system. The –interval option is incredibly useful as it will only re-render on a file change, so is great for developing your templates as you can view the results in near-real-time.
Document mode is also useful for near-real-time rendering of static web resources such as charts, tables, dashboards and more.
If you find any more use cases, please open an issue or pull request to add it here and in the wiki
Credits
Author: iLoveTux This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
History
0.1.1 (2018-07-05)
Improved test-suite
Execution blocks are now replaced with whatever is sent to stdout
aina doc now respects the –interval argument
aina doc –interval is fixed and now only re-renders templates which have changed
aina doc with –recursive now processes files in a top-down manner
Lots of bug fixes
Improved documentation
values of expressions are now automatically coerced into strings
Drop support for Python 3.4 and add support for Python 3.7
Improved logging
0.1.0 (2018-06-20)
First release on PyPI.
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
Built Distribution
File details
Details for the file aina-0.1.3.tar.gz
.
File metadata
- Download URL: aina-0.1.3.tar.gz
- Upload date:
- Size: 19.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4767512532a6bcccb519eefe4b3b7d505d06c47aa0b369a212d4a28a2fe90d4c |
|
MD5 | 05fd18c142e84feed8bb8ee051054ade |
|
BLAKE2b-256 | cba88f8f969361fb96d422d1f0f450388dc62fe538616dd5db1f193b33907c6b |
File details
Details for the file aina-0.1.3-py2.py3-none-any.whl
.
File metadata
- Download URL: aina-0.1.3-py2.py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3501204438fd0738e357aff99158a4244a836f3b9497135860fdc4a1e8d3b276 |
|
MD5 | b149822945973579c9bd81a7e0ec4e37 |
|
BLAKE2b-256 | 982052cefc2082924bc5980eceb8b02aa579aabc796142d9cb389f20dcc10f02 |