GME  13
xme_id2guid.py
Go to the documentation of this file.
00001 import os
00002 import os.path
00003 import sys
00004 
00005 def id2guid(filename, output_filename):
00006     from xml.etree import ElementTree
00007     xme = ElementTree.parse(filename)
00008     id_guid_map = {}
00009     for element in xme.iter():
00010         if element.get('id') and element.get('guid'):
00011             id_guid_map[element.get('id')] = element.get('guid')
00012             del element.attrib['id']
00013     for element in xme.iter():
00014         if element.get('derivedfrom'):
00015             element.attrib['derivedfrom'] = id_guid_map[element.get('derivedfrom')]
00016     for element in xme.iter('reference'):
00017         if element.get('referred'):
00018             element.attrib['referred'] = id_guid_map[element.get('referred')]
00019     for element in xme.iter('set'):
00020         if element.get('members'):
00021             element.attrib['members'] = " ".join([id_guid_map[id] for id in element.get('members').split()])
00022     for element in xme.iter('connpoint'):
00023         if element.get('target'):
00024             element.attrib['target'] = id_guid_map[element.get('target')]
00025         if element.get('refs'):
00026             element.attrib['refs'] = " ".join([id_guid_map[id] for id in element.get('refs').split()])
00027     
00028     with open(output_filename, 'wb') as output:
00029         output.write('<!DOCTYPE project SYSTEM "mga2.dtd">\n')
00030         xme.write(output)
00031 
00032 if __name__ == '__main__':
00033     input = sys.argv[1]
00034     output = os.path.splitext(input)[0] + "_guids.xme"
00035     id2guid(input, output)