Made this program more versatile. Added capabilities: replace with files instead of strings

master
Jan Danielzick 2020-03-03 14:59:28 +01:00
parent 735a713229
commit 0d95b825d8
1 changed files with 17 additions and 4 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/python2
print """
Variable Grabbler - version 2.5
Variable Grabbler - version 3.0
-------------------------------"""
# definitions
@ -19,11 +19,24 @@ print "Done."
print "Working on file: "+sys.argv[1]
for variable in config_values:
print variable,
print "> "+variable " => ",
file_in = open(sys.argv[1], "r")
file_content = file_in.read()
file_in.close()
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_replacement.close()
elif config_values[variable][0] == "exec":
print "Found array. Executing command instead: " + config_values[variable][1]
#Add code to replace variable with command output here
print >> sys.stderr, "Error: This option is not yet supported."
replacement = ""
else:
replacement = config_values[variable]
print "\"" + replacement + "\""
file_out = open(sys.argv[1], "w")
file_out.write(file_content.decode('utf-8').replace("%" + variable + "%", config_values[variable]).encode('utf-8'))
file_out.write(file_content.decode('utf-8').replace("%" + variable + "%", replacement).encode('utf-8'))
file_out.close()
print "\nDone."
print "Done."