-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathpyqt.py
51 lines (47 loc) · 1.6 KB
/
pyqt.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
import re
def fix_icons(contents):
# replace ``addPixmap(QtGui.QPixmap(":/settings.svg"),``
# by ``addFile(":/settings.svg", QtCore.QSize(),``
contents = re.sub(
r'\baddPixmap\(QtGui\.QPixmap\(("[^"]*")\),',
r'addFile(\1, QtCore.QSize(),',
contents
)
return contents
def gettext(contents):
# replace ``_translate("context", `` by ``_(``
contents = re.sub(r'\n', (
'\n'
'_ = __import__(__package__.split(".", 1)[0])._\n'
), contents, 1)
contents = re.sub(
r'\n\s+_translate = QtCore\.QCoreApplication\.translate\n',
'\n',
contents
)
def repl(m):
gd = m.groupdict()
comment = '{ws}# i18n: Widget: “{widget}”'.format(**gd)
field = gd['field']
if field:
field = ' '.join(
word.lower()
for word in re.split(r'([A-Z][a-z_0-9]+)', field)
if word
)
comment += ", {field}".format(field=field)
comment += '.'
gd['pre2'] = gd['pre2'] or ''
return '{comment}\n{ws}{pre1}{pre2}_('.format(comment=comment, **gd)
contents = re.sub((r'(?P<ws> *)(?P<pre1>.*?)(?P<pre2>\.set(?P<field>\w+)\()?'
r'\b_translate\("(?P<widget>.*)",\s'), repl, contents)
assert re.search(r'\b_translate\(', contents) is None
return contents
def no_autoconnection(contents):
# remove calls to ``QtCore.QMetaObject.connectSlotsByName``
contents = re.sub(
r'\n\s+QtCore\.QMetaObject\.connectSlotsByName\(\w+\)\n',
'\n',
contents
)
return contents