Esempio n. 1
0
    def _select_coverage_files(self):
        """
        Prompt a file selection dialog, returning file selections.

        NOTE: This saves & reuses the last known directory for subsequent uses.
        """
        if not self._last_directory:
            self._last_directory = disassembler.get_database_directory()

        # create & configure a Qt File Dialog for immediate use
        file_dialog = QtWidgets.QFileDialog(None, 'Open code coverage file',
                                            self._last_directory,
                                            'All Files (*.*)')
        file_dialog.setFileMode(QtWidgets.QFileDialog.ExistingFiles)

        # prompt the user with the file dialog, and await filename(s)
        filenames, _ = file_dialog.getOpenFileNames()

        #
        # remember the last directory we were in (parsed from a selected file)
        # for the next time the user comes to load coverage files
        #

        if filenames:
            self._last_directory = os.path.dirname(filenames[0]) + os.sep

        # log the captured (selected) filenames from the dialog
        logger.debug("Captured filenames from file dialog:")
        for name in filenames:
            logger.debug(" - %s" % name)

        # return the captured filenames
        return filenames
    def export_to_html(self):
        """
        Export the coverage table to an HTML report.
        """
        if not self._last_directory:
            self._last_directory = disassembler.get_database_directory()

        # build filename for the coverage report based off the coverage name
        name, _ = os.path.splitext(self._model._director.coverage_name)
        filename = name + ".html"
        suggested_filepath = os.path.join(self._last_directory, filename)

        # create & configure a Qt File Dialog for immediate use
        file_dialog = QtWidgets.QFileDialog()
        file_dialog.setFileMode(QtWidgets.QFileDialog.AnyFile)

        # we construct kwargs here for cleaner PySide/PyQt5 compatibility
        kwargs = \
        {
            "filter": "HTML Files (*.html)",
            "caption": "Save HTML Report",
            "directory" if USING_PYQT5 else "dir": suggested_filepath
        }

        # prompt the user with the file dialog, and await their chosen filename(s)
        filename, _ = file_dialog.getSaveFileName(**kwargs)
        if not filename:
            return

        # remember the last directory we were in (parsed from the saved file)
        self._last_directory = os.path.dirname(filename) + os.sep

        # write the generated HTML report to disk
        with open(filename, "wb") as fd:
            fd.write(self._model.to_html())

        lmsg("Saved HTML report to %s" % filename)