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
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<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.write(file_content.decode('utf-8').replace("%" + variable.upper() + "%", replacement).encode('utf-8'))
file_out.close()