def fix_size(self, content, extra=50): """ Adjusts the width and height of the file switcher, based on its content. """ # Update size of dialog based on longest shortened path strings = [] if content: for rich_text in content: label = QLabel(rich_text) label.setTextFormat(Qt.PlainText) strings.append(label.text()) fm = label.fontMetrics() # Max width max_width = max([fm.width(s) * 1.3 for s in strings]) self.list.setMinimumWidth(max_width + extra) # Max height if len(strings) < 8: max_entries = len(strings) else: max_entries = 8 max_height = fm.height() * max_entries * 2.5 self.list.setMinimumHeight(max_height) # Set position according to size self.set_dialog_position()
def _drop_indicator_width(label: QLabel) -> float: ''' Drop indicator width depending on the operating system for a given label ''' if LINUX: return 40. return 3.0 * label.fontMetrics().height()
def fix_size(self, content, extra=50): """ Adjusts the width and height of the file switcher, based on its content. """ # Update size of dialog based on longest shortened path strings = [] if content: for rich_text in content: label = QLabel(rich_text) label.setTextFormat(Qt.PlainText) strings.append(label.text()) fm = label.fontMetrics() # Max width max_width = max([fm.width(s) * 1.3 for s in strings]) self.list.setMinimumWidth(max_width + extra) # Max height if len(strings) < 8: max_entries = len(strings) else: max_entries = 8 max_height = fm.height() * max_entries * 2.5 self.list.setMinimumHeight(max_height) # Set position according to size self.set_dialog_position()
def create_drop_indicator_widget(self, area: DockWidgetArea, mode: OverlayMode) -> QLabel: ''' Create drop indicator widget Parameters ---------- area : DockWidgetArea mode : OverlayMode Returns ------- value : QLabel ''' l = QLabel() l.setObjectName("DockWidgetAreaLabel") metric = 3.0 * l.fontMetrics().height() size = QSizeF(metric, metric) l.setPixmap( self.create_high_dpi_drop_indicator_pixmap(size, area, mode)) l.setWindowFlags(Qt.Tool | Qt.FramelessWindowHint) l.setAttribute(Qt.WA_TranslucentBackground) l.setProperty("dockWidgetArea", area) return l
def fix_size(self, content, extra=50): """Adjusts the width of the file switcher, based on the content.""" # Update size of dialog based on longest shortened path strings = [] if content: for rich_text in content: label = QLabel(rich_text) label.setTextFormat(Qt.PlainText) strings.append(label.text()) fm = label.fontMetrics() max_width = max([fm.width(s)*1.3 for s in strings]) self.list.setMinimumWidth(max_width + extra) self.set_dialog_position()
def update_drop_indicator_icon(self, label: QLabel): ''' Update drop indicator icon Parameters ---------- drop_indicator_widget : QLabel ''' metric = 3.0 * label.fontMetrics().height() size = QSizeF(metric, metric) area = label.property("dockWidgetArea") label.setPixmap( self.create_high_dpi_drop_indicator_pixmap(size, area, self.mode))
def get_item_size(self, content): """ Get the max size (width and height) for the elements of a list of strings as a QLabel. """ strings = [] if content: for rich_text in content: label = QLabel(rich_text) label.setTextFormat(Qt.PlainText) strings.append(label.text()) fm = label.fontMetrics() return (max([fm.width(s) * 1.3 for s in strings]), fm.height())
def get_item_size(self, content): """ Get the max size (width and height) for the elements of a list of strings as a QLabel. """ strings = [] if content: for rich_text in content: label = QLabel(rich_text) label.setTextFormat(Qt.PlainText) strings.append(label.text()) fm = label.fontMetrics() return (max([fm.width(s) * 1.3 for s in strings]), fm.height())
class HomeTab(WidgetBase): """Home applications tab.""" # name, prefix, sender sig_item_selected = Signal(object, object, object) # button_widget, sender sig_channels_requested = Signal(object, object) # application_name, command, prefix, leave_path_alone, sender sig_launch_action_requested = Signal(object, object, bool, object, object) # action, application_name, version, sender sig_conda_action_requested = Signal(object, object, object, object) # url sig_url_clicked = Signal(object) # TODO: Connect these signals to have more granularity # [{'name': package_name, 'version': version}...], sender sig_install_action_requested = Signal(object, object) sig_remove_action_requested = Signal(object, object) def __init__(self, parent=None): """Home applications tab.""" super(HomeTab, self).__init__(parent) # Variables self._parent = parent self.api = AnacondaAPI() self.applications = None self.style_sheet = None self.app_timers = None self.current_prefix = None # Widgets self.list = ListWidgetApplication() self.button_channels = ButtonHomeChannels('Channels') self.button_refresh = ButtonHomeRefresh('Refresh') self.combo = ComboHomeEnvironment() self.frame_top = FrameTabHeader(self) self.frame_body = FrameTabContent(self) self.frame_bottom = FrameTabFooter(self) self.label_home = LabelHome('Applications on') self.label_status_action = QLabel('') self.label_status = QLabel('') self.progress_bar = QProgressBar() self.first_widget = self.combo # Widget setup self.setObjectName('Tab') self.progress_bar.setTextVisible(False) self.list.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) # Layout layout_top = QHBoxLayout() layout_top.addWidget(self.label_home) layout_top.addWidget(SpacerHorizontal()) layout_top.addWidget(self.combo) layout_top.addWidget(SpacerHorizontal()) layout_top.addWidget(self.button_channels) layout_top.addWidget(SpacerHorizontal()) layout_top.addStretch() layout_top.addWidget(self.button_refresh) self.frame_top.setLayout(layout_top) layout_body = QVBoxLayout() layout_body.addWidget(self.list) self.frame_body.setLayout(layout_body) layout_bottom = QHBoxLayout() layout_bottom.addWidget(self.label_status_action) layout_bottom.addWidget(SpacerHorizontal()) layout_bottom.addWidget(self.label_status) layout_bottom.addStretch() layout_bottom.addWidget(self.progress_bar) self.frame_bottom.setLayout(layout_bottom) layout = QVBoxLayout() layout.addWidget(self.frame_top) layout.addWidget(self.frame_body) layout.addWidget(self.frame_bottom) self.setLayout(layout) # Signals self.list.sig_conda_action_requested.connect( self.sig_conda_action_requested) self.list.sig_url_clicked.connect(self.sig_url_clicked) self.list.sig_launch_action_requested.connect( self.sig_launch_action_requested) self.button_channels.clicked.connect(self.show_channels) self.button_refresh.clicked.connect(self.refresh_cards) self.progress_bar.setVisible(False) # --- Setup methods # ------------------------------------------------------------------------- def setup(self, conda_data): """Setup the tab content.""" conda_processed_info = conda_data.get('processed_info') environments = conda_processed_info.get('__environments') applications = conda_data.get('applications') self.current_prefix = conda_processed_info.get('default_prefix') self.set_environments(environments) self.set_applications(applications) def set_environments(self, environments): """Setup the environments list.""" # Disconnect to avoid triggering the signal when updating the content try: self.combo.currentIndexChanged.disconnect() except TypeError: pass self.combo.clear() for i, (env_prefix, env_name) in enumerate(environments.items()): self.combo.addItem(env_name, env_prefix) self.combo.setItemData(i, env_prefix, Qt.ToolTipRole) index = 0 for i, (env_prefix, env_name) in enumerate(environments.items()): if self.current_prefix == env_prefix: index = i break self.combo.setCurrentIndex(index) self.combo.currentIndexChanged.connect(self._item_selected) def set_applications(self, applications): """Build the list of applications present in the current conda env.""" apps = self.api.process_apps(applications, prefix=self.current_prefix) all_applications = [] installed_applications = [] not_installed_applications = [] # Check if some installed applications are not on the apps dict # for example when the channel was removed. linked_apps = self.api.conda_linked_apps_info(self.current_prefix) missing_apps = [app for app in linked_apps if app not in apps] for app in missing_apps: apps[app] = linked_apps[app] for app_name in sorted(list(apps.keys())): app = apps[app_name] item = ListItemApplication(name=app['name'], description=app['description'], versions=app['versions'], command=app['command'], image_path=app['image_path'], prefix=self.current_prefix, needs_license=app.get( 'needs_license', False)) if item.installed: installed_applications.append(item) else: not_installed_applications.append(item) all_applications = installed_applications + not_installed_applications self.list.clear() for i in all_applications: self.list.addItem(i) self.list.update_style_sheet(self.style_sheet) self.set_widgets_enabled(True) self.update_status() # --- Other methods # ------------------------------------------------------------------------- def current_environment(self): """Return the current selected environment.""" env_name = self.combo.currentText() return self.api.conda_get_prefix_envname(env_name) def refresh_cards(self): """Refresh application widgets. List widget items sometimes are hidden on resize. This method tries to compensate for that refreshing and repainting on user demand. """ self.list.update_style_sheet(self.style_sheet) self.list.repaint() for item in self.list.items(): if not item.widget.isVisible(): item.widget.repaint() def show_channels(self): """Emit signal requesting the channels dialog editor.""" self.sig_channels_requested.emit(self.button_channels, C.TAB_HOME) def update_list(self, name=None, version=None): """Update applications list.""" self.set_applications() self.label_status.setVisible(False) self.label_status_action.setVisible(False) self.progress_bar.setVisible(False) def update_versions(self, apps=None): """Update applications versions.""" self.items = [] for i in range(self.list.count()): item = self.list.item(i) self.items.append(item) if isinstance(item, ListItemApplication): name = item.name meta = apps.get(name) if meta: versions = meta['versions'] version = self.api.get_dev_tool_version(item.path) item.update_versions(version, versions) # --- Common Helpers (# FIXME: factor out to common base widget) # ------------------------------------------------------------------------- def _item_selected(self, index): """Notify that the item in combo (environment) changed.""" name = self.combo.itemText(index) prefix = self.combo.itemData(index) self.sig_item_selected.emit(name, prefix, C.TAB_HOME) @property def last_widget(self): """Return the last element of the list to be used in tab ordering.""" if self.list.items(): return self.list.items()[-1].widget def ordered_widgets(self, next_widget=None): """Return a list of the ordered widgets.""" ordered_widgets = [ self.combo, self.button_channels, self.button_refresh, ] ordered_widgets += self.list.ordered_widgets() return ordered_widgets def set_widgets_enabled(self, value): """Enable or disable widgets.""" self.combo.setEnabled(value) self.button_channels.setEnabled(value) self.button_refresh.setEnabled(value) for item in self.list.items(): item.button_install.setEnabled(value) item.button_options.setEnabled(value) if value: item.set_loading(not value) def update_items(self): """Update status of items in list.""" if self.list: for item in self.list.items(): item.update_status() def update_status(self, action='', message='', value=None, max_value=None): """Update the application action status.""" # Elide if too big width = QApplication.desktop().availableGeometry().width() max_status_length = round(width * (2.0 / 3.0), 0) msg_percent = 0.70 fm = self.label_status_action.fontMetrics() action = fm.elidedText(action, Qt.ElideRight, round(max_status_length * msg_percent, 0)) message = fm.elidedText( message, Qt.ElideRight, round(max_status_length * (1 - msg_percent), 0)) self.label_status_action.setText(action) self.label_status.setText(message) if max_value is None and value is None: self.progress_bar.setVisible(False) else: self.progress_bar.setVisible(True) self.progress_bar.setMaximum(max_value) self.progress_bar.setValue(value) def update_style_sheet(self, style_sheet=None): """Update custom CSS style sheet.""" if style_sheet is None: self.style_sheet = load_style_sheet() else: self.style_sheet = style_sheet self.list.update_style_sheet(style_sheet=self.style_sheet) self.setStyleSheet(self.style_sheet)
class PylintWidget(PluginMainWidget): """ Pylint widget. """ DEFAULT_OPTIONS = { "history_filenames": [], "max_entries": 30, "project_dir": None, } ENABLE_SPINNER = True DATAPATH = get_conf_path("pylint.results") VERSION = "1.1.0" # --- Signals sig_edit_goto_requested = Signal(str, int, str) """ This signal will request to open a file in a given row and column using a code editor. Parameters ---------- path: str Path to file. row: int Cursor starting row position. word: str Word to select on given row. """ sig_start_analysis_requested = Signal() """ This signal will request the plugin to start the analysis. This is to be able to interact with other plugins, which can only be done at the plugin level. """ def __init__(self, name=None, plugin=None, parent=None, options=DEFAULT_OPTIONS): super().__init__(name, plugin, parent, options) # Attributes self._process = None self.output = None self.error_output = None self.filename = None self.rdata = [] self.curr_filenames = self.get_option("history_filenames") self.code_analysis_action = None self.browse_action = None # Widgets self.filecombo = PythonModulesComboBox(self) self.ratelabel = QLabel(self) self.datelabel = QLabel(self) self.treewidget = ResultsTree(self) if osp.isfile(self.DATAPATH): try: with open(self.DATAPATH, "rb") as fh: data = pickle.loads(fh.read()) if data[0] == self.VERSION: self.rdata = data[1:] except (EOFError, ImportError): pass # Widget setup self.filecombo.setInsertPolicy(self.filecombo.InsertAtTop) for fname in self.curr_filenames[::-1]: self.set_filename(fname) # Layout layout = QVBoxLayout() layout.addWidget(self.treewidget) self.setLayout(layout) # Signals self.filecombo.valid.connect(self._check_new_file) self.treewidget.sig_edit_goto_requested.connect( self.sig_edit_goto_requested) # --- Private API # ------------------------------------------------------------------------ @Slot() def _start(self): """Start the code analysis.""" self.start_spinner() self.output = "" self.error_output = "" self._process = process = QProcess(self) process.setProcessChannelMode(QProcess.SeparateChannels) process.setWorkingDirectory(getcwd_or_home()) process.readyReadStandardOutput.connect(self._read_output) process.readyReadStandardError.connect( lambda: self._read_output(error=True)) process.finished.connect( lambda ec, es=QProcess.ExitStatus: self._finished(ec, es)) command_args = self.get_command(self.get_filename()) processEnvironment = QProcessEnvironment() processEnvironment.insert("PYTHONIOENCODING", "utf8") # resolve spyder-ide/spyder#14262 if running_in_mac_app(): pyhome = os.environ.get("PYTHONHOME") processEnvironment.insert("PYTHONHOME", pyhome) process.setProcessEnvironment(processEnvironment) process.start(sys.executable, command_args) running = process.waitForStarted() if not running: self.stop_spinner() QMessageBox.critical( self, _("Error"), _("Process failed to start"), ) def _read_output(self, error=False): process = self._process if error: process.setReadChannel(QProcess.StandardError) else: process.setReadChannel(QProcess.StandardOutput) qba = QByteArray() while process.bytesAvailable(): if error: qba += process.readAllStandardError() else: qba += process.readAllStandardOutput() text = str(qba.data(), "utf-8") if error: self.error_output += text else: self.output += text self.update_actions() def _finished(self, exit_code, exit_status): if not self.output: self.stop_spinner() if self.error_output: QMessageBox.critical( self, _("Error"), self.error_output, ) print("pylint error:\n\n" + self.error_output, file=sys.stderr) return filename = self.get_filename() rate, previous, results = self.parse_output(self.output) self._save_history() self.set_data(filename, (time.localtime(), rate, previous, results)) self.output = self.error_output + self.output self.show_data(justanalyzed=True) self.update_actions() self.stop_spinner() def _check_new_file(self): fname = self.get_filename() if fname != self.filename: self.filename = fname self.show_data() def _is_running(self): process = self._process return process is not None and process.state() == QProcess.Running def _kill_process(self): self._process.kill() self._process.waitForFinished() self.stop_spinner() def _update_combobox_history(self): """Change the number of files listed in the history combobox.""" max_entries = self.get_option("max_entries") if self.filecombo.count() > max_entries: num_elements = self.filecombo.count() diff = num_elements - max_entries for __ in range(diff): num_elements = self.filecombo.count() self.filecombo.removeItem(num_elements - 1) self.filecombo.selected() else: num_elements = self.filecombo.count() diff = max_entries - num_elements for i in range(num_elements, num_elements + diff): if i >= len(self.curr_filenames): break act_filename = self.curr_filenames[i] self.filecombo.insertItem(i, act_filename) def _save_history(self): """Save the current history filenames.""" if self.parent: list_save_files = [] for fname in self.curr_filenames: if _("untitled") not in fname: list_save_files.append(fname) self.curr_filenames = list_save_files[:MAX_HISTORY_ENTRIES] self.set_option("history_filenames", self.curr_filenames) else: self.curr_filenames = [] # --- PluginMainWidget API # ------------------------------------------------------------------------ def get_title(self): return _("Code Analysis") def get_focus_widget(self): return self.treewidget def setup(self, options): change_history_depth_action = self.create_action( PylintWidgetActions.ChangeHistory, text=_("History..."), tip=_("Set history maximum entries"), icon=self.create_icon("history"), triggered=self.change_history_depth, ) self.code_analysis_action = self.create_action( PylintWidgetActions.RunCodeAnalysis, icon_text=_("Analyze"), text=_("Run code analysis"), tip=_("Run code analysis"), icon=self.create_icon("run"), triggered=lambda: self.sig_start_analysis_requested.emit(), context=Qt.ApplicationShortcut, register_shortcut=True) self.browse_action = self.create_action( PylintWidgetActions.BrowseFile, text=_("Select Python file"), tip=_("Select Python file"), icon=self.create_icon("fileopen"), triggered=self.select_file, ) self.log_action = self.create_action( PylintWidgetActions.ShowLog, text=_("Output"), icon_text=_("Output"), tip=_("Complete output"), icon=self.create_icon("log"), triggered=self.show_log, ) options_menu = self.get_options_menu() self.add_item_to_menu( self.treewidget.get_action(OneColumnTreeActions.CollapseAllAction), menu=options_menu, section=PylintWidgetOptionsMenuSections.Global, ) self.add_item_to_menu( self.treewidget.get_action(OneColumnTreeActions.ExpandAllAction), menu=options_menu, section=PylintWidgetOptionsMenuSections.Global, ) self.add_item_to_menu( self.treewidget.get_action( OneColumnTreeActions.CollapseSelectionAction), menu=options_menu, section=PylintWidgetOptionsMenuSections.Section, ) self.add_item_to_menu( self.treewidget.get_action( OneColumnTreeActions.ExpandSelectionAction), menu=options_menu, section=PylintWidgetOptionsMenuSections.Section, ) self.add_item_to_menu( change_history_depth_action, menu=options_menu, section=PylintWidgetOptionsMenuSections.History, ) # Update OneColumnTree contextual menu self.add_item_to_menu( change_history_depth_action, menu=self.treewidget.menu, section=PylintWidgetOptionsMenuSections.History, ) self.treewidget.restore_action.setVisible(False) toolbar = self.get_main_toolbar() for item in [ self.filecombo, self.browse_action, self.code_analysis_action ]: self.add_item_to_toolbar( item, toolbar, section=PylintWidgetMainToolBarSections.Main, ) secondary_toolbar = self.create_toolbar("secondary") for item in [ self.ratelabel, self.create_stretcher(), self.datelabel, self.create_stretcher(), self.log_action ]: self.add_item_to_toolbar( item, secondary_toolbar, section=PylintWidgetMainToolBarSections.Main, ) self.show_data() if self.rdata: self.remove_obsolete_items() self.filecombo.insertItems(0, self.get_filenames()) self.code_analysis_action.setEnabled(self.filecombo.is_valid()) else: self.code_analysis_action.setEnabled(False) # Signals self.filecombo.valid.connect(self.code_analysis_action.setEnabled) def on_option_update(self, option, value): if option == "max_entries": self._update_combobox_history() elif option == "history_filenames": self.curr_filenames = value self._update_combobox_history() def update_actions(self): fm = self.ratelabel.fontMetrics() toolbar = self.get_main_toolbar() width = max([fm.width(_("Stop")), fm.width(_("Analyze"))]) widget = toolbar.widgetForAction(self.code_analysis_action) if widget: widget.setMinimumWidth(width * 1.5) if self._is_running(): self.code_analysis_action.setIconText(_("Stop")) self.code_analysis_action.setIcon(self.create_icon("stop")) else: self.code_analysis_action.setIconText(_("Analyze")) self.code_analysis_action.setIcon(self.create_icon("run")) self.remove_obsolete_items() # --- Public API # ------------------------------------------------------------------------ @Slot() @Slot(int) def change_history_depth(self, value=None): """ Set history maximum entries. Parameters ---------- value: int or None, optional The valur to set the maximum history depth. If no value is provided, an input dialog will be launched. Default is None. """ if value is None: dialog = QInputDialog(self) # Set dialog properties dialog.setModal(False) dialog.setWindowTitle(_("History")) dialog.setLabelText(_("Maximum entries")) dialog.setInputMode(QInputDialog.IntInput) dialog.setIntRange(MIN_HISTORY_ENTRIES, MAX_HISTORY_ENTRIES) dialog.setIntStep(1) dialog.setIntValue(self.get_option("max_entries")) # Connect slot dialog.intValueSelected.connect( lambda value: self.set_option("max_entries", value)) dialog.show() else: self.set_option("max_entries", value) def get_filename(self): """ Get current filename in combobox. """ return str(self.filecombo.currentText()) @Slot(str) def set_filename(self, filename): """ Set current filename in combobox. """ if self._is_running(): self._kill_process() # Don't try to reload saved analysis for filename, if filename # is the one currently displayed. # Fixes spyder-ide/spyder#13347 if self.get_filename() == filename: return filename = str(filename) index, _data = self.get_data(filename) if filename not in self.curr_filenames: self.filecombo.insertItem(0, filename) self.curr_filenames.insert(0, filename) self.filecombo.setCurrentIndex(0) else: try: index = self.filecombo.findText(filename) self.filecombo.removeItem(index) self.curr_filenames.pop(index) except IndexError: self.curr_filenames.remove(filename) self.filecombo.insertItem(0, filename) self.curr_filenames.insert(0, filename) self.filecombo.setCurrentIndex(0) num_elements = self.filecombo.count() if num_elements > self.get_option("max_entries"): self.filecombo.removeItem(num_elements - 1) self.filecombo.selected() def start_code_analysis(self, filename=None): """ Perform code analysis for given `filename`. If `filename` is None default to current filename in combobox. If this method is called while still running it will stop the code analysis. """ if self._is_running(): self._kill_process() else: if filename is not None: self.set_filename(filename) if self.filecombo.is_valid(): self._start() self.update_actions() def stop_code_analysis(self): """ Stop the code analysis process. """ if self._is_running(): self._kill_process() def remove_obsolete_items(self): """ Removing obsolete items. """ self.rdata = [(filename, data) for filename, data in self.rdata if is_module_or_package(filename)] def get_filenames(self): """ Return all filenames for which there is data available. """ return [filename for filename, _data in self.rdata] def get_data(self, filename): """ Get and load code analysis data for given `filename`. """ filename = osp.abspath(filename) for index, (fname, data) in enumerate(self.rdata): if fname == filename: return index, data else: return None, None def set_data(self, filename, data): """ Set and save code analysis `data` for given `filename`. """ filename = osp.abspath(filename) index, _data = self.get_data(filename) if index is not None: self.rdata.pop(index) self.rdata.insert(0, (filename, data)) while len(self.rdata) > self.get_option("max_entries"): self.rdata.pop(-1) with open(self.DATAPATH, "wb") as fh: pickle.dump([self.VERSION] + self.rdata, fh, 2) def show_data(self, justanalyzed=False): """ Show data in treewidget. """ text_color = MAIN_TEXT_COLOR prevrate_color = MAIN_PREVRATE_COLOR if not justanalyzed: self.output = None self.log_action.setEnabled(self.output is not None and len(self.output) > 0) if self._is_running(): self._kill_process() filename = self.get_filename() if not filename: return _index, data = self.get_data(filename) if data is None: text = _("Source code has not been rated yet.") self.treewidget.clear_results() date_text = "" else: datetime, rate, previous_rate, results = data if rate is None: text = _("Analysis did not succeed " "(see output for more details).") self.treewidget.clear_results() date_text = "" else: text_style = "<span style=\"color: %s\"><b>%s </b></span>" rate_style = "<span style=\"color: %s\"><b>%s</b></span>" prevrate_style = "<span style=\"color: %s\">%s</span>" color = DANGER_COLOR if float(rate) > 5.: color = SUCCESS_COLOR elif float(rate) > 3.: color = WARNING_COLOR text = _("Global evaluation:") text = ((text_style % (text_color, text)) + (rate_style % (color, ("%s/10" % rate)))) if previous_rate: text_prun = _("previous run:") text_prun = " (%s %s/10)" % (text_prun, previous_rate) text += prevrate_style % (prevrate_color, text_prun) self.treewidget.set_results(filename, results) date = time.strftime("%Y-%m-%d %H:%M:%S", datetime) date_text = text_style % (text_color, date) self.ratelabel.setText(text) self.datelabel.setText(date_text) @Slot() def show_log(self): """ Show output log dialog. """ if self.output: output_dialog = TextEditor(self.output, title=_("Code analysis output"), parent=self, readonly=True) output_dialog.resize(700, 500) output_dialog.exec_() # --- Python Specific # ------------------------------------------------------------------------ def get_pylintrc_path(self, filename): """ Get the path to the most proximate pylintrc config to the file. """ search_paths = [ # File"s directory osp.dirname(filename), # Working directory getcwd_or_home(), # Project directory self.get_option("project_dir"), # Home directory osp.expanduser("~"), ] return get_pylintrc_path(search_paths=search_paths) @Slot() def select_file(self, filename=None): """ Select filename using a open file dialog and set as current filename. If `filename` is provided, the dialog is not used. """ if filename is None: self.sig_redirect_stdio_requested.emit(False) filename, _selfilter = getopenfilename( self, _("Select Python file"), getcwd_or_home(), _("Python files") + " (*.py ; *.pyw)", ) self.sig_redirect_stdio_requested.emit(True) if filename: self.set_filename(filename) self.start_code_analysis() def get_command(self, filename): """ Return command to use to run code analysis on given filename """ command_args = [] if PYLINT_VER is not None: command_args = [ "-m", "pylint", "--output-format=text", "--msg-template=" '{msg_id}:{symbol}:{line:3d},{column}: {msg}"', ] pylintrc_path = self.get_pylintrc_path(filename=filename) if pylintrc_path is not None: command_args += ["--rcfile={}".format(pylintrc_path)] command_args.append(filename) return command_args def parse_output(self, output): """ Parse output and return current revious rate and results. """ # Convention, Refactor, Warning, Error results = {"C:": [], "R:": [], "W:": [], "E:": []} txt_module = "************* Module " module = "" # Should not be needed - just in case something goes wrong for line in output.splitlines(): if line.startswith(txt_module): # New module module = line[len(txt_module):] continue # Supporting option include-ids: ("R3873:" instead of "R:") if not re.match(r"^[CRWE]+([0-9]{4})?:", line): continue items = {} idx_0 = 0 idx_1 = 0 key_names = ["msg_id", "message_name", "line_nb", "message"] for key_idx, key_name in enumerate(key_names): if key_idx == len(key_names) - 1: idx_1 = len(line) else: idx_1 = line.find(":", idx_0) if idx_1 < 0: break item = line[(idx_0):idx_1] if not item: break if key_name == "line_nb": item = int(item.split(",")[0]) items[key_name] = item idx_0 = idx_1 + 1 else: pylint_item = (module, items["line_nb"], items["message"], items["msg_id"], items["message_name"]) results[line[0] + ":"].append(pylint_item) # Rate rate = None txt_rate = "Your code has been rated at " i_rate = output.find(txt_rate) if i_rate > 0: i_rate_end = output.find("/10", i_rate) if i_rate_end > 0: rate = output[i_rate + len(txt_rate):i_rate_end] # Previous run previous = "" if rate is not None: txt_prun = "previous run: " i_prun = output.find(txt_prun, i_rate_end) if i_prun > 0: i_prun_end = output.find("/10", i_prun) previous = output[i_prun + len(txt_prun):i_prun_end] return rate, previous, results
class FindInFilesWidget(PluginMainWidget): """ Find in files widget. """ DEFAULT_OPTIONS = { 'case_sensitive': False, 'exclude_case_sensitive': False, 'exclude': EXCLUDE_PATTERNS[0], 'exclude_index': None, 'exclude_regexp': False, 'path_history': [], 'max_results': 1000, 'hist_limit': MAX_PATH_HISTORY, 'more_options': False, 'search_in_index': None, 'search_text': '', 'search_text_regexp': False, 'supported_encodings': ("utf-8", "iso-8859-1", "cp1252"), 'text_color': MAIN_TEXT_COLOR, } REGEX_INVALID = "background-color:rgb(255, 80, 80);" REGEX_ERROR = _("Regular expression error") # Signals sig_edit_goto_requested = Signal(str, int, str) """ This signal will request to open a file in a given row and column using a code editor. Parameters ---------- path: str Path to file. row: int Cursor starting row position. word: str Word to select on given row. """ sig_finished = Signal() """ This signal is emitted to inform the search process has finished. """ sig_max_results_reached = Signal() """ This signal is emitted to inform the search process has finished due to reaching the maximum number of results. """ def __init__(self, name=None, plugin=None, parent=None, options=DEFAULT_OPTIONS): super().__init__(name, plugin, parent=parent, options=options) # Attributes self.text_color = self.get_option('text_color') self.supported_encodings = self.get_option('supported_encodings') self.search_thread = None self.running = False self.more_options_action = None self.extras_toolbar = None search_text = self.get_option('search_text') path_history = self.get_option('path_history') exclude = self.get_option('exclude') if not isinstance(search_text, (list, tuple)): search_text = [search_text] if not isinstance(exclude, (list, tuple)): exclude = [exclude] if not isinstance(path_history, (list, tuple)): path_history = [path_history] # Widgets self.search_text_edit = PatternComboBox( self, search_text, _("Search pattern"), ) self.search_label = QLabel(_('Search:')) self.search_in_label = QLabel(_('Location:')) self.exclude_label = QLabel(_('Exclude:')) self.path_selection_combo = SearchInComboBox(path_history, self) self.exclude_pattern_edit = PatternComboBox( self, exclude, _("Exclude pattern"), ) self.result_browser = ResultsBrowser( self, text_color=self.text_color, max_results=self.get_option('max_results'), ) # Setup self.search_label.setBuddy(self.search_text_edit) self.exclude_label.setBuddy(self.exclude_pattern_edit) fm = self.search_label.fontMetrics() base_size = int(fm.width(_('Location:')) * 1.2) self.search_text_edit.setMinimumWidth(base_size * 6) self.exclude_pattern_edit.setMinimumWidth(base_size * 6) self.path_selection_combo.setMinimumWidth(base_size * 6) self.search_label.setMinimumWidth(base_size) self.search_in_label.setMinimumWidth(base_size) self.exclude_label.setMinimumWidth(base_size) exclude_idx = self.get_option('exclude_index') if (exclude_idx is not None and exclude_idx >= 0 and exclude_idx < self.exclude_pattern_edit.count()): self.exclude_pattern_edit.setCurrentIndex(exclude_idx) search_in_index = self.get_option('search_in_index') self.path_selection_combo.set_current_searchpath_index(search_in_index) # Layout layout = QHBoxLayout() layout.addWidget(self.result_browser) self.setLayout(layout) # Signals self.path_selection_combo.sig_redirect_stdio_requested.connect( self.sig_redirect_stdio_requested) self.search_text_edit.valid.connect(lambda valid: self.find()) self.exclude_pattern_edit.valid.connect(lambda valid: self.find()) self.result_browser.sig_edit_goto_requested.connect( self.sig_edit_goto_requested) self.result_browser.sig_max_results_reached.connect( self.sig_max_results_reached) self.result_browser.sig_max_results_reached.connect( self._stop_and_reset_thread) # --- PluginMainWidget API # ------------------------------------------------------------------------ def get_title(self): return _("Find") def get_focus_widget(self): return self.search_text_edit def setup(self, options=DEFAULT_OPTIONS): self.search_regexp_action = self.create_action( FindInFilesWidgetActions.ToggleSearchRegex, text=_('Regular expression'), tip=_('Regular expression'), icon=self.create_icon('regex'), toggled=lambda val: self.set_option('search_text_regexp', val), initial=self.get_option('search_text_regexp'), ) self.case_action = self.create_action( FindInFilesWidgetActions.ToggleExcludeCase, text=_("Case sensitive"), tip=_("Case sensitive"), icon=self.create_icon("format_letter_case"), toggled=lambda val: self.set_option('case_sensitive', val), initial=self.get_option('case_sensitive'), ) self.find_action = self.create_action( FindInFilesWidgetActions.Find, icon_text=_('Search'), text=_("&Find in files"), tip=_("Search text in multiple files"), icon=self.create_icon('find'), triggered=self.find, register_shortcut=False, ) self.exclude_regexp_action = self.create_action( FindInFilesWidgetActions.ToggleExcludeRegex, text=_('Regular expression'), tip=_('Regular expression'), icon=self.create_icon('regex'), toggled=lambda val: self.set_option('exclude_regexp', val), initial=self.get_option('exclude_regexp'), ) self.exclude_case_action = self.create_action( FindInFilesWidgetActions.ToggleCase, text=_("Exclude case sensitive"), tip=_("Exclude case sensitive"), icon=self.create_icon("format_letter_case"), toggled=lambda val: self.set_option('exclude_case_sensitive', val), initial=self.get_option('exclude_case_sensitive'), ) self.more_options_action = self.create_action( FindInFilesWidgetActions.ToggleMoreOptions, text=_('Show advanced options'), tip=_('Show advanced options'), icon=self.create_icon("options_more"), toggled=lambda val: self.set_option('more_options', val), initial=self.get_option('more_options'), ) self.set_max_results_action = self.create_action( FindInFilesWidgetActions.MaxResults, text=_('Set maximum number of results'), tip=_('Set maximum number of results'), triggered=lambda x=None: self.set_max_results(), ) # Toolbar toolbar = self.get_main_toolbar() for item in [ self.search_label, self.search_text_edit, self.search_regexp_action, self.case_action, self.more_options_action, self.find_action ]: self.add_item_to_toolbar( item, toolbar=toolbar, section=FindInFilesWidgetMainToolBarSections.Main, ) # Exclude Toolbar self.extras_toolbar = self.create_toolbar( FindInFilesWidgetToolBars.Exclude) for item in [ self.exclude_label, self.exclude_pattern_edit, self.exclude_regexp_action, self.create_stretcher() ]: self.add_item_to_toolbar( item, toolbar=self.extras_toolbar, section=FindInFilesWidgetExcludeToolBarSections.Main, ) # Location toolbar location_toolbar = self.create_toolbar( FindInFilesWidgetToolBars.Location) for item in [self.search_in_label, self.path_selection_combo]: self.add_item_to_toolbar( item, toolbar=location_toolbar, section=FindInFilesWidgetLocationToolBarSections.Main, ) menu = self.get_options_menu() self.add_item_to_menu( self.set_max_results_action, menu=menu, ) def update_actions(self): if self.running: icon_text = _('Stop') icon = self.create_icon('stop') else: icon_text = _('Search') icon = self.create_icon('find') self.find_action.setIconText(icon_text) self.find_action.setIcon(icon) if self.extras_toolbar and self.more_options_action: self.extras_toolbar.setVisible( self.more_options_action.isChecked()) def on_option_update(self, option, value): if option == 'more_options': if value: icon = self.create_icon('options_less') tip = _('Hide advanced options') else: icon = self.create_icon('options_more') tip = _('Show advanced options') if self.extras_toolbar: self.extras_toolbar.setVisible(value) if self.more_options_action: self.more_options_action.setIcon(icon) self.more_options_action.setToolTip(tip) elif option == 'max_results': self.result_browser.set_max_results(value) # --- Private API # ------------------------------------------------------------------------ def _get_options(self): """ Get search options. """ text_re = self.search_regexp_action.isChecked() exclude_re = self.exclude_regexp_action.isChecked() case_sensitive = self.case_action.isChecked() # Clear fields self.search_text_edit.lineEdit().setStyleSheet("") self.exclude_pattern_edit.lineEdit().setStyleSheet("") self.exclude_pattern_edit.setToolTip("") self.search_text_edit.setToolTip("") utext = str(self.search_text_edit.currentText()) if not utext: return try: texts = [(utext.encode('utf-8'), 'utf-8')] except UnicodeEncodeError: texts = [] for enc in self.supported_encodings: try: texts.append((utext.encode(enc), enc)) except UnicodeDecodeError: pass exclude = str(self.exclude_pattern_edit.currentText()) if not case_sensitive: texts = [(text[0].lower(), text[1]) for text in texts] file_search = self.path_selection_combo.is_file_search() path = self.path_selection_combo.get_current_searchpath() if not exclude_re: items = [ fnmatch.translate(item.strip()) for item in exclude.split(",") if item.strip() != '' ] exclude = '|'.join(items) # Validate exclude regular expression if exclude: error_msg = regexp_error_msg(exclude) if error_msg: exclude_edit = self.exclude_pattern_edit.lineEdit() exclude_edit.setStyleSheet(self.REGEX_INVALID) tooltip = self.REGEX_ERROR + ': ' + str(error_msg) self.exclude_pattern_edit.setToolTip(tooltip) return None else: exclude = re.compile(exclude) # Validate text regular expression if text_re: error_msg = regexp_error_msg(texts[0][0]) if error_msg: self.search_text_edit.lineEdit().setStyleSheet( self.REGEX_INVALID) tooltip = self.REGEX_ERROR + ': ' + str(error_msg) self.search_text_edit.setToolTip(tooltip) return None else: texts = [(re.compile(x[0]), x[1]) for x in texts] return (path, file_search, exclude, texts, text_re, case_sensitive) def _update_options(self): """ Extract search options from widgets and set the corresponding option. """ hist_limit = self.get_option('hist_limit') search_texts = [ str(self.search_text_edit.itemText(index)) for index in range(self.search_text_edit.count()) ] excludes = [ str(self.search_text_edit.itemText(index)) for index in range(self.exclude_pattern_edit.count()) ] path_history = self.path_selection_combo.get_external_paths() self.set_option('path_history', path_history) self.set_option('search_text', search_texts[:hist_limit]) self.set_option('exclude', excludes[:hist_limit]) self.set_option('path_history', path_history[-hist_limit:]) self.set_option('exclude_index', self.exclude_pattern_edit.currentIndex()) self.set_option('search_in_index', self.path_selection_combo.currentIndex()) def _handle_search_complete(self, completed): """ Current search thread has finished. """ self.result_browser.set_sorting(ON) self.result_browser.expandAll() if self.search_thread is None: return self.sig_finished.emit() found = self.search_thread.get_results() self._stop_and_reset_thread() if found is not None: self.result_browser.show() self.stop_spinner() self.update_actions() def _stop_and_reset_thread(self, ignore_results=False): """Stop current search thread and clean-up.""" if self.search_thread is not None: if self.search_thread.isRunning(): if ignore_results: self.search_thread.sig_finished.disconnect( self.search_complete) self.search_thread.stop() self.search_thread.wait() self.search_thread.setParent(None) self.search_thread = None self.running = False self.stop_spinner() self.update_actions() # --- Public API # ------------------------------------------------------------------------ @property def path(self): """Return the current path.""" return self.path_selection_combo.path @property def project_path(self): """Return the current project path.""" return self.path_selection_combo.project_path @property def file_path(self): """Return the current file path.""" return self.path_selection_combo.file_path def set_directory(self, directory): """ Set directory as current path. Parameters ---------- directory: str Directory path string. """ self.path_selection_combo.path = osp.abspath(directory) def set_project_path(self, path): """ Set path as current project path. Parameters ---------- path: str Project path string. """ self.path_selection_combo.set_project_path(path) def disable_project_search(self): """Disable project search path in combobox.""" self.path_selection_combo.set_project_path(None) def set_file_path(self, path): """ Set path as current file path. Parameters ---------- path: str File path string. """ self.path_selection_combo.file_path = path def set_search_text(self, text): """ Set current search text. Parameters ---------- text: str Search string. Notes ----- If `text` is empty, focus will be given to the search lineedit and no search will be performed. """ if text: self.search_text_edit.add_text(text) self.search_text_edit.lineEdit().selectAll() self.search_text_edit.setFocus() def find(self): """ Start/stop find action. Notes ----- If there is no search running, this will start the search. If there is a search running, this will stop it. """ if self.running: self.stop() else: self.start() def stop(self): """Stop find thread.""" self._stop_and_reset_thread() def start(self): """Start find thread.""" options = self._get_options() if options is None: return self._stop_and_reset_thread(ignore_results=True) search_text = self.search_text_edit.currentText() # Update and set options self._update_options() # Start self.running = True self.start_spinner() self.search_thread = SearchThread(self, search_text, self.text_color) self.search_thread.sig_finished.connect(self._handle_search_complete) self.search_thread.sig_file_match.connect( self.result_browser.append_file_result) self.search_thread.sig_line_match.connect( self.result_browser.append_result) self.result_browser.clear_title(search_text) self.search_thread.initialize(*self._get_options()) self.search_thread.start() self.update_actions() def add_external_path(self, path): """ Parameters ---------- path: str Path to add to combobox. """ self.path_selection_combo.add_external_path(path) def set_max_results(self, value=None): """ Set maximum amount of results to add to the result browser. Parameters ---------- value: int, optional Number of results. If None an input dialog will be used. Default is None. """ if value is None: value, valid = QInputDialog.getInt( self, self.get_name(), _('Set maximum number of results: '), value=self.get_option('max_results'), min=1, step=1, ) else: valid = True if valid: self.set_option('max_results', value)
class BaseTimerStatus(StatusBarWidget): """Status bar widget base for widgets that update based on timers.""" TITLE = None TIP = None def __init__(self, parent, statusbar): """Status bar widget base for widgets that update based on timers.""" super(BaseTimerStatus, self).__init__(parent, statusbar) # Widgets self.label = QLabel(self.TITLE) self.value = QLabel() # Widget setup self.setToolTip(self.TIP) self.value.setAlignment(Qt.AlignRight) self.value.setFont(self.label_font) fm = self.value.fontMetrics() self.value.setMinimumWidth(fm.width('000%')) # Layout layout = self.layout() layout.addWidget(self.label) layout.addWidget(self.value) layout.addSpacing(20) # Setup if self.is_supported(): self.timer = QTimer() self.timer.timeout.connect(self.update_label) self.timer.start(2000) else: self.timer = None self.hide() def set_interval(self, interval): """Set timer interval (ms).""" if self.timer is not None: self.timer.setInterval(interval) def import_test(self): """Raise ImportError if feature is not supported.""" raise NotImplementedError def is_supported(self): """Return True if feature is supported.""" try: self.import_test() return True except ImportError: return False def get_value(self): """Return value (e.g. CPU or memory usage).""" raise NotImplementedError def update_label(self): """Update status label widget, if widget is visible.""" if self.isVisible(): self.value.setText('%d %%' % self.get_value())