From 7c32f270ad28a75b1c74b73b0b7d4cdb3e169500 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Wed, 8 Apr 2020 02:58:43 +0200 Subject: [PATCH] began implementation of variable argument feature --- variable_grabbler.py | 55 +++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/variable_grabbler.py b/variable_grabbler.py index 7c9592c..f856b81 100644 --- a/variable_grabbler.py +++ b/variable_grabbler.py @@ -1,14 +1,14 @@ #!/usr/bin/python2 print """ -Variable Grabbler - version 3.4 --------------------------------""" +Variable Grabbler - version 4.0_pre1 +------------------------------------""" ################################################################ # Changes in this version: #=============================================================== -# * added support for command execution -# * some internal technicalities that should not impact features -# * Yes. +# *"\?" in a variable will now be replaced with a simple ? before +# processing +# *commands are now not run over and over again fi not needed # ################################################################ @@ -35,9 +35,10 @@ config_values = json.loads(config_file.read().decode("utf-8")) config_file.close() print "Done." + print "Working on file: "+sys.argv[1] for variable in config_values: - print "> "+variable+" => ", + print "> "+variable.upper()+" => ", file_in = open(sys.argv[1], "r") file_content = file_in.read() file_in.close() @@ -48,24 +49,36 @@ for variable in config_values: file_replacement.close() elif config_values[variable][0] == "exec": print "Found array. Executing command instead: " + config_values[variable][1] - process = subprocess.Popen(config_values[variable][1], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = process.communicate() - if process.returncode == 0: - 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 + if variable.upper() in file_content: + process = subprocess.Popen(config_values[variable][1], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = process.communicate() + if process.returncode == 0: + 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 + else: + print "Process exited abnormally. Exiting. No changes will be made." + print_err("Subprocess exited abnormally. Exiting.\nThe following output was produced:") + print_err(stdout) + print_err("The following error messages were produced:") + print_err(stderr) + print "Exiting now." + sys.exit(2) else: - print "Process exited abnormally. Exiting. No changes will be made." - print_err("Subprocess exited abnormally. Exiting.\nThe following output was produced:") - print_err(stdout) - print_err("The following error messages were produced:") - print_err(stderr) - print "Exiting now." - sys.exit(2) + print variable.upper(), + print "was not found in the file. Skipping command execution." else: - replacement = config_values[variable] + replacement = repr(config_values[variable]) print repr(replacement) + # look for variable sections that contain question marks + argument_positions = [] + i = 0 + while i