2020-02-23 21:40:16 +01:00
|
|
|
#!/usr/bin/python2
|
|
|
|
|
|
|
|
print """
|
2020-03-03 14:59:28 +01:00
|
|
|
Variable Grabbler - version 3.0
|
2020-02-23 21:40:16 +01:00
|
|
|
-------------------------------"""
|
|
|
|
|
|
|
|
# definitions
|
2020-02-24 00:56:44 +01:00
|
|
|
import os, json, sys
|
2020-02-23 21:40:16 +01:00
|
|
|
|
2020-02-24 00:56:44 +01:00
|
|
|
if not len(sys.argv)==3:
|
|
|
|
print >> sys.stderr, "Error: Exactly two arguments required: \"python variable_grabbler.py <file to be rewritten> <config file>\""
|
|
|
|
sys.exit(1)
|
2020-02-23 21:40:16 +01:00
|
|
|
|
2020-02-24 00:04:25 +01:00
|
|
|
print "Reading config file...",
|
2020-02-24 01:04:44 +01:00
|
|
|
config_file = open(sys.argv[2],"r")
|
|
|
|
config_values = json.loads(config_file.read().decode("utf-8"))
|
|
|
|
config_file.close()
|
2020-02-24 00:04:25 +01:00
|
|
|
print "Done."
|
2020-02-23 21:40:16 +01:00
|
|
|
|
2020-02-24 00:56:44 +01:00
|
|
|
print "Working on file: "+sys.argv[1]
|
2020-02-24 01:04:44 +01:00
|
|
|
for variable in config_values:
|
2020-03-03 14:59:28 +01:00
|
|
|
print "> "+variable " => ",
|
2020-02-24 00:56:44 +01:00
|
|
|
file_in = open(sys.argv[1], "r")
|
|
|
|
file_content = file_in.read()
|
|
|
|
file_in.close()
|
2020-03-03 14:59:28 +01:00
|
|
|
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 + "\""
|
2020-02-24 01:04:44 +01:00
|
|
|
file_out = open(sys.argv[1], "w")
|
2020-03-03 14:59:28 +01:00
|
|
|
file_out.write(file_content.decode('utf-8').replace("%" + variable + "%", replacement).encode('utf-8'))
|
2020-02-24 00:56:44 +01:00
|
|
|
file_out.close()
|
2020-03-03 14:59:28 +01:00
|
|
|
print "Done."
|