#!/usr/bin/env python

last_version = 3
from xml.etree import ElementTree

def from1To2(xml_file):
    xml_file.set('version', '2')
    for node in xml_file.iter('world'):
        width = int(node.get('width'))
        height = int(node.get('height'))
    for node in xml_file.find('items').iter():
        if node.tag == "item":
            node.set('x', str(round(float(node.get('x')) / width, 3)))
            node.set('y', str(round(float(node.get('y')) / height, 3)))
            node.set('width', str(round(float(node.get('width')) / width, 3)))
            node.set('height', str(round(float(node.get('height')) / height, 3)))
    for node in xml_file.find('rooms').iter():
        if node.tag == 'area':
            node.set('x', str(round(float(node.get('x')) / width, 3)))
            node.set('y', str(round(float(node.get('y')) / height, 3)))
            node.set('width', str(round(float(node.get('width')) / width, 3)))
            node.set('height', str(round(float(node.get('height')) / height, 3)))
    return xml_file

def from2To3(xml_file):
    xml_file.set('version', '3')
    for node in xml_file.find('rooms').iter():
        if node.tag == 'room':
            node.set('bgm', '')
    return xml_file

def upgradeVersion(path_file):
    upgrade_function = [from1To2, from2To3]
    xml_file = ElementTree.fromstring(open(path_file, 'rb').read())
    version = int(xml_file.get('version'))
    while version < last_version:
        xml_file = upgrade_function[version - 1](xml_file)
        version += 1
    return xml_file


if __name__ == "__main__":
    upgradeVersion('world.rooms')

@@ -1245,24 +1245,288 @@
 path_file):%0A
+    %22%22%22%0A    funzione per portare un qualsiasi file .rooms all'ultima versione%0A    corrente%0A    prende in ingresso il path del file di cui fare l'upgrade%0A    ritorna un xml.etree.Element del contenuto del file aggiornato all'ultima%0A    versione disponibile%0A    %22%22%22%0A
     upgrade_

add docstring