File: sip_to_xml5.py

package info (click to toggle)
kdevelop-python 24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,640 kB
  • sloc: python: 183,048; cpp: 18,798; xml: 140; sh: 14; makefile: 9
file content (100 lines) | stat: -rw-r--r-- 3,643 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# This file is part of KDevelop
# SPDX-FileCopyrightText: 2011 Victor Varvariuc <victor.varvariuc@gmail.com>
# SPDX-FileCopyrightText: 2011 Sven Brauch <svenbrauch@googlemail.com>

import os, sys, subprocess
from xml.dom import minidom, NotFoundErr

import PyQt5.QtCore
sipBin = '/usr/bin/sip'
sipDir = '/usr/share/sip/PyQt5'
sipFlags = PyQt5.QtCore.PYQT_CONFIGURATION['sip_flags']

def convertSipToXML(inFilePath, outFilePath):
    command = [sipBin, '-m', outFilePath, '-I', sipDir]
    command.extend(sipFlags.split())
    command.append(inFilePath)

    print('\nConverting sip to xml:\n%s' % ' '.join(command))
    try:
        print(subprocess.check_output(command, stderr = subprocess.STDOUT))
    except subprocess.CalledProcessError as e:
        print('There was an error when running the command:')
        print(e.output)

    print('Opening and parsing XML document...')
    #replace the invalid "&"s which are used by pykde to indicate shortcuts
    #they would need to be escaped, but oh well
    f = open(outFilePath, 'r')
    data = f.read()
    f.close()
    data = data.replace('&', '')
    data = data.replace('("', '')
    data = data.replace('")', '')
    #data = data.replace('""', '"')
    f = open(outFilePath, 'w')
    f.write(data)
    f.close()
    xmlDoc = minidom.parse(outFilePath)

    module = xmlDoc.firstChild
    assert module.nodeName == 'Module'
    moduleName = module.attributes['name'].value
    print('Module name: %s' % moduleName)

    def setIds(node):
        '''Set `name` attribute as id to be able to do node.getElementById('element_id')'''
        for node in node.childNodes:
            if node.nodeType == node.ELEMENT_NODE: # only element nodes
                try:
                    node.setIdAttribute('name')
                except NotFoundErr:
                    pass
                setIds(node)

    print('Setting IDs...')
    setIds(module) # recursively set IDs

    print('Looking for child classes...')
    childClasses = {}
    for node in module.childNodes:
        # skip non element nodes
        if node.nodeType == node.ELEMENT_NODE and node.nodeName == 'Class':
            name = node.attributes['name'].value
            if name.count('.') >= 1:
                childClasses[name] = node

    print('Reparenting classes...')
    for name, node in childClasses.iteritems():
        parentClassNode = xmlDoc
        parentClassNode = xmlDoc.getElementById(name.split('.')[0])
        for component in name.split('.')[1:-1]:
            previous = parentClassNode
            parentClassNode = xmlDoc.createElement("Class")
            parentClassNode.setAttribute("name", component)
            parentClassNode.setAttribute("convert", "1")
            previous.appendChild(parentClassNode)
            print("new class:", component)
        node.setAttribute("name", name.split('.')[-1])
        parentClassNode.appendChild(node)

    print('Saving changes...')
    with open(outFilePath, 'w') as f:
        xmlDoc.writexml(f)


if not os.path.isdir(sipDir):
    print('Could not find sip direcotry: %s' % sipDir)
    print('Looks like package "python-qt-dev" is not installed.')
    sys.exit()

modules = sys.argv[1:] # ['QtGui.xml', 'QtCore.xml'] # files to convert

for moduleName in modules:
    sipFilePath = os.path.join(sipDir, moduleName, moduleName.split('/')[-1] + 'mod.sip')
    xmlFilePath = moduleName + '.xml'
    if os.path.isfile(sipFilePath) or os.path.exists(xmlFilePath.split('/')[-1]):
        convertSipToXML(sipFilePath, xmlFilePath.split('/')[-1])
    else:
        print('Input sip file does not exist: %s' % sipFilePath)