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,6 +49,7 @@ for variable in config_values:
file_replacement.close()
elif config_values[variable][0] == "exec":
print "Found array. Executing command instead: " + config_values[variable][1]
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:
@ -64,8 +66,19 @@ for variable in config_values:
print "Exiting now."
sys.exit(2)
else:
replacement = config_values[variable]
print variable.upper(),
print "was not found in the file. Skipping command execution."
else:
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()