mirror of
https://gitlab.com/ita1024/waf.git
synced 2025-01-26 18:20:22 +01:00
First version of the pyqt5 extra to add QT5 ui/resources translation to py to be used with pyqt5 or pyside2
This commit is contained in:
parent
84808aa7e4
commit
0c78c7ad8e
280
waflib/extras/pyqt5.py
Normal file
280
waflib/extras/pyqt5.py
Normal file
@ -0,0 +1,280 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
# Thomas Nagy, 2006-2016 (ita) original C++ QT5 implementation
|
||||
# Federico Pellegrin, 2016 (fedepell) adapted for Python
|
||||
|
||||
"""
|
||||
This tool helps with finding Python Qt5 tools and libraries,
|
||||
and provides translation from QT5 files to Python code.
|
||||
|
||||
The following snippet illustrates the tool usage::
|
||||
|
||||
def options(opt):
|
||||
opt.load('py pyqt5')
|
||||
|
||||
def configure(conf):
|
||||
conf.load('py pyqt5')
|
||||
|
||||
def build(bld):
|
||||
bld(
|
||||
features = 'py pyqt5',
|
||||
source = 'main.py textures.qrc aboutDialog.ui',
|
||||
)
|
||||
|
||||
Here, the UI description and resource files will be processed
|
||||
to generate code.
|
||||
|
||||
Usage
|
||||
=====
|
||||
|
||||
Load the "pyqt5" tool.
|
||||
|
||||
Reference the qrc resources files or ui5 definition files as
|
||||
your sources and they will be translated into python code with
|
||||
the system tools (PyQt5 or pyside2 are searched in this order)
|
||||
and then compiled
|
||||
"""
|
||||
|
||||
try:
|
||||
from xml.sax import make_parser
|
||||
from xml.sax.handler import ContentHandler
|
||||
except ImportError:
|
||||
has_xml = False
|
||||
ContentHandler = object
|
||||
else:
|
||||
has_xml = True
|
||||
|
||||
import os, sys
|
||||
from waflib.Tools import python
|
||||
from waflib import Task, Utils, Options, Errors, Context
|
||||
from waflib.TaskGen import feature, after_method, extension, before_method
|
||||
from waflib.Configure import conf
|
||||
from waflib import Logs
|
||||
|
||||
EXT_RCC = ['.qrc']
|
||||
"""
|
||||
File extension for the resource (.qrc) files
|
||||
"""
|
||||
|
||||
EXT_UI = ['.ui']
|
||||
"""
|
||||
File extension for the user interface (.ui) files
|
||||
"""
|
||||
|
||||
|
||||
class XMLHandler(ContentHandler):
|
||||
"""
|
||||
Parses ``.qrc`` files
|
||||
"""
|
||||
def __init__(self):
|
||||
self.buf = []
|
||||
self.files = []
|
||||
def startElement(self, name, attrs):
|
||||
if name == 'file':
|
||||
self.buf = []
|
||||
def endElement(self, name):
|
||||
if name == 'file':
|
||||
self.files.append(str(''.join(self.buf)))
|
||||
def characters(self, cars):
|
||||
self.buf.append(cars)
|
||||
|
||||
@extension(*EXT_RCC)
|
||||
def create_pyrcc_task(self, node):
|
||||
"Creates rcc and py task for ``.qrc`` files"
|
||||
rcnode = node.change_ext('.py')
|
||||
self.create_task('pyrcc', node, rcnode)
|
||||
self.process_py(rcnode)
|
||||
|
||||
@extension(*EXT_UI)
|
||||
def create_pyuic_task(self, node):
|
||||
"Create uic tasks and py for user interface ``.ui`` definition files"
|
||||
uinode = node.change_ext('.py')
|
||||
self.create_task('ui5py', node, uinode)
|
||||
self.process_py(uinode)
|
||||
|
||||
@extension('.ts')
|
||||
def add_pylang(self, node):
|
||||
"""Adds all the .ts file into ``self.lang``"""
|
||||
self.lang = self.to_list(getattr(self, 'lang', [])) + [node]
|
||||
|
||||
@feature('pyqt5')
|
||||
@after_method('apply_link')
|
||||
def apply_pyqt5(self):
|
||||
"""
|
||||
The additional parameters are:
|
||||
|
||||
:param lang: list of translation files (\*.ts) to process
|
||||
:type lang: list of :py:class:`waflib.Node.Node` or string without the .ts extension
|
||||
:param langname: if given, transform the \*.ts files into a .qrc files to include in the binary file
|
||||
:type langname: :py:class:`waflib.Node.Node` or string without the .qrc extension
|
||||
"""
|
||||
if getattr(self, 'lang', None):
|
||||
qmtasks = []
|
||||
for x in self.to_list(self.lang):
|
||||
if isinstance(x, str):
|
||||
x = self.path.find_resource(x + '.ts')
|
||||
qmtasks.append(self.create_task('ts2qm', x, x.change_ext('.qm')))
|
||||
|
||||
|
||||
if getattr(self, 'langname', None):
|
||||
qmnodes = [x.outputs[0] for x in qmtasks]
|
||||
rcnode = self.langname
|
||||
if isinstance(rcnode, str):
|
||||
rcnode = self.path.find_or_declare(rcnode + '.qrc')
|
||||
t = self.create_task('qm2rcc', qmnodes, rcnode)
|
||||
k = create_pyrcc_task(self, t.outputs[0])
|
||||
|
||||
|
||||
class pyrcc(Task.Task):
|
||||
"""
|
||||
Processes ``.qrc`` files
|
||||
"""
|
||||
color = 'BLUE'
|
||||
run_str = '${QT_PYRCC} ${SRC} -o ${TGT}'
|
||||
ext_out = ['.py']
|
||||
|
||||
|
||||
def rcname(self):
|
||||
return os.path.splitext(self.inputs[0].name)[0]
|
||||
|
||||
def scan(self):
|
||||
"""Parse the *.qrc* files"""
|
||||
if not has_xml:
|
||||
Logs.error('No xml.sax support was found, rcc dependencies will be incomplete!')
|
||||
return ([], [])
|
||||
|
||||
parser = make_parser()
|
||||
curHandler = XMLHandler()
|
||||
parser.setContentHandler(curHandler)
|
||||
fi = open(self.inputs[0].abspath(), 'r')
|
||||
try:
|
||||
parser.parse(fi)
|
||||
finally:
|
||||
fi.close()
|
||||
|
||||
nodes = []
|
||||
names = []
|
||||
root = self.inputs[0].parent
|
||||
for x in curHandler.files:
|
||||
nd = root.find_resource(x)
|
||||
if nd: nodes.append(nd)
|
||||
else: names.append(x)
|
||||
return (nodes, names)
|
||||
|
||||
|
||||
class ui5py(Task.Task):
|
||||
"""
|
||||
Processes ``.ui`` files for python
|
||||
"""
|
||||
color = 'BLUE'
|
||||
run_str = '${QT_PYUIC} ${SRC} -o ${TGT}'
|
||||
ext_out = ['.py']
|
||||
|
||||
class ts2qm(Task.Task):
|
||||
"""
|
||||
Generates ``.qm`` files from ``.ts`` files
|
||||
"""
|
||||
color = 'BLUE'
|
||||
run_str = '${QT_LRELEASE} ${QT_LRELEASE_FLAGS} ${SRC} -qm ${TGT}'
|
||||
|
||||
class qm2rcc(Task.Task):
|
||||
"""
|
||||
Generates ``.qrc`` files from ``.qm`` files
|
||||
"""
|
||||
color = 'BLUE'
|
||||
after = 'ts2qm'
|
||||
def run(self):
|
||||
"""Create a qrc file including the inputs"""
|
||||
txt = '\n'.join(['<file>%s</file>' % k.path_from(self.outputs[0].parent) for k in self.inputs])
|
||||
code = '<!DOCTYPE RCC><RCC version="1.0">\n<qresource>\n%s\n</qresource>\n</RCC>' % txt
|
||||
self.outputs[0].write(code)
|
||||
|
||||
def configure(self):
|
||||
"""
|
||||
Besides the configuration options, the environment variable QT5_ROOT may be used
|
||||
to give the location of the qt5 libraries (absolute path).
|
||||
|
||||
The detection uses the program ``pkg-config`` through :py:func:`waflib.Tools.config_c.check_cfg`
|
||||
"""
|
||||
|
||||
self.find_pyqt5_binaries()
|
||||
|
||||
# warn about this during the configuration too
|
||||
if not has_xml:
|
||||
Logs.error('No xml.sax support was found, rcc dependencies will be incomplete!')
|
||||
|
||||
|
||||
|
||||
@conf
|
||||
def find_pyqt5_binaries(self):
|
||||
"""
|
||||
Detects Qt programs such as qmake, moc, uic, lrelease
|
||||
"""
|
||||
env = self.env
|
||||
opt = Options.options
|
||||
|
||||
qtdir = getattr(opt, 'qtdir', '')
|
||||
qtbin = getattr(opt, 'qtbin', '')
|
||||
|
||||
paths = []
|
||||
|
||||
if qtdir:
|
||||
qtbin = os.path.join(qtdir, 'bin')
|
||||
|
||||
# the qt directory has been given from QT5_ROOT - deduce the qt binary path
|
||||
if not qtdir:
|
||||
qtdir = self.environ.get('QT5_ROOT', '')
|
||||
qtbin = self.environ.get('QT5_BIN') or os.path.join(qtdir, 'bin')
|
||||
|
||||
if qtbin:
|
||||
paths = [qtbin]
|
||||
|
||||
# no qtdir, look in the path and in /usr/local/Trolltech
|
||||
if not qtdir:
|
||||
paths = self.environ.get('PATH', '').split(os.pathsep)
|
||||
paths.extend(['/usr/share/qt5/bin', '/usr/local/lib/qt5/bin'])
|
||||
try:
|
||||
lst = Utils.listdir('/usr/local/Trolltech/')
|
||||
except OSError:
|
||||
pass
|
||||
else:
|
||||
if lst:
|
||||
lst.sort()
|
||||
lst.reverse()
|
||||
|
||||
# keep the highest version
|
||||
qtdir = '/usr/local/Trolltech/%s/' % lst[0]
|
||||
qtbin = os.path.join(qtdir, 'bin')
|
||||
paths.append(qtbin)
|
||||
|
||||
|
||||
def find_bin(lst, var):
|
||||
if var in env:
|
||||
return
|
||||
for f in lst:
|
||||
try:
|
||||
ret = self.find_program(f, path_list=paths)
|
||||
except self.errors.ConfigurationError:
|
||||
pass
|
||||
else:
|
||||
env[var]=ret
|
||||
break
|
||||
|
||||
find_bin(['pyuic5','pyside2-uic'], 'QT_PYUIC')
|
||||
if not env.QT_PYUIC:
|
||||
self.fatal('cannot find the uic compiler for python for qt5')
|
||||
|
||||
find_bin(['pyrcc5','pyside2-rcc'], 'QT_PYRCC')
|
||||
if not env.QT_PYUIC:
|
||||
self.fatal('cannot find the rcc compiler for python for qt5')
|
||||
|
||||
find_bin(['pylupdate5', 'pyside2-lupdate'], 'QT_PYLUPDATE')
|
||||
find_bin(['lrelease-qt5', 'lrelease'], 'QT_LRELEASE')
|
||||
|
||||
|
||||
def options(opt):
|
||||
"""
|
||||
Command-line options
|
||||
"""
|
||||
pass
|
||||
|
Loading…
x
Reference in New Issue
Block a user