From 0f09f9571ac5c34b1c2f0e6234e3c7c42a48c644 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Mon, 24 Feb 2020 00:56:44 +0100 Subject: [PATCH] rewrote to work based on command line arguments [Sorry, @Jakob.] --- variable_grabbler.py | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/variable_grabbler.py b/variable_grabbler.py index 9ccb81e..037c6b6 100644 --- a/variable_grabbler.py +++ b/variable_grabbler.py @@ -1,41 +1,29 @@ #!/usr/bin/python2 print """ -Variable Grabbler - version 1.9 +Variable Grabbler - version 2.0 -------------------------------""" # definitions -import os, json, fnmatch +import os, json, sys -dir = os.path.dirname(os.path.abspath(__file__)) -file_name_pattern = "*.{php,html}" +if not len(sys.argv)==3: + print >> sys.stderr, "Error: Exactly two arguments required: \"python variable_grabbler.py \"" + sys.exit(1) print "Reading config file...", -config = open("variable_grabbler.cfg","r") +config = open(sys.argv[2],"r") parsed = json.loads(config.read().decode("utf-8")) config.close() print "Done." -print "Working on files:", +print "Working on file: "+sys.argv[1] for x in parsed: - print "\n["+x+"]:", - for path, dirs, files in os.walk(dir): - printed_path = False - for file_name in fnmatch.filter(files, file_name_pattern): - file_path = os.path.join(path, file_name) - - if not printed_path: - print "\n"+path+" >", - printed_path = True - print file_name, - - file_in = open(file_path, "r") - file_content = file_in.read() - file_in.close() - - file_content = file_content.decode('utf-8').replace("%" + x + "%", parsed[x]).encode('utf-8') - - file_out = open(file_path, "w") - file_out.write(file_content) - file_out.close() + print x, + file_in = open(sys.argv[1], "r") + file_content = file_in.read() + file_in.close() + file_out = open(file_path, "w") + file_out.write(file_content.decode('utf-8').replace("%" + x + "%", parsed[x]).encode('utf-8')) + file_out.close() print "\nDone."