began implementation of variable argument feature

master
BodgeMaster 2020-04-08 02:58:43 +02:00
parent 2a133985e6
commit 7c32f270ad
1 changed files with 34 additions and 21 deletions

View File

@ -1,14 +1,14 @@
#!/usr/bin/python2 #!/usr/bin/python2
print """ print """
Variable Grabbler - version 3.4 Variable Grabbler - version 4.0_pre1
-------------------------------""" ------------------------------------"""
################################################################ ################################################################
# Changes in this version: # Changes in this version:
#=============================================================== #===============================================================
# * added support for command execution # *"\?" in a variable will now be replaced with a simple ? before
# * some internal technicalities that should not impact features # processing
# * Yes. # *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() config_file.close()
print "Done." print "Done."
print "Working on file: "+sys.argv[1] print "Working on file: "+sys.argv[1]
for variable in config_values: for variable in config_values:
print "> "+variable+" => ", print "> "+variable.upper()+" => ",
file_in = open(sys.argv[1], "r") file_in = open(sys.argv[1], "r")
file_content = file_in.read() file_content = file_in.read()
file_in.close() file_in.close()
@ -48,24 +49,36 @@ for variable in config_values:
file_replacement.close() file_replacement.close()
elif config_values[variable][0] == "exec": elif config_values[variable][0] == "exec":
print "Found array. Executing command instead: " + config_values[variable][1] 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) if variable.upper() in file_content:
stdout, stderr = process.communicate() process = subprocess.Popen(config_values[variable][1], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if process.returncode == 0: stdout, stderr = process.communicate()
print "Process exited normally. Replacing variable with the output from STDOUT..." if process.returncode == 0:
print_err("Subprocess exited normally.\nThe following error messages were produced:") print "Process exited normally. Replacing variable with the output from STDOUT..."
print_err(stderr) print_err("Subprocess exited normally.\nThe following error messages were produced:")
replacement = stdout 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: else:
print "Process exited abnormally. Exiting. No changes will be made." print variable.upper(),
print_err("Subprocess exited abnormally. Exiting.\nThe following output was produced:") print "was not found in the file. Skipping command execution."
print_err(stdout)
print_err("The following error messages were produced:")
print_err(stderr)
print "Exiting now."
sys.exit(2)
else: else:
replacement = config_values[variable] replacement = repr(config_values[variable])
print repr(replacement) print repr(replacement)
# look for variable sections that contain question marks
argument_positions = []
i = 0
while i<len(replacement):
if replacement[i] == '?' and (i==0 or not replacement[i-1]=='\\'):
argument_positions = argument_positions + [ i ]
print "DEBUG: " + argument_positions
replacement = replacement.replace("\\?", "?")
file_out = open(sys.argv[1], "w") file_out = open(sys.argv[1], "w")
file_out.write(file_content.decode('utf-8').replace("%" + variable.upper() + "%", replacement).encode('utf-8')) file_out.write(file_content.decode('utf-8').replace("%" + variable.upper() + "%", replacement).encode('utf-8'))
file_out.close() file_out.close()