|
Lines 1-163
Link Here
|
| 1 |
#!/usr/bin/env python |
|
|
| 2 |
|
| 3 |
#Terms and Conditions |
| 4 |
|
| 5 |
#Copyright (c) 2002 Jim Bublitz (jbublitz@nwinternet.com) |
| 6 |
|
| 7 |
#Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 8 |
#this software and associated documentation files (the "Software"), to deal in |
| 9 |
#the Software without restriction, including without limitation the rights to |
| 10 |
#use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 11 |
#of the Software, and to permit persons to whom the Software is furnished to do |
| 12 |
#so, subject to the following conditions: |
| 13 |
|
| 14 |
#The above copyright notice and this permission notice shall be included in all |
| 15 |
#copies or substantial portions of the Software. |
| 16 |
|
| 17 |
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
#COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 21 |
#IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 |
#CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 |
|
| 24 |
#Except as contained in this notice, the names of the copyright holders shall not |
| 25 |
#be used in advertising or otherwise to promote the sale, use or other dealings |
| 26 |
#in this Software without prior written authorization from the copyright holders. |
| 27 |
|
| 28 |
# 2003/04/19: some minor bits from Hans-Peter Jansen, <hpj@urpla.net> |
| 29 |
|
| 30 |
import sys, getopt, os, os.path, string |
| 31 |
|
| 32 |
#---------- globals ---------- |
| 33 |
|
| 34 |
FALSE = 0 |
| 35 |
TRUE = not FALSE |
| 36 |
addApp = TRUE |
| 37 |
addImport = TRUE |
| 38 |
pyuicPath = "pyuic" |
| 39 |
filename = "" |
| 40 |
i18nFunc = FALSE |
| 41 |
|
| 42 |
|
| 43 |
# --------- support functions ---------- |
| 44 |
|
| 45 |
def getOptions (): |
| 46 |
global filename |
| 47 |
|
| 48 |
opterr = 0 |
| 49 |
shortOptions = 'aip:' |
| 50 |
longOptions = ['noapp', 'noimport', 'pyuic=', 'usekdei18n'] |
| 51 |
|
| 52 |
try: |
| 53 |
optlist, args = getopt.getopt (sys.argv [1:], shortOptions, longOptions) |
| 54 |
except getopt.GetoptError: |
| 55 |
opterr = 1 |
| 56 |
optlist = [] |
| 57 |
args = [] |
| 58 |
|
| 59 |
if opterr or (len (args) != 1): |
| 60 |
print """\nUsage: |
| 61 |
|
| 62 |
kdepyuic [options] filename.ui |
| 63 |
|
| 64 |
Options: |
| 65 |
-a, --noapp Don't add TDEApplication code |
| 66 |
-i, --noimport Don't add tdecore, tdeui import statements |
| 67 |
-p, --pyuic Path to pyuic program |
| 68 |
--usekdei18n Adds KDEs default i18n functions to your Python KDE ui file |
| 69 |
""" |
| 70 |
return FALSE |
| 71 |
|
| 72 |
filename = args[0] |
| 73 |
return checkOptions (optlist) |
| 74 |
|
| 75 |
def checkOptions (optlist): |
| 76 |
global addApp, addImport, pyuicPath, i18nFunc |
| 77 |
|
| 78 |
for pair in optlist: |
| 79 |
if (pair [0] == '--noapp') or (pair [0] == '-a'): |
| 80 |
addApp = FALSE |
| 81 |
|
| 82 |
elif (pair [0] == '--noimport') or (pair [0] == '-i'): |
| 83 |
addImport = FALSE |
| 84 |
|
| 85 |
elif (pair [0] == '--pyuic') or (pair [0] == '-p'): |
| 86 |
pyuicPath = pair [1] |
| 87 |
|
| 88 |
if (pair [0] == '--usekdei18n'): |
| 89 |
i18nFunc=TRUE |
| 90 |
else: |
| 91 |
i18nFunc=FALSE |
| 92 |
|
| 93 |
|
| 94 |
# --------- operations ---------- |
| 95 |
|
| 96 |
def addimport (n): |
| 97 |
if addApp: |
| 98 |
n.write ('from tdecore import TDECmdLineArgs, TDEApplication\n') |
| 99 |
if i18nFunc: |
| 100 |
n.write ('from tdecore import i18n\n') |
| 101 |
n.write ('from tdeui import *\n\n') |
| 102 |
|
| 103 |
|
| 104 |
def addapp (indent, n): |
| 105 |
n.write (indent + 'appname = ""\n') |
| 106 |
n.write (indent + 'description = ""\n') |
| 107 |
n.write (indent + 'version = ""\n') |
| 108 |
n.write ('\n') |
| 109 |
n.write (indent + 'TDECmdLineArgs.init (sys.argv, appname, description, version)\n') |
| 110 |
n.write (indent + 'a = TDEApplication ()\n\n') |
| 111 |
|
| 112 |
def doPyuic (): |
| 113 |
|
| 114 |
fn = os.path.splitext (os.path.basename(filename)) [0] + '.py' |
| 115 |
opts = "" |
| 116 |
|
| 117 |
if i18nFunc: |
| 118 |
opts = opts + ' -tr i18n ' |
| 119 |
|
| 120 |
if addApp: |
| 121 |
opts = opts + ' -x ' |
| 122 |
|
| 123 |
opts = opts + ' -o ' |
| 124 |
|
| 125 |
|
| 126 |
if os.system (pyuicPath + opts + fn + ' ' + filename) != 0: |
| 127 |
print pyuicPath + opts + fn + ' ' + filename + " failed" |
| 128 |
sys.exit (-1) |
| 129 |
|
| 130 |
if addApp or addImport: |
| 131 |
m = open (fn, 'r') |
| 132 |
n = open (fn + '.tmp', 'w') |
| 133 |
|
| 134 |
buff = m.readlines () |
| 135 |
|
| 136 |
for line in buff: |
| 137 |
if addImport and (string.strip (line) == 'from qt import *'): |
| 138 |
n.write (line) |
| 139 |
addimport (n) |
| 140 |
elif addApp and (string.strip (line) == 'a = TQApplication(sys.argv)'): |
| 141 |
indent = 0 |
| 142 |
while line [indent] in string.whitespace: |
| 143 |
indent = indent + 1 |
| 144 |
addapp (line[:indent], n) |
| 145 |
elif string.find(line, " = KDatePicker(") != -1: |
| 146 |
o = string.find(line, ",") |
| 147 |
n.write (line[:o] + ",TQDate.currentDate()" + line[o:]) |
| 148 |
else: |
| 149 |
n.write (line) |
| 150 |
|
| 151 |
m.close () |
| 152 |
n.close () |
| 153 |
|
| 154 |
os.unlink (fn) |
| 155 |
os.rename (fn + '.tmp', fn) |
| 156 |
|
| 157 |
print fn + ' written' |
| 158 |
|
| 159 |
|
| 160 |
# --------- main ---------- |
| 161 |
|
| 162 |
getOptions () |
| 163 |
doPyuic () |