summaryrefslogtreecommitdiffstats
path: root/src/qtuicompiler.py
blob: 7e196dfa329bf14e8fb05721709399d68a22c891 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python
###########################################################################
# qtuicompiler - description                                              #
# ------------------------------                                          #
# begin     : Thu Apr 21 2005                                             #
# copyright : (C) 2005 by Simon Edwards                                   #
# email     : [email protected]                                         #
#                                                                         #
###########################################################################
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU Library General Public License as       #
#   published by the Free Software Foundation; either version 2 of the    #
#   License, or (at your option) any later version.                       #
#                                                                         #
###########################################################################

import os
import sys
from python_tqt import pyqtconfig
from distutils.spawn import *
import traceback

pyqt_configuration = pyqtconfig.Configuration()
pyuic_exe = None

############################################################################
def FindPyuic():
    global pyuic_exe
    if pyuic_exe is not None: return pyuic_exe
    
    pyuic_exe = find_executable('pyuic',pyqt_configuration.pyqt_bin_dir)
    if pyuic_exe is None:
        # Search on the $Path.
        pyuic_exe = find_executable('pyuic')

############################################################################
def CompileUI(ui_file_name, py_file_name=None, kde=False):
    pyuic_exe = find_executable('pyuic',pyqt_configuration.default_bin_dir)
    if pyuic_exe is None:
        # Search on the $Path.
        pyuic_exe = find_executable('pyuic')
    if pyuic_exe is None:
        pass  # FIXME raise something!
        
    if py_file_name is None:
        py_file_name = os.path.splitext(os.path.basename(ui_file_name))[0] + '.py'
    
    tmp_file_name = py_file_name + '.bak'
    cmd = [pyuic_exe]
    if kde:
        cmd.append('-tr')
        cmd.append('i18n')
    cmd.append('-o')
    cmd.append(tmp_file_name)
    cmd.append(ui_file_name)
    spawn(cmd)
    
    input = open(tmp_file_name, 'r')
    output = open(py_file_name, 'w')
    for line in input.readlines():
        if kde and string.strip(line) == 'from python_tqt.qt import *':
            output.write(line)
            output.write('from tdecore import *\nfrom tdeui import *\n\n')
        elif kde and string.find(line, " = KDatePicker(") != -1:
            o = string.find(line, ",")
            output.write(line[:o] + ",QDate.currentDate()" + line[o:])
        else:
            output.write (line)

    input.close()
    output.close()

    os.remove(tmp_file_name)

############################################################################
def DynamicImport(importargs,kde=False):
    file_name = importargs[0].replace('.',os.sep)
    file_name_ui = file_name + '.ui'
    if os.path.exists(file_name_ui):
        try:
            UpdateUI(file_name_ui,kde)
        except:
            traceback.print_exc()
            raise ImportError, "Unable to compile Qt designer file %s." % args[0]

############################################################################
def UpdateUI(ui_file,kde=False):
    py_file = ui_file[:-3] + '.py'
    remake = False
    if os.path.exists(py_file):
        remake = os.stat(py_file).st_mtime <= os.stat(ui_file).st_mtime
    else:
        remake = True

    if remake:
        CompileUI(ui_file, py_file, kde)

############################################################################
def main():
    # FIXME parse args and add --kde parameter.
    if len(sys.argv)!=3:
        print """\nUsage:
qtuicompiler filename.ui filename.py\n\n
"""
        return

    CompileUI(sys.argv[1],sys.argv[2])
    
if __name__=='__main__':
    main()