class FileBasedTextStream(QTextStream): def __init__(self, qfile): super().__init__(qfile) self.saved_file = qfile self.qfi = None # may never need this def rewind(self): self.flush() self.seek(0) def writeLine(self, str): self << str self << '\n' def open_mode(self): return self.saved_file.openMode() def fullpath(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.canonicalFilePath() def folderpath(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.canonicalPath() def filename(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.fileName() def basename(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.completeBaseName() def suffix(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.suffix()
def onActivated(self, index): path = self.model().filePath(index) fileInfo = QFileInfo(path) if (fileInfo.isDir()): prefs = preferences.Preferences.instance() prefs.setMapsDirectory(fileInfo.canonicalFilePath()) return self.mMainWindow.openFile(path)
def findDocument(self, fileName): canonicalFilePath = QFileInfo(fileName).canonicalFilePath() if (canonicalFilePath == ''): # file doesn't exist return -1 for i in range(self.mDocuments.size()): fileInfo = QFileInfo(self.mDocuments.at(i).fileName()) if (fileInfo.canonicalFilePath() == canonicalFilePath): return i return -1
def findDocument(self, fileName): canonicalFilePath = QFileInfo(fileName).canonicalFilePath() if (canonicalFilePath==''): # file doesn't exist return -1 for i in range(self.mDocuments.size()): fileInfo = QFileInfo(self.mDocuments.at(i).fileName()) if (fileInfo.canonicalFilePath() == canonicalFilePath): return i return -1
class FileBasedTextStream(QTextStream): def __init__(self, qfile): super().__init__(qfile) self.saved_file = qfile self.qfi = None # may never need this def rewind(self): self.flush() self.seek(0) def writeLine(self, str): self << str self << '\n' def open_mode(self): return self.saved_file.openMode() def fullpath(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.canonicalFilePath() def folderpath(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.canonicalPath() def filename(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.fileName() def basename(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.completeBaseName() def suffix(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.suffix() def flush(self): super().flush() # make sure text buffer goes to device return self.device().flush() # do a real flush def show_error(self, action, parent): error_number = self.device().error() if error_number: # is not 0, no error error_string = self.device().errorString() msg_string = 'Error {} ({}) on {}'.format(error_number, error_string, action) warning_msg(msg_string, self.fullpath(), parent)
class FileBasedTextStream(QTextStream): def __init__(self, qfile): super().__init__(qfile) self.saved_file = qfile self.qfi = None # may never need this def rewind(self): self.flush() self.seek(0) def writeLine(self, str): self << str self << '\n' def open_mode(self): return self.saved_file.openMode() def fullpath(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.canonicalFilePath() def folderpath(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.canonicalPath() def filename(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.fileName() def basename(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.completeBaseName() def suffix(self): if self.qfi is None: self.qfi = QFileInfo(self.saved_file) return self.qfi.suffix() def flush(self): super().flush() # make sure text buffer goes to device return self.device().flush() # do a real flush def show_error( self, action, parent ): error_number = self.device().error() if error_number : # is not 0, no error error_string = self.device().errorString() msg_string = 'Error {} ({}) on {}'.format( error_number, error_string, action ) warning_msg( msg_string, self.fullpath(), parent )
def _generate_resource(self, resources_dir, required_py, standard_library_dir, job_writer, nr_resources): """ Generate the application resource. """ project = self._project self._create_directory(resources_dir) resource_contents = [] # Handle any application package. if project.application_package.name is not None: fi = QFileInfo( project.path_from_user(project.application_package.name)) package_src_dir = fi.canonicalFilePath() package_name = project.application_package.name if package_name != '': package_name = fi.completeBaseName() self._write_package(resource_contents, resources_dir, package_name, project.application_package, package_src_dir, job_writer) # Handle the Python standard library. self._write_stdlib_py(resource_contents, resources_dir, required_py, standard_library_dir, job_writer) # Handle any additional packages. for package in project.other_packages: self._write_package(resource_contents, resources_dir, '', package, project.path_from_user(package.name), job_writer) # Handle the PyQt package. if any([m for m in project.pyqt_modules if m != 'sip']): pyqt_subdir = 'PyQt5' if project.application_is_pyqt5 else 'PyQt4' pyqt_dst_dir = resources_dir + '/' + pyqt_subdir pyqt_src_dir = standard_library_dir + '/site-packages/' + pyqt_subdir self._create_directory(pyqt_dst_dir) self._freeze(job_writer, pyqt_dst_dir + '/__init__.pyo', pyqt_src_dir + '/__init__.py', pyqt_subdir + '/__init__.py') resource_contents.append(pyqt_subdir + '/__init__.pyo') # Handle the PyQt.uic package. if 'uic' in project.pyqt_modules: skip_dirs = ['__pycache__'] if project.python_target_version[0] == 3: skip_dirs.append('port_v2') else: skip_dirs.append('port_v3') def copy_freeze(src, dst): for skip in skip_dirs: if skip in src: break else: if dst.endswith('.py'): src = QDir.fromNativeSeparators(src) dst = QDir.fromNativeSeparators(dst) rel_dst = dst[len(resources_dir) + 1:] + 'o' self._freeze(job_writer, dst + 'o', src, rel_dst) resource_contents.append(rel_dst) shutil.copytree(QDir.toNativeSeparators(pyqt_src_dir + '/uic'), QDir.toNativeSeparators(pyqt_dst_dir + '/uic'), copy_function=copy_freeze) # Write the .qrc files. if nr_resources == 1: resource_names = [ self._write_resource(resources_dir, resource_contents) ] else: resource_names = [] nr_files = len(resource_contents) if nr_resources > nr_files: nr_resources = nr_files per_resource = (nr_files + nr_resources - 1) // nr_resources start = 0 for r in range(nr_resources): end = start + per_resource if end > nr_files: end = nr_files resource_names.append( self._write_resource(resources_dir, resource_contents[start:end], r)) start += per_resource return resource_names
def _generate_resource(self, resources_dir, required_py, job_writer, nr_resources): """ Generate the application resource. """ project = self._project self._create_directory(resources_dir) resource_contents = [] # Handle any application package. if project.application_package.name is not None: fi = QFileInfo(project.path_from_user( project.application_package.name)) package_src_dir = fi.canonicalFilePath() package_name = project.application_package.name if package_name != '': package_name = fi.completeBaseName() self._write_package(resource_contents, resources_dir, package_name, project.application_package, package_src_dir, job_writer) # Handle the Python standard library. self._write_stdlib_py(resource_contents, resources_dir, required_py, job_writer) # Handle any additional packages. for package in project.other_packages: self._write_package(resource_contents, resources_dir, '', package, project.path_from_user(package.name), job_writer) # Handle the PyQt package. if len(project.pyqt_modules) != 0: pyqt_subdir = 'PyQt5' if project.application_is_pyqt5 else 'PyQt4' pyqt_dst_dir = resources_dir + '/' + pyqt_subdir pyqt_src_dir = project.path_from_user(project.python_target_stdlib_dir) + '/site-packages/' + pyqt_subdir self._create_directory(pyqt_dst_dir) self._freeze(job_writer, pyqt_dst_dir + '/__init__.pyo', pyqt_src_dir + '/__init__.py', pyqt_subdir + '/__init__.pyo') resource_contents.append(pyqt_subdir + '/__init__.pyo') # Handle the PyQt.uic package. if 'uic' in project.pyqt_modules: skip_dirs = ['__pycache__'] if project.python_target_version[0] == 3: skip_dirs.append('port_v2') else: skip_dirs.append('port_v3') def copy_freeze(src, dst): for skip in skip_dirs: if skip in src: break else: if dst.endswith('.py'): dst += 'o' src = QDir.fromNativeSeparators(src) dst = QDir.fromNativeSeparators(dst) rel_dst = dst[len(resources_dir) + 1:] self._freeze(job_writer, dst, src, rel_dst) resource_contents.append(rel_dst) shutil.copytree(QDir.toNativeSeparators(pyqt_src_dir + '/uic'), QDir.toNativeSeparators(pyqt_dst_dir + '/uic'), copy_function=copy_freeze) # Write the .qrc files. if nr_resources == 1: resource_names = [self._write_resource(resources_dir, resource_contents)] else: resource_names = [] nr_files = len(resource_contents) if nr_resources > nr_files: nr_resources = nr_files per_resource = (nr_files + nr_resources - 1) // nr_resources start = 0 for r in range(nr_resources): end = start + per_resource if end > nr_files: end = nr_files resource_names.append( self._write_resource(resources_dir, resource_contents[start:end], r)) start += per_resource return resource_names
def load(cls, file_name): """ Return a new project loaded from the given file. Raise a UserException if there was an error. """ fi = QFileInfo(file_name) tree = ElementTree() try: root = tree.parse(QDir.toNativeSeparators(fi.canonicalFilePath())) except Exception as e: raise UserException( "There was an error reading the project file.", str(e)) cls._assert(root.tag == 'Project', "Unexpected root tag '{0}', 'Project' expected.".format( root.tag)) # Check the project version number. version = root.get('version') cls._assert(version is not None, "Missing 'version' attribute.") try: version = int(version) except: version = None cls._assert(version is not None, "Invalid 'version'.") if version < cls.min_version: raise UserException("The project's format is no longer supported.") if version > cls.version: raise UserException( "The project's format is version {0} but only version {1} is supported.".format(version, cls.version)) # Create the project and populate it. project = cls() project._name = fi # The Python specific configuration. python = root.find('Python') cls._assert(python is not None, "Missing 'Python' tag.") project.python_host_interpreter = python.get('hostinterpreter', '') # This was added in version 5. project.python_use_platform = python.get('platformpython', '').split() project.python_source_dir = python.get('sourcedir', '') project.python_ssl = cls._get_bool(python, 'ssl', 'Python') project.python_target_include_dir = python.get('targetincludedir', '') project.python_target_library = python.get('targetlibrary', '') project.python_target_stdlib_dir = python.get('targetstdlibdir', '') major = cls._get_int(python, 'major', 'Python') minor = cls._get_int(python, 'minor', 'Python') patch = cls._get_int(python, 'patch', 'Python', default=0) project.python_target_version = (major, minor, patch) # The application specific configuration. application = root.find('Application') cls._assert(application is not None, "Missing 'Application' tag.") project.application_entry_point = application.get('entrypoint', '') project.application_is_pyqt5 = cls._get_bool(application, 'ispyqt5', 'Application') project.application_is_console = cls._get_bool(application, 'isconsole', 'Application') project.application_is_bundle = cls._get_bool(application, 'isbundle', 'Application') project.application_name = application.get('name', '') project.application_script = application.get('script', '') project.sys_path = application.get('syspath', '') # Any qmake configuration. This was added in version 5. qmake_configuration = application.find('QMakeConfiguration') if qmake_configuration is not None: project.qmake_configuration = qmake_configuration.text # Any application package. app_package = application.find('Package') if app_package is not None: project.application_package = cls._load_package(app_package) else: project.application_package = QrcPackage() # Any PyQt modules. for pyqt_m in root.iterfind('PyQtModule'): name = pyqt_m.get('name', '') cls._assert(name != '', "Missing or empty 'PyQtModule.name' attribute.") project.pyqt_modules.append(name) # Any standard library modules. for stdlib_module_element in root.iterfind('StdlibModule'): name = stdlib_module_element.get('name') cls._assert(name is not None, "Missing 'StdlibModule.name' attribute.") project.standard_library.append(name) # Any external C libraries. for external_lib_element in root.iterfind('ExternalLib'): name = external_lib_element.get('name') cls._assert(name is not None, "Missing 'ExternalLib.name' attribute.") defines = external_lib_element.get('defines', '') includepath = external_lib_element.get('includepath', '') libs = external_lib_element.get('libs', '') project.external_libraries.append( ExternalLibrary(name, defines, includepath, libs)) # Any other Python packages. project.other_packages = [cls._load_package(package) for package in root.iterfind('Package')] # Any other extension module. for extension_module_element in root.iterfind('ExtensionModule'): name = extension_module_element.get('name') cls._assert(name is not None, "Missing 'ExtensionModule.name' attribute.") qt = extension_module_element.get('qt', '') config = extension_module_element.get('config', '') sources = extension_module_element.get('sources', '') defines = extension_module_element.get('defines', '') includepath = extension_module_element.get('includepath', '') libs = extension_module_element.get('libs', '') project.other_extension_modules.append( ExtensionModule(name, qt, config, sources, defines, includepath, libs)) # The other configuration. others = root.find('Others') if others is not None: project.build_dir = others.get('builddir', '') project.qmake = others.get('qmake', '') return project
def load(cls, file_name): """ Return a new project loaded from the given file. Raise a UserException if there was an error. """ fi = QFileInfo(file_name) tree = ElementTree() try: root = tree.parse(QDir.toNativeSeparators(fi.canonicalFilePath())) except Exception as e: raise UserException("There was an error reading the project file.", str(e)) cls._assert( root.tag == 'Project', "Unexpected root tag '{0}', 'Project' expected.".format(root.tag)) # Check the project version number. version = root.get('version') cls._assert(version is not None, "Missing 'version' attribute.") try: version = int(version) except: version = None cls._assert(version is not None, "Invalid 'version'.") if version < cls.min_version: raise UserException("The project's format is no longer supported.") if version > cls.version: raise UserException( "The project's format is version {0} but only version {1} is supported." .format(version, cls.version)) # Create the project and populate it. project = cls() project._name = fi # The Python specific configuration. python = root.find('Python') cls._assert(python is not None, "Missing 'Python' tag.") project.python_host_interpreter = python.get('hostinterpreter', '') # This was added in version 5. project.python_use_platform = python.get('platformpython', '').split() project.python_source_dir = python.get('sourcedir', '') project.python_ssl = cls._get_bool(python, 'ssl', 'Python') project.python_target_include_dir = python.get('targetincludedir', '') project.python_target_library = python.get('targetlibrary', '') project.python_target_stdlib_dir = python.get('targetstdlibdir', '') major = cls._get_int(python, 'major', 'Python') minor = cls._get_int(python, 'minor', 'Python') patch = cls._get_int(python, 'patch', 'Python', default=0) project.python_target_version = (major, minor, patch) # The application specific configuration. application = root.find('Application') cls._assert(application is not None, "Missing 'Application' tag.") project.application_entry_point = application.get('entrypoint', '') project.application_is_pyqt5 = cls._get_bool(application, 'ispyqt5', 'Application') project.application_is_console = cls._get_bool(application, 'isconsole', 'Application') project.application_is_bundle = cls._get_bool(application, 'isbundle', 'Application') project.application_name = application.get('name', '') project.application_script = application.get('script', '') project.sys_path = application.get('syspath', '') # Any qmake configuration. This was added in version 5. qmake_configuration = application.find('QMakeConfiguration') if qmake_configuration is not None: project.qmake_configuration = qmake_configuration.text # Any application package. app_package = application.find('Package') if app_package is not None: project.application_package = cls._load_package(app_package) else: project.application_package = QrcPackage() # Any PyQt modules. for pyqt_m in root.iterfind('PyQtModule'): name = pyqt_m.get('name', '') cls._assert(name != '', "Missing or empty 'PyQtModule.name' attribute.") project.pyqt_modules.append(name) # Any standard library modules. for stdlib_module_element in root.iterfind('StdlibModule'): name = stdlib_module_element.get('name') cls._assert(name is not None, "Missing 'StdlibModule.name' attribute.") project.standard_library.append(name) # Any external C libraries. for external_lib_element in root.iterfind('ExternalLib'): name = external_lib_element.get('name') cls._assert(name is not None, "Missing 'ExternalLib.name' attribute.") defines = external_lib_element.get('defines', '') includepath = external_lib_element.get('includepath', '') libs = external_lib_element.get('libs', '') project.external_libraries.append( ExternalLibrary(name, defines, includepath, libs)) # Any other Python packages. project.other_packages = [ cls._load_package(package) for package in root.iterfind('Package') ] # Any other extension module. for extension_module_element in root.iterfind('ExtensionModule'): name = extension_module_element.get('name') cls._assert(name is not None, "Missing 'ExtensionModule.name' attribute.") qt = extension_module_element.get('qt', '') config = extension_module_element.get('config', '') sources = extension_module_element.get('sources', '') defines = extension_module_element.get('defines', '') includepath = extension_module_element.get('includepath', '') libs = extension_module_element.get('libs', '') project.other_extension_modules.append( ExtensionModule(name, qt, config, sources, defines, includepath, libs)) # The other configuration. others = root.find('Others') if others is not None: project.build_dir = others.get('builddir', '') project.qmake = others.get('qmake', '') return project