Package: conftl
Configuration Templating LanguageSimple to learn but yet powerful language for templating your configuration files. It is a 'slang' of theweb2py's templating language, written from scratch and optimized for textual non-html data.FeaturesSimple to learn - a person who has some idea of the Python syntax could dive into conftl for 15 min.Powerful - Python code in templating.Command line tool for rendering.Different methods for trigerring rendering from Python code.Suitable for system administration, devops and similar roles.Performance - minimal code base optimized for performance.Platform independent - tested under Linux and Windows, should work on other Unix platforms as well, Python 2.7 compatible, Python 3.x compatible.Getting StartedInstall conftl using pip:pip install conftlAlternatively download the source code:git clone https://github.com/ttt-fifo/conftl
cd conftl
python setup.py installHello world from the command line:$render-c"name='John Smith'"Hello,{{=name}}Hello,JohnSmithNOTE: WriteHello, {{=name}}on stdin, followed by Enter, Ctr+DHello world from the Python REPL:>>>>>>fromconftlimportrender>>>>>>render(content='Hello, {{=name}}',context=dict(name='John Smith'))'Hello, John Smith'>>>PrerequisitesLinux or other Unix distribution or Windows.Python 2.7 or Python 3.xPlease place anissuein case the current implementation is not working with your platform and I will try to help.Python Modules: futureTemplating KickstartClear text from the template is printed to the output as islorem ipsum dolor sim amet
text clear lorem ipsumwill go exactly the same into the outputlorem ipsum dolor sim amet
text clear lorem ipsumPython code in template should be written using tags{{ ...code... }}For example if you would like to assign a value3toi, you can do it using the following syntax:{{i = 3}}You could also write multiline Python code as well - like the following example:{{
import sys

def one():
    return 1

i = 3
}}Printing a variable value to the outputis done by tagging it and placing = sign in front of the variable like this{{=myvar}}For example ifihas the value of3and you put in template:{{=i}}you will receive in the output:3Combining a Python code block with clear text and variable outputs- you should not indent the code block as you normally do with Python, but you should determine it with{{pass}}special keyword instead.Whenever you write a code block into the original Python interpreter you indent the code. Lets take the following example of original Python code block:foriinrange(0,2):print('X',i)The equivalent of the above code would be:{{for i in range(0, 2):}}
X {{=i}}
{{pass}}You are able to pass values to template variables from outside of the template- there are multiple methods to give 'context' to the template, e.g. assigning variable values outside of the template. Look the follow up sections.Advanced templating topicscould be found atTEMPLATING.mdExamplesTake a look at theexamples folderfrom the project repository.Command Line Tool for Rendering (render)The render command line tool works as follows:render -i templatename.tmpl -o filename.confwill take the template from filetemplatename.tmpland write the output tofilename.confWARNING: filename.conf will be overwriten!!!In case input template is not given by -i, you would be expected to place template code on stdin.NOTE: For Linux and other Unix systems write template code and finish it with Ctr + DNOTE: For Windows finish template code with with Ctr + Z and then hit ENTERIn case the output filename (-o) is not given, the output will be written to stdout.Giving context variables on the command lineYou want to giveivalue of4and use it in your template. Use -c flag:render -i templatename.tmpl -o filename.conf -c i=4For assigning values to multiple variables, just repeat -c flag multiple times:render -i templatename.tmpl -o filename.conf -c i=4 -c j=8 -c x=2For assigning complex variable datatypes, wrap assignment in double quote like this:render -i templatename.tmpl -o filename.conf -c "mydict={'a': 1, 'b': 'string'}"Context from json fileThe json file format should be similar to:{"myvar":4,"otherthing":[1,3,5],"stringsomething":"hello world"}You can invoke render by giving the -j option like this:render -i mytemplate.tmpl -j mycontext.ctxNOTE: the command line variables have precedence over json file, e.g. if you assign i=2 in json file and i=3 on command line, the final value of i will be i=3.Environment in context for convenienceFor convenience the ENV dictionary is automatically included in the context and it contains the OS environment variables. The following example prints them on the screen:render
{{for e in ENV:}}
{{=e}} : {{=ENV[e]}}
{{pass}}
..................................
... environment will come here ...
..................................NOTE: the ENV is included automatically in context only with the command line tool, rendering from Python (the next section) does not have ENV automatically in context.How to use ENV in templates?Many devops / sysadmin systems pass data to their underlying scripts via environment variables. As an example the following shell commands:$ export SYSTEM=production
$
$ render
{{if ENV['SYSTEM'] == 'production':}}
Listen 80
{{elif ENV['SYSTEM'] == 'ci_cd':}}
Listen 8080
{{elif ENV['SYSTEM'] == 'devel':}}
Listen 8081
{{else:}}
{{raise RuntimeError('wrong SYSTEM')}}
{{pass}}(Ctr+D at the end)will give you the output based on the SYSTEM environment variable:Listen 80Command line tool helpSeerender -hRendering Template from PythonThere are three interfaces for rendering a template from Python: the functionrender(...), the classRenderand the decorator@template(...). Please see the explanation below:render(...) functionConsider the following example:>>>>>>fromconftlimportrender>>>render(content='{{=i}}',context=dict(i=8))'8'>>>As you can see, you can give the context= value, which is a dict, containing your variable data.The signature of the function follows:render(infile=None,outfile=None,context=None,content=None,delimiters=None)You can use the function by giving infile= as argument (this is the template file). If not given, you should give the content= value - this would be a string with the template content.Output file could be given by outfile= argument. If given, the output will be written to this file. On outfile= absence, the output is returned as string.In case you need to use other delimiters than the default{{ }}, you can change the delimiters like this:>>>>>>render(content='[[=i]]',context=dict(i=7),delimiters='[[ ]]')'7'>>>template decoratorDefine a function which returns the context as a dict. Decorate your function with template decorator:fromconftlimporttemplate# Define your function, which should output a dict# with the template context and decorate it with# template decorator@template(infile='mytemplate.tmpl',outfile='myconf.conf')deftemplate_myconf(*arg,**kwarg):# ...here your complex computations...i=....j=....x='.....'returndict(i=i,j=j,templ_var=x)if__name__=='__main__':# Here invoke your function and it should create# the needed myconf.conftemplate_myconf(...someargs...)The possible arguments for template decorator are@template(infile=None,outfile=None,content=None,delimiters=None)You must give eihter infile= or content= as input. You can omit outfile= and in this case the decorated function will return the output as a string. Changing delimiters= is also possible. The function, decorated with template(...) must return dict, otherwise exception is raised.This type of context computation is well know by the web2py users, because this is the layout of the web2py controller.Render objectAn object from Render class could be used in a long running processes. Load the object in memory once and use it multiple times for templating multiple files:fromconftlimportRenderrndr=Render()# ... use it multiple times like thisrndr.instream=open('filename.tmpl','r')rndr.outstream=open('otherfile.conf','w')rndr.context=dict(i=...,j=...,somevar='...')rndr()rndr.instream.close()rndr.outstream.close()# ....Theinstreamandoutstreamshould be file handles or StringIO objects.Known LimitationsArbitrary Python code is possible to be executed by the current templating language. I would advice against giving opportunity to the end-users to write template code, unless you know what you are doing. Multiple attack vectors could be used by a malicious end-user who has the possibility to execute arbitrary Python code.In case you want to template a HTML output, you would be better off using the web2py's templating language (calledyatl). Yatl has XML escaping switched on by default and also multiple HTML helper functions.ContributingTesting implementation on different platforms.Do not hesitate to fork me on github.Placeissueif you spot issues with this code.VersioningSee thetagson this repository.AuthorsTodor Todorov-ttt-fifoLicenseBSD + other copyright creditsSeeLICENSEfor details.AcknowledgmentsThanks toMassimo Di Pierroand theweb2pyteam for the inspiration.Logo:server,icons,arrowSee AlsoDifferences between conftl and yatl from the documentdifferences_yatl.txtweb2py on githubweb2py templating languageyatlAnother implementation of the same templating language may be found at theweppyproject.