import sys, os, string print("If you run this code make sure that you are inside a test directory") print(" with a folder called 'presets' and some sample '.milk' files.") print("It will create a new file for each '.milk' file") print(" with the new extension set to '.drop'") print("REMOVE THE EXIT FROM THE PROGRAM TO CONFIRM THAT YOU KNOW WHAT YOU ARE DOING") print(" Do NOT run this if you don't know anything about python.") print(" The code only writes to the '.drop' extension, '.milk' is read only.") print(" The program converts all of the subdirectories inside the 'presets' folder.") # exit(0) # <<< REMOVE THIS LINE OF CODE. print("Processing presets folder and subdirectories...") bStripExtraChars = True # Strips "`" and ";" characters from each PROCESS line sp = " " # four spaces for indent proc = "PROCESS" for dirname, dirnames, filenames in os.walk('presets'): for filename in filenames: if (filename.find(".milk") > 0): outfile = os.path.join(dirname, filename.replace(".milk",".drop")) fw = open(outfile, "w") fw.write("preset00:\n") ix = filename.find("-") fw.write(sp + "// Title: " + filename[ix+1:].replace(".milk","").strip() + "\n") if (ix > 0): fw.write(sp + "// Author: " + filename[:ix].strip() + "\n") prevword0 = "" prevdigit = "" fr = open(os.path.join(dirname, filename)) for line in fr: ix = line.find("=") bef = line[:ix].strip() aft = line[ix+1:].strip() if (bef.find("[preset") == 0 or bef == ""): # Skip [preset00] fw.write("\n") # Blank line else: digit = "" word0 = "" word1 = "" words = bef.split("_") for word in words: if (word.isdigit()): digit = word else: if (digit == ""): word0 += word + "_" else: word1 += word + "_" if (word1 != ""): # Look for an exception. ix = len(bef) - 1 ndigit = "" while(bef[ix].isdigit() and ix > 0): ndigit = bef[ix] + ndigit ix -= 1 if (ndigit != ""): nword0 = bef[:ix+1] + "_" if (ndigit == "1" or nword0 + proc == prevword0): digit = ndigit word0 = nword0 word1 = "" if (word1 != ""): word0 = word0 + digit aft = word1.strip("_") + "=" + aft else: word0 += proc # PROCESS if (digit != ""): prevdigit = digit if (bStripExtraChars): # Remove the two special characters. aft = aft.strip("`").strip(";") if (digit != ""): if (prevword0 != word0 or prevdigit != digit): prevword0 = word0 prevdigit = digit fw.write(word0 + ":" + "\n") ### Normal Output: if (prevword0 == ""): fw.write(sp + bef + "=" + aft + "\n") # prefix00: main section else: fw.write(sp + aft + "\n") # all other sections ### Testing Output: #if len(line) > 25: # line = line[:25].strip() #if len(aft) > 25: # aft = aft[:25] #if (prevword0 == ""): # fw.write(" " + bef + "=" + aft + "\t\t" + line.strip()) # prefix00: main section #else: # fw.write(" " + aft + "\t\t" + line.strip()) # all other sections fr.close() fw.close() print("PROCSSING COMPLETE")