Advertisement
Guest User

XML Modifier.py

a guest
Jun 27th, 2015
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.29 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 27, 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.     #Get modifications from user
  34.     if fileFound == True:
  35.         print("File loaded.\n")
  36.         #Ask for move
  37.         moveChoiceLoop = True
  38.         while moveChoiceLoop == True:
  39.             moveChoice = input("Move? (y/n): ")
  40.             if moveChoice == "y":
  41.                 moveRightLoop = True
  42.                 while moveRightLoop == True:
  43.                     moveRight = input("\tMove right by: ")
  44.                     if moveRight.replace("+","").replace("-","").replace(".","").isdigit():
  45.                         moveRight = eval(moveRight)
  46.                         moveRightLoop = False
  47.                     else:
  48.                         print("\t\tInvalid input.")
  49.                 moveDownLoop = True
  50.                 while moveDownLoop == True:
  51.                     moveDown = input("\tMove down by: ")
  52.                     if moveDown.replace("+","").replace("-","").replace(".","").isdigit():
  53.                         moveDown = eval(moveDown)
  54.                         moveDownLoop = False
  55.                         moveChoiceLoop = False
  56.                     else:
  57.                         print("\t\tInvalid input.")
  58.             elif moveChoice == "n":
  59.                 moveChoiceLoop = False
  60.             else:
  61.                 print("\tInvalid input.")
  62.         #Ask for rescale
  63.         rescaleChoiceLoop = True
  64.         while rescaleChoiceLoop == True:
  65.             rescaleChoice = input("Rescale? (y/n): ")
  66.             if rescaleChoice == "y":
  67.                 rescaleLoop = True
  68.                 while rescaleLoop == True:
  69.                     rescale = input("\tMultiply shape dimensions by: ")
  70.                     if rescale.replace("+","").replace("-","").replace(".","").isdigit():
  71.                         rescale = eval(rescale)
  72.                         rescaleLoop = False
  73.                         rescaleChoiceLoop = False
  74.                     else:
  75.                         print("\t\tInvalid input.")
  76.             elif rescaleChoice == "n":
  77.                 rescale = 1
  78.                 rescaleChoiceLoop = False
  79.             else:
  80.                 print("\tInvalid input.")
  81.         #Ask for window size change
  82.         windowSizeChoiceLoop = True
  83.         while windowSizeChoiceLoop == True:
  84.             windowSizeChoice = input("Change window size? (y/n): ")
  85.             if windowSizeChoice == "y":
  86.                 windowSizeLoop = True
  87.                 while windowSizeLoop == True:
  88.                     windowSize = input("\tWindow size: ")
  89.                     if windowSize.replace("+","").replace("-","").replace(".","").isdigit():
  90.                         windowSizeLoop = False
  91.                         windowSizeChoiceLoop = False
  92.                     else:
  93.                         print("\t\tInvalid input.")
  94.             elif windowSizeChoice == "n":
  95.                 windowSizeChoiceLoop = False
  96.             else:
  97.                 print("\tInvalid input.")
  98.         #Apply modifications
  99.         print("\nProcessing...",end="")
  100.         outputFile = open(fileName + "_edited.xml",'w')
  101.         modCounter = 0
  102.         for line in range(0,len(XML)):
  103.             #Make sure every PlaceObject element has Matrix, Translate, and Scale elements
  104.             if "<PlaceObject" in XML[line]:
  105.                 if "<Matrix>" in XML[line+1]:
  106.                     if "<Translate" not in XML[line+2]:
  107.                         XML[line+1] += " <Translate TranslateX=\"0\" TranslateY=\"0\" />"
  108.                         modCounter += 1
  109.                     if "<Scale" not in XML[line+3]:
  110.                         XML[line+2] += " <Scale ScaleX=\"1\" ScaleY=\"1\" />"
  111.                         modCounter += 1
  112.                 else:
  113.                     XML[line] += " <Matrix> <Translate TranslateX=\"0\" TranslateY=\"0\" /> <Scale ScaleX=\"1\" ScaleY=\"1\" /> </Matrix>"
  114.                     modCounter += 1
  115.             #Apply window size change
  116.             if windowSizeChoice == "y":
  117.                 if "<Movie SwfVersion=" in XML[line]:
  118.                     splitLine = XML[line].split()
  119.                     splitLine[2] = "Width=\"" + windowSize + "\""
  120.                     splitLine[3] = "Height=\"" + windowSize + "\""
  121.                     for element in range(0,len(splitLine)):
  122.                         if element == 0:
  123.                             XML[line] = splitLine[0] + " "
  124.                         else:
  125.                             XML[line] += splitLine[element] + " "
  126.                     modCounter += 1
  127.             #Apply move
  128.             if moveChoice == "y":
  129.                 if "<Translate" in XML[line]:
  130.                     splitLine = XML[line].split()
  131.                     if len(splitLine) == 4 or len(splitLine) == 8:
  132.                         moveX = eval(splitLine[1].replace("TranslateX=","").replace("\"",""))
  133.                         moveY = eval(splitLine[2].replace("TranslateY=","").replace("\"",""))
  134.                         moveX = (moveX + moveRight) * rescale
  135.                         moveY = (moveY + moveDown) * rescale
  136.                         splitLine[1] = "TranslateX=\"" + str(moveX) + "\""
  137.                         splitLine[2] = "TranslateY=\"" + str(moveY) + "\""
  138.                     elif len(splitLine) == 14:
  139.                         moveX = eval(splitLine[6].replace("TranslateX=","").replace("\"",""))
  140.                         moveY = eval(splitLine[7].replace("TranslateY=","").replace("\"",""))
  141.                         moveX = (moveX + moveRight) * rescale
  142.                         moveY = (moveY + moveDown) * rescale
  143.                         splitLine[6] = "TranslateX=\"" + str(moveX) + "\""
  144.                         splitLine[7] = "TranslateY=\"" + str(moveY) + "\""
  145.                     else:
  146.                         print("\nError. Length of splitLine for move is " + str(len(splitLine)) + ". Please report this error. Content of splitLine:")
  147.                         print(splitLine)
  148.                         break
  149.                     for element in range(0,len(splitLine)):
  150.                         if element == 0:
  151.                             XML[line] = splitLine[0] + " "
  152.                         else:
  153.                             XML[line] += splitLine[element] + " "
  154.                     modCounter += 1
  155.             #Apply rescale
  156.             if rescaleChoice == "y":
  157.                 if "<Scale" in XML[line]:
  158.                     splitLine = XML[line].split()
  159.                     if len(splitLine) == 4:
  160.                         rescaleX = eval(splitLine[1].replace("ScaleX=","").replace("\"",""))
  161.                         rescaleY = eval(splitLine[2].replace("ScaleY=","").replace("\"",""))
  162.                         rescaleX *= rescale
  163.                         rescaleY *= rescale
  164.                         splitLine[1] = "ScaleX=\"" + str(rescaleX) + "\""
  165.                         splitLine[2] = "ScaleY=\"" + str(rescaleY) + "\""
  166.                     elif len(splitLine) == 8:
  167.                         rescaleX = eval(splitLine[5].replace("ScaleX=","").replace("\"",""))
  168.                         rescaleY = eval(splitLine[6].replace("ScaleY=","").replace("\"",""))
  169.                         rescaleX *= rescale
  170.                         rescaleY *= rescale
  171.                         splitLine[5] = "ScaleX=\"" + str(rescaleX) + "\""
  172.                         splitLine[6] = "ScaleY=\"" + str(rescaleY) + "\""
  173.                     elif len(splitLine) == 14:
  174.                         rescaleX = eval(splitLine[10].replace("ScaleX=","").replace("\"",""))
  175.                         rescaleY = eval(splitLine[11].replace("ScaleY=","").replace("\"",""))
  176.                         rescaleX *= rescale
  177.                         rescaleY *= rescale
  178.                         splitLine[10] = "ScaleX=\"" + str(rescaleX) + "\""
  179.                         splitLine[11] = "ScaleY=\"" + str(rescaleY) + "\""
  180.                     else:
  181.                         print("\nError. Length of splitLine for rescale is " + str(len(splitLine)) + ". Please report this error. Content of splitLine:")
  182.                         print(splitLine)
  183.                         break
  184.                     for element in range(0,len(splitLine)):
  185.                         if element == 0:
  186.                             XML[line] = splitLine[0] + " "
  187.                         else:
  188.                             XML[line] += splitLine[element] + " "
  189.                     modCounter += 1
  190.             outputFile.write(XML[line])
  191.         #Done
  192.         outputFile.close()
  193.         print("\n" + str(modCounter) + " modifications made in " + str(len(XML)) + " line(s).")
  194.         print("Look for a file named \"" + fileName + "_edited.xml\" in your directory.\n")
  195.         anotherChoiceLoop = True
  196.         while anotherChoiceLoop == True:
  197.             anotherChoice = input("Modify another XML file? (y/n): ")
  198.             if anotherChoice == "y":
  199.                 anotherChoiceLoop = False
  200.             elif anotherChoice == "n":
  201.                 anotherChoiceLoop = False
  202.                 anotherLoop = False
  203.             else:
  204.                 print("\tInvalid input.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement