From 219f0a4da55199ea2a3b44e9884132620ca60857 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Wed, 8 Apr 2020 05:22:50 +0200 Subject: [PATCH] blob --- variable_grabbler.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/variable_grabbler.py b/variable_grabbler.py index f7aaee8..6fa83d5 100644 --- a/variable_grabbler.py +++ b/variable_grabbler.py @@ -1,22 +1,23 @@ #!/usr/bin/python2 print """ -Variable Grabbler - version 4.0_pre4 +Variable Grabbler - version 4.0_pre6 ------------------------------------""" ################################################################ # Changes in this version: #=============================================================== # *"\?" in a variable will now be replaced with a simple ? # before processing, remember to double escape that because -# json (=> \\?) +# json doesn't like \? (=> \\?) # *commands are now not run over and over again if not needed -# +# *files are now not rewritten for each variable individually +# *some technical stuff ################################################################ # Exit codes: #=============================================================== # 0 => normal exit -# 1 => argument error +# 1 => command line argument error # 2 => error while processing # @@ -49,7 +50,7 @@ for variable in config_values: if config_values[variable][0] == "file": print "Found array. Adding file instead: " + config_values[variable][1] file_replacement = open(config_values[variable][1], "r") - replacement = file_replacement.read() + file_content = file_content.decode('utf-8').replace("%" + variable.upper() + "%", file_replacement.read()).encode('utf-8') file_replacement.close() elif config_values[variable][0] == "exec": print "Found array. Executing command instead: " + config_values[variable][1] @@ -60,7 +61,7 @@ for variable in config_values: print "Process exited normally. Replacing variable with the output from STDOUT..." print_err("Subprocess exited normally.\nThe following error messages were produced:") print_err(stderr) - replacement = stdout + file_content = file_content.decode('utf-8').replace("%" + variable.upper() + "%", stdout).encode('utf-8') else: print "Process exited abnormally. Exiting. No changes will be made." print_err("Subprocess exited abnormally. Exiting.\nThe following output was produced:") @@ -81,11 +82,12 @@ for variable in config_values: if replacement[i] == '?' and (i==0 or not replacement[i-1]=='\\'): argument_positions = argument_positions + [ i ] i = i+1 - print "DEBUG: " + str(argument_positions) + #print "DEBUG: " + str(argument_positions) replacement = replacement.replace("\\?", "?") print repr(replacement) # actually replace variables - file_content = file_content.decode('utf-8').replace("%" + variable.upper() + "%", replacement).encode('utf-8') + if argument_positions == []: + file_content = file_content.decode('utf-8').replace("%" + variable.upper() + "%", replacement).encode('utf-8') print "Saving new file..." file_out = open(sys.argv[1], "w")