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