finished new variable grabbler

master
BodgeMaster 2021-09-08 07:39:38 +02:00
parent 587c73f0fa
commit 9592c279ce
1 changed files with 55 additions and 14 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os, sys, json, traceback import os, sys, json, traceback, subprocess
# Definitions # Definitions
def stdout(string, end='\n', flush=False): def stdout(string, end='\n', flush=False):
@ -9,31 +9,34 @@ def stdout(string, end='\n', flush=False):
def stderr(string, end='\n', flush=False): def stderr(string, end='\n', flush=False):
print(string, end=end, file=sys.stderr, flush=flush) print(string, end=end, file=sys.stderr, flush=flush)
exit_codes = {
"normal exit": 0,
"wrong usage": 1,
"error while processing": 2
}
stderr("Variable Grabbler - version 5.0_pre1\n--------------------------------------------------------------------------------") stderr("Variable Grabbler - version 5.0_pre1\n--------------------------------------------------------------------------------")
################################################################################ ################################################################################
# Chnages in this version: # Chnages in this version:
# - port to Python 3 # - complete rewrite in Python 3
# - output to stdout instead of rewriting the file # - output to stdout instead of rewriting the file
#=============================================================================== #===============================================================================
# Full documentation: tbd # Full documentation: tbd
#=============================================================================== #===============================================================================
# Exit codes:
# 0: normal exit
# 1: wrong usage
# 2: error while processing
# Command line input handling # Command line input handling
usage = "Usage: python3 "+sys.argv[0]+" <macro config file> <file to be processed>" usage = "Usage: python3 "+sys.argv[0]+" <macro config file> <file to be processed>"
if not len(sys.argv)==3: if not len(sys.argv)==3:
stderr("E: Wrong amount of arguments.") stderr("E: Wrong amount of arguments.")
stderr(usage) stderr(usage)
sys.exit(1) sys.exit(exit_codes["wrong usage"])
if sys.argv[1] == sys.argv[2]: if sys.argv[1] == sys.argv[2]:
stderr("E: Both input files are the same.") stderr("E: Both input files are the same.")
stderr(usage) stderr(usage)
sys.exit(1) sys.exit(exit_codes["wrong usage"])
stderr("I: Reading macro definitions... ", end="") # load macros into a dict
stderr("I: Reading macro definitions... ")
macros = {} macros = {}
try: try:
if os.path.isfile(sys.argv[1]): if os.path.isfile(sys.argv[1]):
@ -44,13 +47,47 @@ try:
macros = json.loads(sys.stdin.read()) macros = json.loads(sys.stdin.read())
else: else:
stderr("E: Not a valid file: "+sys.argv[1]) stderr("E: Not a valid file: "+sys.argv[1])
sys.exit(2) sys.exit(exit_codes["error while processing"])
except: except:
stderr("E: An exception occurred while trying to read macro definitions:") stderr("E: An exception occurred while trying to read macro definitions:")
traceback.print_exc() traceback.print_exc()
sys.exit(2) sys.exit(exit_codes["error while processing"])
stderr("Done.")
# process "file" and "exec" macros
stderr("I: Loading templates and processing commands...")
for macro in macros:
if macros[macro][0] == "file":
if os.path.isfile(macros[macro][1]):
try:
stderr("I: Loading template: " + macro)
template = open(macros[macro][1], "r")
macros[macro] = template.read()
template.close()
except:
stderr("E: An exception occurred while trying to read the template:")
traceback.print_exc()
sys.exit(exit_codes["error while processing"])
else:
stderr("E: Macro \"" + macro + "\": template \"" + macros[macro][1] + "\" is not a valid file.")
sys.exit(exit_codes["error while processing"])
elif macros[macro][0] == "exec":
try:
stderr("I: Running command: " + macro)
process = subprocess.Popen(macros[macro][1], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process_stdout, process_stderr = process.communicate()
macros[macro] = process_stdout
if len(process_stderr) > 0:
stderr("Output on stderr:\n"+process_stderr)
if not process.returncode == 0:
stderr("E: Command execution failed with exit code "+str(process.returncode))
sys.exit(exit_codes["error while processing"])
except:
stderr("E: An exception occurred while trying to process the command:")
traceback.print_exc()
sys.exit(exit_codes["error while processing"])
# open file to be processed and iterate over it
stderr("I: Preparing to process input file...")
try: try:
input_file = sys.stdin input_file = sys.stdin
if os.path.isfile(sys.argv[2]): if os.path.isfile(sys.argv[2]):
@ -60,8 +97,12 @@ try:
pass pass
else: else:
stderr("E: Not a valid file: "+sys.argv[2]) stderr("E: Not a valid file: "+sys.argv[2])
sys.exit(2) sys.exit(exit_codes["error while processing"])
# make the magic happen
stderr("I: Processing input file...")
for line in input_file: for line in input_file:
for macro in macros:
line = line.replace("%" + macro + "%", str(macros[macro]))
stdout(line, end="") stdout(line, end="")
# close input file if it's not sys.stdin, assume sys.stdin is being handled for us # close input file if it's not sys.stdin, assume sys.stdin is being handled for us
if not input_file==sys.stdin: if not input_file==sys.stdin:
@ -69,4 +110,4 @@ try:
except: except:
stderr("E: An exception occured while processing " + input_file.name + ":") stderr("E: An exception occured while processing " + input_file.name + ":")
traceback.print_exc() traceback.print_exc()
sys.exit(2) sys.exit(exit_codes["error while processing"])