| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
om> |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
from distutils import log |
| 22 |
from PyQt5.QtWidgets import * |
| 23 |
from distutils.core import Command |
| 24 |
from distutils.dep_util import newer |
| 25 |
from io import StringIO |
| 26 |
import glob |
| 27 |
import os |
| 28 |
import re |
| 29 |
|
| 30 |
|
| 31 |
_translate_re = re.compile(r'QtGui\.QCoreApplication.translate\(.*?, (.*?), None, QtGui\.QApplication\)') |
| 32 |
_import_re = re.compile(r'(from PyQt5 import QtCore, QtGui)') |
| 33 |
|
| 34 |
|
| 35 |
class build_ui(Command): |
| 36 |
description = "build Qt UI files" |
| 37 |
user_options = [ |
| 38 |
('force', 'f', 'Force creation of ui files'), |
| 39 |
('module=', 'm', 'Specify UI module to (re)build'), |
| 40 |
] |
| 41 |
boolean_options = ['force'] |
| 42 |
|
| 43 |
def initialize_options(self): |
| 44 |
self.force = None |
| 45 |
self.module = None |
| 46 |
|
| 47 |
def finalize_options(self): |
| 48 |
if not self.module: |
| 49 |
self.module = glob.glob("ui/*.ui") |
| 50 |
else: |
| 51 |
self.module = ['ui/%s.ui' % self.module] |
| 52 |
|
| 53 |
def run(self): |
| 54 |
from PyQt5 import uic |
| 55 |
|
| 56 |
prj_name = self.distribution.get_name() or '?' |
| 57 |
|
| 58 |
for uifile in self.module: |
| 59 |
uifile = uifile.replace('\\', '/') |
| 60 |
pyfile = "lib/ui_%s.py" % os.path.splitext(os.path.basename(uifile))[0] |
| 61 |
if self.force or newer(uifile, pyfile): |
| 62 |
log.info("compiling %s -> %s", uifile, pyfile) |
| 63 |
tmp = StringIO() |
| 64 |
uic.compileUi(uifile, tmp) |
| 65 |
source = _translate_re.sub(r'gettext(\1)', tmp.getvalue()) |
| 66 |
source = source.replace("from PyQt5 import QtCore, QtGui, QtWidgets", |
| 67 |
"from PyQt5 import QtCore, QtGui, QtWidgets\n" |
| 68 |
"from breezy.plugins.%s.lib.i18n import gettext\n" % prj_name) |
| 69 |
|
| 70 |
f = open(pyfile, "w") |
| 71 |
f.write(source) |
| 72 |
f.close() |