Advertisement
Guest User

XML Modifier.py

a guest
Jun 26th, 2015
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.46 KB | None | 0 0
  1. #XML modifier based on http://cps05box.wikia.com/wiki/Cp-s05box_Wiki:SWF_Tricks/Tutorials/Scaling_up_SWF_files#Quick_Drop by Penguin-Pal (JavaScript code: http://cps05box.wikia.com/wiki/MediaWiki:Common.js/swfxml.js)
  2. #Coded in Python 3 and designed for use with SWiX
  3. #Author: Hey.youcp - Date: June 26, 2015 - Version: Not final
  4. anotherLoop = True
  5. while anotherLoop == True:
  6.     #Open file
  7.     fileLoop = True
  8.     while fileLoop == True:
  9.         fileName = input("Enter name of XML file (without file extension): ")
  10.         fileFound = False
  11.         try:
  12.             inputFile = open(fileName + ".xml",'r')
  13.             fileFound = True
  14.             print("Loading...")
  15.             xml = []
  16.             for line in inputFile:
  17.                 xml.append(line)
  18.             inputFile.close()
  19.             fileLoop = False
  20.         except FileNotFoundError:
  21.             print("File not found. Check that the file name is correct and that the XML file is in the same directory as the Python file.")
  22.             continueLoop = True
  23.             while continueLoop == True:
  24.                 continueChoice = input("\tContinue? (y/n): ")
  25.                 if continueChoice == "y":
  26.                     continueLoop = False
  27.                 elif continueChoice == "n":
  28.                     continueLoop = False
  29.                     fileLoop = False
  30.                     anotherLoop = False
  31.                 else:
  32.                     print("\t\tInvalid input.")
  33.     if fileFound == True:
  34.         print("File loaded.\n")
  35.         #Get modifications from user
  36.         moveChoiceLoop = True
  37.         while moveChoiceLoop == True:
  38.             moveChoice = input("Move? (y/n): ")
  39.             if moveChoice == "y":
  40.                 moveRightLoop = True
  41.                 while moveRightLoop == True:
  42.                     moveRight = input("\tMove right by: ")
  43.                     if moveRight.replace("+","").replace("-","").replace(".","").isdigit():
  44.                         moveRight = eval(moveRight)
  45.                         moveRightLoop = False
  46.                     else:
  47.                         print("\t\tInvalid input.")
  48.                 moveDownLoop = True
  49.                 while moveDownLoop == True:
  50.                     moveDown = input("\tMove down by: ")
  51.                     if moveDown.replace("+","").replace("-","").replace(".","").isdigit():
  52.                         moveDown = eval(moveDown)
  53.                         moveDownLoop = False
  54.                         moveChoiceLoop = False
  55.                     else:
  56.                         print("\t\tInvalid input.")
  57.             elif moveChoice == "n":
  58.                 moveChoiceLoop = False
  59.             else:
  60.                 print("\tInvalid input.")
  61.         rescaleChoiceLoop = True
  62.         while rescaleChoiceLoop == True:
  63.             rescaleChoice = input("Rescale? (y/n): ")
  64.             if rescaleChoice == "y":
  65.                 rescaleLoop = True
  66.                 while rescaleLoop == True:
  67.                     rescale = input("\tMultiply shape dimensions by: ")
  68.                     if rescale.replace("+","").replace("-","").replace(".","").isdigit():
  69.                         rescale = eval(rescale)
  70.                         rescaleLoop = False
  71.                         rescaleChoiceLoop = False
  72.                     else:
  73.                         print("\t\tInvalid input.")
  74.             elif rescaleChoice == "n":
  75.                 rescale = 1
  76.                 rescaleChoiceLoop = False
  77.             else:
  78.                 print("\tInvalid input.")
  79.         windowSizeChoiceLoop = True
  80.         while windowSizeChoiceLoop == True:
  81.             windowSizeChoice = input("Change window size? (y/n): ")
  82.             if windowSizeChoice == "y":
  83.                 windowSizeLoop = True
  84.                 while windowSizeLoop == True:
  85.                     windowSize = input("\tWindow size: ")
  86.                     if windowSize.replace("+","").replace("-","").replace(".","").isdigit():
  87.                         windowSizeLoop = False
  88.                         windowSizeChoiceLoop = False
  89.                     else:
  90.                         print("\t\tInvalid input.")
  91.             elif windowSizeChoice == "n":
  92.                 windowSizeChoiceLoop = False
  93.             else:
  94.                 print("\tInvalid input.")
  95.         #Apply modifications
  96.         print("\nProcessing...",end="")
  97.         outputFile = open("output.xml",'w')
  98.         modCounter = 0
  99.         for line in range(0,len(xml)):
  100.             if windowSizeChoice == "y":
  101.                 if "<Movie SwfVersion=" in xml[line]:
  102.                     splitLine = xml[line].split()
  103.                     splitLine[2] = "Width=\"" + windowSize + "\""
  104.                     splitLine[3] = "Height=\"" + windowSize + "\""
  105.                     xml[line] = splitLine[0] + " " + splitLine[1] + " " + splitLine[2] + " " + splitLine[3] + " " + splitLine[4] + " " + splitLine[5] + "\n"
  106.                     modCounter += 1
  107.             if moveChoice == "y":
  108.                 if "<Translate" in xml[line] and "TranslateX=\"0\"" not in xml[line] and "TranslateY=\"0\"" not in xml[line]:
  109.                     splitLine = xml[line].split()
  110.                     moveX = eval(splitLine[1].replace("TranslateX=","").replace("\"",""))
  111.                     moveY = eval(splitLine[2].replace("TranslateY=","").replace("\"",""))
  112.                     moveX = (moveX + moveRight) * rescale
  113.                     moveY = (moveY + moveDown) * rescale
  114.                     splitLine[1] = "TranslateX=\"" + str(moveX) + "\""
  115.                     splitLine[2] = "TranslateY=\"" + str(moveY) + "\""
  116.                     xml[line] = "\t\t\t" + splitLine[0] + " " + splitLine[1] + " " + splitLine[2] + " " + splitLine[3]
  117.                     modCounter += 1
  118.             if rescaleChoice == "y":
  119.                 if "<Translate" in xml[line] and "TranslateX=\"0\"" not in xml[line] and "TranslateY=\"0\"" not in xml[line]:
  120.                     if "<Scale" in xml[line+1]:
  121.                         splitLine = xml[line+1].split()
  122.                         rescaleX = eval(splitLine[1].replace("ScaleX=","").replace("\"",""))
  123.                         rescaleY = eval(splitLine[2].replace("ScaleY=","").replace("\"",""))
  124.                         rescaleX *= rescale
  125.                         rescaleY *= rescale
  126.                         splitLine[1] = "ScaleX=\"" + str(rescaleX) + "\""
  127.                         splitLine[2] = "ScaleY=\"" + str(rescaleY) + "\""
  128.                         xml[line+1] = "\n\t\t\t" + splitLine[0] + " " + splitLine[1] + " " + splitLine[2] + " " + splitLine[3] + "\n"
  129.                     else:
  130.                         xml[line] += "\n\t\t\t" + splitLine[0] + " " + splitLine[1] + " " + splitLine[2] + " " + splitLine[3] + "\n"
  131.                     modCounter += 1
  132.             outputFile.write(xml[line])
  133.         #Done
  134.         outputFile.close()
  135.         print("\n" + str(modCounter) + " line(s) modified out of " + str(len(xml)) + " line(s) in total.")
  136.         print("Look for a file named \"output.xml\" in your directory.\n")
  137.         anotherChoiceLoop = True
  138.         while anotherChoiceLoop == True:
  139.             anotherChoice = input("Modify another XML file? (y/n): ")
  140.             if anotherChoice == "y":
  141.                 anotherChoiceLoop = False
  142.                 print("Warning: File will still output to \"output.xml\", so rename the old output file if you don't want it to be overwritten.\n")
  143.             elif anotherChoice == "n":
  144.                 anotherChoiceLoop = False
  145.                 anotherLoop = False
  146.             else:
  147.                 print("\tInvalid input.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement