File indexing completed on 2024-05-12 16:09:14

0001 #!/usr/bin/env python2.7
0002 # This file is part of KDevelop
0003 # SPDX-FileCopyrightText: 2011 Victor Varvariuc <victor.varvariuc@gmail.com>
0004 # SPDX-FileCopyrightText: 2011 Sven Brauch <svenbrauch@googlemail.com>
0005 
0006 import os, sys, subprocess
0007 from xml.dom import minidom, NotFoundErr
0008 
0009 import sipconfig
0010 from PyQt4 import pyqtconfig
0011 
0012 config = pyqtconfig.Configuration() # Get the PyQt configuration information.
0013 
0014 def convertSipToXML(inFilePath, outFilePath):
0015     if not os.path.exists(outFilePath):
0016         command = [config.sip_bin, '-m', outFilePath, '-I', config.pyqt_sip_dir, '-I', config.pyqt_sip_dir+'/python3-PyKDE4']
0017         command.extend(config.pyqt_sip_flags.split())
0018         command.append(inFilePath)
0019 
0020         print('\nConverting sip to xml:\n%s' % ' '.join(command))
0021         try:
0022             print subprocess.check_output(command, stderr = subprocess.STDOUT)
0023         except subprocess.CalledProcessError as e:
0024             print('There was an error when running the command:')
0025             print(e.output)
0026 
0027         print('Opening and parsing XML document...')
0028         # replace the invalid "&"s which are used by pykde to indicate shortcuts
0029         # they would need to be escaped, but oh well
0030         f = open(outFilePath, 'r')
0031         data = f.read()
0032         f.close()
0033         data = data.replace('&', '')
0034         data = data.replace('("', '')
0035         data = data.replace('")', '')
0036         data = data.replace('""', '"')
0037         f = open(outFilePath, 'w')
0038         f.write(data)
0039         f.close()
0040     xmlDoc = minidom.parse(outFilePath)
0041 
0042     module = xmlDoc.firstChild
0043     assert module.nodeName == 'Module'
0044     moduleName = module.attributes['name'].value
0045     print('Module name: %s' % moduleName)
0046 
0047     def setIds(node):
0048         '''Set `name` attribute as id to be able to do node.getElementById('element_id')'''
0049         for node in node.childNodes:
0050             if node.nodeType == node.ELEMENT_NODE: # only element nodes
0051                 try:
0052                     node.setIdAttribute('name')
0053                 except NotFoundErr:
0054                     pass
0055                 setIds(node)
0056 
0057     print('Setting IDs...')
0058     setIds(module) # recursively set IDs
0059 
0060     print('Looking for child classes...')
0061     childClasses = {}
0062     for node in module.childNodes:
0063         # skip non element nodes
0064         if node.nodeType == node.ELEMENT_NODE and node.nodeName == 'Class':
0065             name = node.attributes['name'].value
0066             if name.count('.') >= 1:
0067                 childClasses[name] = node
0068 
0069     print('Reparenting classes...')
0070     for name, node in childClasses.iteritems():
0071         parentClassNode = xmlDoc
0072         parentClassNode = xmlDoc.getElementById(name.split('.')[0])
0073         for component in name.split('.')[1:-1]:
0074             previous = parentClassNode
0075             parentClassNode = xmlDoc.createElement("Class")
0076             parentClassNode.setAttribute("name", component)
0077             parentClassNode.setAttribute("convert", "1")
0078             previous.appendChild(parentClassNode)
0079             print "new class:", component
0080         node.setAttribute("name", name.split('.')[-1])
0081         parentClassNode.appendChild(node)
0082 
0083     print('Saving changes...')
0084     with open(outFilePath, 'w') as f:
0085         xmlDoc.writexml(f)
0086 
0087 
0088 sipDir = config.pyqt_sip_dir
0089 if not os.path.isdir(sipDir):
0090     print('Could not find sip direcotry: %s' % sipDir)
0091     print('Looks like package "python-qt-dev" is not installed.')
0092     sys.exit()
0093 
0094 modules = sys.argv[1:] # ['QtGui.xml', 'QtCore.xml'] # files to convert
0095 
0096 for moduleName in modules:
0097     sipFilePath = os.path.join(sipDir, moduleName, moduleName.split('/')[-1] + 'mod.sip')
0098     xmlFilePath = moduleName + '.xml'
0099     if os.path.isfile(sipFilePath) or os.path.exists(xmlFilePath.split('/')[-1]):
0100         convertSipToXML(sipFilePath, xmlFilePath.split('/')[-1])
0101     else:
0102         print('Input sip file does not exist: %s' % sipFilePath)