Hi,
Can anyone recommend to me a commandline templating system?
I want to be able to store information about clients and cases in simple text files and then merge that information into precedent documents. I'll then save and print those documents.
My perfect script would be something like this:
echo "name='Richard Parsons'\naddress='10 Green Hill etc'" > me.txt echo "name='Miles Davies'\naddress='14 Jazz Road etc'" > miles.txt echo "Hey $to.name of $to.address,\nGreat album!\nTake care,\n$from.name of $from.address" > precedent.txt magic-template-script -from me.txt --to miles.txt < precedent.txt > letter.txt
I'm thinking of using Cheetah (http://www.cheetahtemplate.org/), but am not particularly attached to it. I want something that I can run straight from bash if possible, but if scripting is necessary I most prefer python.
Any comments or thoughts?
Thanks, Richard
On 22 July 2010 09:36, Richard Parsons richard.lee.parsons@gmail.com wrote:
Can anyone recommend to me a commandline templating system?
I want to be able to store information about clients and cases in simple text files and then merge that information into precedent documents. I'll then save and print those documents.
echo "name='Richard Parsons'\naddress='10 Green Hill etc'" > me.txt echo "name='Miles Davies'\naddress='14 Jazz Road etc'" > miles.txt echo "Hey $to.name of $to.address,\nGreat album!\nTake care,\n$from.name of $from.address" > precedent.txt magic-template-script -from me.txt --to miles.txt < precedent.txt > letter.txt
Hah. I have in the past had perverted ideas of using the C preprocessor to do this.
It does seem to work, but I've never really done anything major with it.
Srdjan
On 22 Jul 09:36, Richard Parsons wrote:
Hi,
Can anyone recommend to me a commandline templating system?
I want to be able to store information about clients and cases in simple text files and then merge that information into precedent documents. I'll then save and print those documents.
My perfect script would be something like this:
echo "name='Richard Parsons'\naddress='10 Green Hill etc'" > me.txt echo "name='Miles Davies'\naddress='14 Jazz Road etc'" > miles.txt echo "Hey $to.name of $to.address,\nGreat album!\nTake care,\n$from.name of $from.address" > precedent.txt magic-template-script -from me.txt --to miles.txt < precedent.txt > letter.txt
I'm thinking of using Cheetah (http://www.cheetahtemplate.org/), but am not particularly attached to it. I want something that I can run straight from bash if possible, but if scripting is necessary I most prefer python.
Any comments or thoughts?
Exactly what are you expecting as the output? As far as I can gather, you just want a plain text output... Am I right in thinking that you're making a letter? So the output for that would be something like:
----- Begin Output ----- Miles Davies 14 Jazz Road Blah blah blah
Date
Richard Parsons 10 Green Hill Blah blah blah
Hey Miles Davies of 14 Jazz Road Great Album! Take Care Richard Parsons of 10 Green Hill ----- End Output -----
Or similar?
So, *assuming* that your files are of the form that you stipulate (i.e. very much sourceable bash files, with variable='value' style) then something like:
----- Begin Script ----- #!/bin/bash
set -e set -u
trap usage EXIT
function usage() { cat <<EOU >&2 Usage: $0 <from_file> <to_file> > output_file.txt EOU }
function fill_template() { cat <<EOT $to_name $to_address
$(date +"%A %e %B %Y")
Hey $to_name of $to_address, Great Album. Take Care, $from_name of $from_address EOT }
function get_from() { unset name unset address source $from_file from_address="$address" from_name="$name" }
function get_to() { unset name unset address source $to_file to_address="$address" to_name="$name" }
from_file=$1 to_file=$2
to_name='' to_address='' from_name='' from_address=''
get_from get_to fill_template
trap EXIT ----- End Script -----
Not the neatest of scripts, but would do the job... with a bit more work could be made quite flexible too.
Cheers,
On 22/07/10 09:36, Richard Parsons wrote:
I most prefer python.
For fun (and blissfully ignoring the subject line), here's a python variant of Bretts express-data-as-code approach:
mak@yoda:~$ cat >richard.py <<EOM name='Richard Parsons' address='10 Green Hill' EOM
mak@yoda:~$ cat >me.py <<EOM name='mak' EOM
mak@yoda:~$ cat >letter.py <<EOM import sys import me dude = sys.argv[1] dude_module = __import__(dude) print (""" Yo %s! How's things at %s? Love, %s """ % (dude_module.name, dude_module.address, me.name)) EOM
mak@yoda:~$ python letter.py richard
Yo Richard Parsons! How's things at 10 Green Hill? Love, mak
You get some basic data validation: quoting errors will produce a SyntaxError, missing values an AttributeError. There are some limitations: your field names have to be valid identifier names, you don't want to stomp all over built-ins etc. And obviously it's vital that you trust your "data" to not be malicious.
To give the variables in the template some prettier names, you can use python's built-in string Template (see http://docs.python.org/library/string.html#template-strings). It gets a bit trickier because of the identifier limitations, but you could get around that with some key renaming:
from string import Template import sys import me dude = sys.argv[1] dude_module = __import__(dude) myvars = dict([('dude_' + k, dude_module.__dict__[k]) for k in dude_module.__dict__.keys() if not k.startswith('__')]) myvars.update([('me_' + k, me.__dict__[k]) for k in me.__dict__.keys() if not k.startswith('__')]) print Template("""Yo $dude_name! How's things at $dude_address? Cheers $me_name.""").substitute(myvars)
-- Martijn