From 0d95b825d81c70a85616be629c0dbc4bc162fbf4 Mon Sep 17 00:00:00 2001 From: Jan Danielzick Date: Tue, 3 Mar 2020 14:59:28 +0100 Subject: [PATCH] Made this program more versatile. Added capabilities: replace with files instead of strings --- variable_grabbler.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/variable_grabbler.py b/variable_grabbler.py index abea85a..4d9253d 100644 --- a/variable_grabbler.py +++ b/variable_grabbler.py @@ -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."