rewrote to work based on command line arguments [Sorry, @Jakob.]

master
BodgeMaster 2020-02-24 00:56:44 +01:00
parent 474136551f
commit 0f09f9571a
1 changed files with 14 additions and 26 deletions

View File

@ -1,41 +1,29 @@
#!/usr/bin/python2 #!/usr/bin/python2
print """ print """
Variable Grabbler - version 1.9 Variable Grabbler - version 2.0
-------------------------------""" -------------------------------"""
# definitions # definitions
import os, json, fnmatch import os, json, sys
dir = os.path.dirname(os.path.abspath(__file__)) if not len(sys.argv)==3:
file_name_pattern = "*.{php,html}" print >> sys.stderr, "Error: Exactly two arguments required: \"python variable_grabbler.py <file to be rewritten> <config file>\""
sys.exit(1)
print "Reading config file...", print "Reading config file...",
config = open("variable_grabbler.cfg","r") config = open(sys.argv[2],"r")
parsed = json.loads(config.read().decode("utf-8")) parsed = json.loads(config.read().decode("utf-8"))
config.close() config.close()
print "Done." print "Done."
print "Working on files:", print "Working on file: "+sys.argv[1]
for x in parsed: for x in parsed:
print "\n["+x+"]:", print x,
for path, dirs, files in os.walk(dir): file_in = open(sys.argv[1], "r")
printed_path = False file_content = file_in.read()
for file_name in fnmatch.filter(files, file_name_pattern): file_in.close()
file_path = os.path.join(path, file_name) file_out = open(file_path, "w")
file_out.write(file_content.decode('utf-8').replace("%" + x + "%", parsed[x]).encode('utf-8'))
if not printed_path: file_out.close()
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 "\nDone." print "\nDone."