cleanin up coding conventions

This commit is contained in:
q
2018-11-08 18:36:27 +02:00
parent 1a7855242c
commit c4b484a7b9
2 changed files with 111 additions and 74 deletions

View File

@@ -2,50 +2,60 @@
import sys
import scipy.misc
import numpy as n
from argparse import ArgumentParser
from argparse import ArgumentParser
def png2asc(inFile):
im=scipy.misc.imread(inFile)
im = scipy.misc.imread(inFile)
for r in im:
for c in r:
if c>127:
char="X"
char = "X"
if c<128:
char="."
char = "."
if c==0:
char=" "
char = " "
sys.stdout.write(char)
sys.stdout.write("\n")
sys.stdout.write("\n")
def asc2png(inFile,outFile):
def asc2png(inFile, outFile):
reader = open(inFile,'r')
coords=[]
max_c=0
coords = []
max_c = 0
for r,row in enumerate(reader):
for c,col in enumerate(row):
if col not in (' ','\n'):
if col==".":
coords.append((r,c,127))
if col not in (' ', '\n'):
if col == ".":
coords.append((r, c, 127))
else:
coords.append((r,c,255))
max_c=max(max_c,c)
im=n.zeros( (r,max_c-1), dtype=n.uint8 )
coords.append((r, c, 255))
max_c = max(max_c, c)
im=n.zeros( (r, max_c-1), dtype = n.uint8 )
for c in coords:
im[c[0],c[1]]=c[2]
im[c[0], c[1]] = c[2]
scipy.misc.imsave(outFile, im)
if __name__ == "__main__":
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()
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(opts.input,opts.output)
if opts.output == None:
png2asc(opts.input)
else:
asc2png(opts.input, opts.output)

View File

@@ -1,73 +1,100 @@
#!/usr/bin/env python
import sys,re,os
from argparse import ArgumentParser
from argparse import ArgumentParser
from argparse import RawTextHelpFormatter
def setup_options():
parser=ArgumentParser(description="""Template Filler
parser = ArgumentParser(
description="""Template Filler
=== Template example: ===
Hello [[name]]!
[[letter]]
[[letter]]
== Value file/Value pair example: ==
[[name]]=John
[[letter]]=@letter.txt
""",formatter_class=RawTextHelpFormatter)
parser.add_argument("-e",action="store_true",dest="env",default=False,
help="Use the environment to replace ${env} style variables.")
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()
[[letter]]=@letter.txt
""",
formatter_class = RawTextHelpFormatter
)
parser.add_argument(
"-e",
action = "store_true",
dest = "env",
default = False,
help = "Use the environment to replace ${env} style variables."
)
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:
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)
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=[]
pairs = []
for p in args:
tokens = p.split('=', 1)
if len(tokens)!=2:
if len(tokens) != 2:
print("Argument key=value pair '%s' does not parse"%(p,))
sys.exit(1)
sys.exit(1)
pairs.append( (tokens[0], tokens[1].decode('string_escape')) )
return pairs
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:
value=p[1]
if len(value)>0:
if value[0]=="@":
value=open(value[1:],'rt').read()
elif value[0:2]=="\\@":
value=value[1:]
l=l.replace(p[0],value)
if options.env:
var_list=[m.group(0) for m in re.finditer('\${[^ ]+}', l)]
for v in var_list:
l=l.replace(v,os.environ.get(v[:-1][2:],""))
sys.stdout.write(l)
if __name__ == "__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:
value = p[1]
if len(value) > 0:
if value[0] == "@":
value = open(value[1:], 'rt').read()
elif value[0:2] == "\\@":
value = value[1:]
l = l.replace(p[0], value)
if options.env:
var_list = [m.group(0) for m in re.finditer('\${[^ ]+}', l)]
for v in var_list:
l = l.replace(v, os.environ.get(v[:-1][2:], ""))
sys.stdout.write(l)