1############################################################################# 2## 3## Copyright (C) 2020 Riverbank Computing Limited. 4## Copyright (C) 2006 Thorsten Marek. 5## All right reserved. 6## 7## This file is part of PyQt. 8## 9## You may use this file under the terms of the GPL v2 or the revised BSD 10## license as follows: 11## 12## "Redistribution and use in source and binary forms, with or without 13## modification, are permitted provided that the following conditions are 14## met: 15## * Redistributions of source code must retain the above copyright 16## notice, this list of conditions and the following disclaimer. 17## * Redistributions in binary form must reproduce the above copyright 18## notice, this list of conditions and the following disclaimer in 19## the documentation and/or other materials provided with the 20## distribution. 21## * Neither the name of the Riverbank Computing Limited nor the names 22## of its contributors may be used to endorse or promote products 23## derived from this software without specific prior written 24## permission. 25## 26## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 29## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 30## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 32## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 36## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 37## 38############################################################################# 39 40 41__all__ = ("compileUi", "compileUiDir", "loadUiType", "loadUi", "widgetPluginPath") 42 43from .Compiler import indenter, compiler 44 45 46_header = """# -*- coding: utf-8 -*- 47 48# Form implementation generated from reading ui file '%s' 49# 50# Created by: PyQt5 UI code generator %s 51# 52# WARNING: Any manual changes made to this file will be lost when pyuic5 is 53# run again. Do not edit this file unless you know what you are doing. 54 55 56""" 57 58 59_display_code = """ 60 61if __name__ == "__main__": 62\timport sys 63\tapp = QtWidgets.QApplication(sys.argv) 64\t%(widgetname)s = QtWidgets.%(baseclass)s() 65\tui = %(uiclass)s() 66\tui.setupUi(%(widgetname)s) 67\t%(widgetname)s.show() 68\tsys.exit(app.exec_())""" 69 70 71def compileUiDir(dir, recurse=False, map=None, **compileUi_args): 72 """compileUiDir(dir, recurse=False, map=None, **compileUi_args) 73 74 Creates Python modules from Qt Designer .ui files in a directory or 75 directory tree. 76 77 dir is the name of the directory to scan for files whose name ends with 78 '.ui'. By default the generated Python module is created in the same 79 directory ending with '.py'. 80 recurse is set if any sub-directories should be scanned. The default is 81 False. 82 map is an optional callable that is passed the name of the directory 83 containing the '.ui' file and the name of the Python module that will be 84 created. The callable should return a tuple of the name of the directory 85 in which the Python module will be created and the (possibly modified) 86 name of the module. The default is None. 87 compileUi_args are any additional keyword arguments that are passed to 88 the compileUi() function that is called to create each Python module. 89 """ 90 91 import os 92 93 # Compile a single .ui file. 94 def compile_ui(ui_dir, ui_file): 95 # Ignore if it doesn't seem to be a .ui file. 96 if ui_file.endswith('.ui'): 97 py_dir = ui_dir 98 py_file = ui_file[:-3] + '.py' 99 100 # Allow the caller to change the name of the .py file or generate 101 # it in a different directory. 102 if map is not None: 103 py_dir, py_file = map(py_dir, py_file) 104 105 # Make sure the destination directory exists. 106 try: 107 os.makedirs(py_dir) 108 except: 109 pass 110 111 ui_path = os.path.join(ui_dir, ui_file) 112 py_path = os.path.join(py_dir, py_file) 113 114 ui_file = open(ui_path, 'r') 115 py_file = open(py_path, 'w') 116 117 try: 118 compileUi(ui_file, py_file, **compileUi_args) 119 finally: 120 ui_file.close() 121 py_file.close() 122 123 if recurse: 124 for root, _, files in os.walk(dir): 125 for ui in files: 126 compile_ui(root, ui) 127 else: 128 for ui in os.listdir(dir): 129 if os.path.isfile(os.path.join(dir, ui)): 130 compile_ui(dir, ui) 131 132 133def compileUi(uifile, pyfile, execute=False, indent=4, from_imports=False, resource_suffix='_rc', import_from='.'): 134 """compileUi(uifile, pyfile, execute=False, indent=4, from_imports=False, resource_suffix='_rc', import_from='.') 135 136 Creates a Python module from a Qt Designer .ui file. 137 138 uifile is a file name or file-like object containing the .ui file. 139 pyfile is the file-like object to which the Python code will be written to. 140 execute is optionally set to generate extra Python code that allows the 141 code to be run as a standalone application. The default is False. 142 indent is the optional indentation width using spaces. If it is 0 then a 143 tab is used. The default is 4. 144 from_imports is optionally set to generate relative import statements. At 145 the moment this only applies to the import of resource modules. 146 resource_suffix is the suffix appended to the basename of any resource file 147 specified in the .ui file to create the name of the Python module generated 148 from the resource file by pyrcc4. The default is '_rc', i.e. if the .ui 149 file specified a resource file called foo.qrc then the corresponding Python 150 module is foo_rc. 151 import_from is optionally set to the package used for relative import 152 statements. The default is ``'.'``. 153 """ 154 155 from PyQt5.QtCore import PYQT_VERSION_STR 156 157 try: 158 uifname = uifile.name 159 except AttributeError: 160 uifname = uifile 161 162 indenter.indentwidth = indent 163 164 pyfile.write(_header % (uifname, PYQT_VERSION_STR)) 165 166 winfo = compiler.UICompiler().compileUi(uifile, pyfile, from_imports, resource_suffix, import_from) 167 168 if execute: 169 indenter.write_code(_display_code % winfo) 170 171 172def loadUiType(uifile, from_imports=False, resource_suffix='_rc', import_from='.'): 173 """loadUiType(uifile, from_imports=False, resource_suffix='_rc', import_from='.') -> (form class, base class) 174 175 Load a Qt Designer .ui file and return the generated form class and the Qt 176 base class. 177 178 uifile is a file name or file-like object containing the .ui file. 179 from_imports is optionally set to generate relative import statements. At 180 the moment this only applies to the import of resource modules. 181 resource_suffix is the suffix appended to the basename of any resource file 182 specified in the .ui file to create the name of the Python module generated 183 from the resource file by pyrcc4. The default is '_rc', i.e. if the .ui 184 file specified a resource file called foo.qrc then the corresponding Python 185 module is foo_rc. 186 import_from is optionally set to the package used for relative import 187 statements. The default is ``'.'``. 188 """ 189 190 import sys 191 192 from PyQt5 import QtWidgets 193 194 if sys.hexversion >= 0x03000000: 195 from .port_v3.string_io import StringIO 196 else: 197 from .port_v2.string_io import StringIO 198 199 code_string = StringIO() 200 winfo = compiler.UICompiler().compileUi(uifile, code_string, from_imports, 201 resource_suffix, import_from) 202 203 ui_globals = {} 204 exec(code_string.getvalue(), ui_globals) 205 206 uiclass = winfo["uiclass"] 207 baseclass = winfo["baseclass"] 208 209 # Assume that the base class is a custom class exposed in the globals. 210 ui_base = ui_globals.get(baseclass) 211 if ui_base is None: 212 # Otherwise assume it is in the QtWidgets module. 213 ui_base = getattr(QtWidgets, baseclass) 214 215 return (ui_globals[uiclass], ui_base) 216 217 218def loadUi(uifile, baseinstance=None, package='', resource_suffix='_rc'): 219 """loadUi(uifile, baseinstance=None, package='') -> widget 220 221 Load a Qt Designer .ui file and return an instance of the user interface. 222 223 uifile is a file name or file-like object containing the .ui file. 224 baseinstance is an optional instance of the Qt base class. If specified 225 then the user interface is created in it. Otherwise a new instance of the 226 base class is automatically created. 227 package is the optional package which is used as the base for any relative 228 imports of custom widgets. 229 resource_suffix is the suffix appended to the basename of any resource file 230 specified in the .ui file to create the name of the Python module generated 231 from the resource file by pyrcc4. The default is '_rc', i.e. if the .ui 232 file specified a resource file called foo.qrc then the corresponding Python 233 module is foo_rc. 234 """ 235 236 from .Loader.loader import DynamicUILoader 237 238 return DynamicUILoader(package).loadUi(uifile, baseinstance, resource_suffix) 239 240 241# The list of directories that are searched for widget plugins. 242from .objcreator import widgetPluginPath 243