def test_pylint_max_history_conf(pylint_test_scripts, mocker): """Regression test for checking max_entries configuration. For further information see spyder-ide/spyder#12884 """ # Create the pylint widget for code analysis main_window = MainWindowMock() main_window.projects.get_active_project_path = mocker.MagicMock( return_value=None) pylint_sw = Pylint(parent=main_window) pylint_widget = PylintWidget(parent=pylint_sw) pylint_widget.filecombo.clear() script_0, script_1, script_2 = pylint_test_scripts( ["test_script_{}.py".format(n) for n in range(3)]) # Change the max_entry to 2 pylint_widget.parent.set_option('max_entries', 2) pylint_widget.change_history_limit(2) assert pylint_widget.parent.get_option('max_entries') == 2 # Call to set_filename pylint_widget.set_filename(filename=script_0) assert pylint_widget.filecombo.count() == 1 # Add to more filenames pylint_widget.set_filename(filename=script_1) pylint_widget.set_filename(filename=script_2) assert pylint_widget.filecombo.count() == 2 assert 'test_script_2.py' in pylint_widget.curr_filenames[0] assert 'test_script_1.py' in pylint_widget.curr_filenames[1] # Change the max entry to 1 pylint_widget.parent.set_option('max_entries', 1) pylint_widget.change_history_limit(1) assert pylint_widget.filecombo.count() == 1 assert 'test_script_2.py' in pylint_widget.curr_filenames[0]
class Pylint(SpyderPluginWidget): """Python source code analysis based on pylint.""" CONF_SECTION = 'pylint' CONFIGWIDGET_CLASS = PylintConfigPage CONF_FILE = False def __init__(self, parent=None): SpyderPluginWidget.__init__(self, parent) max_entries = self.get_option('max_entries', 50) self.pylint = PylintWidget(self, max_entries=max_entries, options_button=self.options_button, text_color=MAIN_TEXT_COLOR, prevrate_color=MAIN_PREVRATE_COLOR) layout = QVBoxLayout() layout.addWidget(self.pylint) self.setLayout(layout) # Add history_action to treewidget context menu history_action = create_action(self, _("History..."), None, ima.icon('history'), _("Set history maximum entries"), triggered=self.change_history_depth) self.pylint.treewidget.common_actions += (None, history_action) # Follow editorstacks tab change self.main.editor.sig_editor_focus_changed.connect(self.set_filename) # Used by Analyze button to check if file should be saved and start # analysis self.pylint.start_analysis.connect(self.run_pylint_from_analyze_button) #------ SpyderPluginWidget API -------------------------------------------- def get_plugin_title(self): """Return widget title""" return _("Code Analysis") def get_plugin_icon(self): """Return widget icon""" path = osp.join(self.PLUGIN_PATH, self.IMG_PATH) return ima.icon('pylint', icon_path=path) def get_focus_widget(self): """ Return the widget to give focus to when this plugin's dockwidget is raised on top-level """ return self.pylint.treewidget def get_plugin_actions(self): """Return a list of actions related to plugin""" return self.pylint.treewidget.get_menu_actions() def on_first_registration(self): """Action to be performed on first plugin registration""" self.tabify(self.main.help) self.dockwidget.hide() def register_plugin(self): """Register plugin in Spyder's main window""" self.pylint.treewidget.sig_edit_goto.connect(self.main.editor.load) self.pylint.redirect_stdio.connect( self.main.redirect_internalshell_stdio) self.add_dockwidget() pylint_act = create_action(self, _("Run static code analysis"), triggered=self.run_pylint) pylint_act.setEnabled(is_module_installed('pylint')) self.register_shortcut(pylint_act, context="Pylint", name="Run analysis") self.main.source_menu_actions += [MENU_SEPARATOR, pylint_act] self.main.editor.pythonfile_dependent_actions += [pylint_act] def refresh_plugin(self): """Refresh pylint widget""" self.pylint.remove_obsolete_items() def apply_plugin_settings(self, options): """Apply configuration file's plugin settings""" # The history depth option will be applied at # next Spyder startup, which is soon enough pass #------ Public API -------------------------------------------------------- @Slot() def change_history_depth(self): "Change history max entries""" depth, valid = QInputDialog.getInt(self, _('History'), _('Maximum entries'), self.get_option('max_entries'), 10, 10000) if valid: self.set_option('max_entries', depth) def get_filename(self): """Get current filename in combobox.""" return self.pylint.get_filename() @Slot() def set_filename(self): """Set filename without code analysis.""" self.pylint.set_filename(self.main.editor.get_current_filename()) @Slot() def run_pylint(self): """Run pylint code analysis""" if (self.get_option('save_before', True) and not self.main.editor.save()): return self.switch_to_plugin() self.analyze(self.main.editor.get_current_filename()) def analyze(self, filename): """Reimplement analyze method""" if self.dockwidget: self.switch_to_plugin() self.pylint.analyze(filename) @Slot() def run_pylint_from_analyze_button(self): """ See if file should and can be saved and run pylint code analysis. Does not check that file name is valid etc, so should only be used for Analyze button. """ if (self.get_option('save_before', True) and not self.main.editor.save()): return self.pylint.start()