-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyqode_uic.py
58 lines (46 loc) · 1.38 KB
/
pyqode_uic.py
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
"""
Compile Qt ui files to python scripts using the ``pyqode.qt``
package instead of ``PyQt5``.
The tool is a simple wrapper on top of ``pyuic5``.
It does two thing:
- run pyuic5 (with the supplied arguments)
- replace ``from PyQt5 import`` by ``from pyqode.qt import`` in the
"""
import re
import sys
import os
def fix_imports(script):
"""
Replace "from PyQt5 import" by "from pyqode.qt import".
:param script: script path
"""
with open(script, 'r') as f_script:
lines = f_script.read().splitlines()
new_lines = []
for l in lines:
if l.startswith("import "):
l = "from . " + l
if "from PyQt5 import" in l:
l = l.replace("from PyQt5 import", "from pyqode.qt import")
new_lines.append(l)
with open(script, 'w') as f_script:
f_script.write("\n".join(new_lines))
def main(tool):
global args, cmd, p, match, output
args = ' '.join(sys.argv[1:])
cmd = '%s %s' % (tool, args)
p = re.compile(r'-o .*\.py')
match = re.search(p, args)
status = os.system(cmd)
if status == 0:
if match:
output = match.group().replace('-o', '').strip()
fix_imports(output)
else:
print('Error: no output specified', file=sys.stderr)
else:
return status
def main_uic():
return main('pyuic5')
def main_rcc():
return main('pyrcc5')