Debian Codegraph @

extras/build_ui.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2007 Lukáš Lalinský <[email protected]om>
5 # Copyright (C) 2009 Alexander Belchenko <[email protected]>
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 # project name based on `name` argument in setup() call
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 # f = open(pyfile, "wb")
70 f = open(pyfile, "w")
71 f.write(source)
72 f.close()