Template filler
This commit is contained in:
1
bin/fimplate
Symbolic link
1
bin/fimplate
Symbolic link
@@ -0,0 +1 @@
|
||||
../fimplate.py
|
||||
20
chr2pix
20
chr2pix
@@ -2,6 +2,8 @@
|
||||
import sys
|
||||
import scipy.misc
|
||||
import numpy as n
|
||||
from argparse import ArgumentParser
|
||||
|
||||
|
||||
def png2asc(inFile):
|
||||
im=scipy.misc.imread(inFile)
|
||||
@@ -35,15 +37,15 @@ def asc2png(inFile,outFile):
|
||||
|
||||
scipy.misc.imsave(outFile, im)
|
||||
|
||||
if len(sys.argv)==1:
|
||||
print("Arguments:")
|
||||
print(" [png image file] : prints ascii in stdout")
|
||||
print(" [text file] [png image file] : convert characters to pixels in image")
|
||||
sys.exit(0)
|
||||
inFile = sys.argv[1]
|
||||
if inFile.lower().endswith('.png'):
|
||||
png2asc(inFile)
|
||||
|
||||
parser=ArgumentParser(description="Convert image to ascii, or ascii to image. If only input is defined, it is assumed as image (PNG).")
|
||||
parser.add_argument('input', action="store")
|
||||
parser.add_argument('output', action="store", nargs='?')
|
||||
opts=parser.parse_args()
|
||||
|
||||
if opts.output==None:
|
||||
png2asc(opts.input)
|
||||
else:
|
||||
asc2png(inFile,sys.argv[2])
|
||||
asc2png(opts.input,opts.output)
|
||||
|
||||
|
||||
|
||||
69
fimplate.py
Executable file
69
fimplate.py
Executable file
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import sys
|
||||
import os
|
||||
import re
|
||||
from argparse import ArgumentParser
|
||||
from argparse import RawTextHelpFormatter
|
||||
|
||||
def setup_options():
|
||||
parser=ArgumentParser(description="""Template Filler
|
||||
|
||||
=== Template example: ===
|
||||
Hello [[name]]!
|
||||
|
||||
|
||||
== Value file/Value pair example: ==
|
||||
[[name]]=John
|
||||
""",formatter_class=RawTextHelpFormatter)
|
||||
parser.add_argument("-f",action="store",dest="file",
|
||||
help="File name to read keys/values.")
|
||||
parser.add_argument("-p",action="append",dest="values",default=[],
|
||||
help="key=value pairs. This option may be issued several times.")
|
||||
parser.add_argument('template', action="store", nargs='?',
|
||||
help="Template file to be filled. If not defined, stdin used.")
|
||||
options=parser.parse_args()
|
||||
return options
|
||||
|
||||
def parse_file(filename):
|
||||
pairs=[]
|
||||
with open(filename,"r") as reader:
|
||||
for i,l in enumerate(reader):
|
||||
l=l.rstrip("\n\r")
|
||||
if len(l)==0:
|
||||
continue
|
||||
tokens = l.split('=', 1)
|
||||
if len(tokens)!=2:
|
||||
print("File %s:%i key=value pair '%s' does not parse"%(filename,i+1,l,))
|
||||
sys.exit(1)
|
||||
pairs.append( (tokens[0], tokens[1].decode('string_escape')) )
|
||||
return pairs
|
||||
|
||||
def parse_arguments(args):
|
||||
pairs=[]
|
||||
for p in args:
|
||||
tokens = p.split('=', 1)
|
||||
if len(tokens)!=2:
|
||||
print("Argument key=value pair '%s' does not parse"%(p,))
|
||||
sys.exit(1)
|
||||
pairs.append( (tokens[0], tokens[1].decode('string_escape')) )
|
||||
return pairs
|
||||
|
||||
def main():
|
||||
options=setup_options();
|
||||
pairs=[]
|
||||
if options.file!=None:
|
||||
pairs.extend(parse_file(options.file))
|
||||
pairs.extend(parse_arguments(options.values))
|
||||
if options.template==None:
|
||||
in_reader=sys.stdin
|
||||
else:
|
||||
in_reader=open(options.template,'rb')
|
||||
for l in in_reader:
|
||||
for p in pairs:
|
||||
l=l.replace(p[0],p[1])
|
||||
sys.stdout.write(l)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user