mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 11:41:49 +02:00
refactor: improve pylint score
This commit is contained in:
@ -3,9 +3,11 @@ import subprocess
|
|||||||
import qdarktheme
|
import qdarktheme
|
||||||
from jedi import Script
|
from jedi import Script
|
||||||
from jedi.api import Completion
|
from jedi.api import Completion
|
||||||
|
|
||||||
|
# pylint: disable=no-name-in-module
|
||||||
from qtpy.Qsci import QsciScintilla, QsciLexerPython, QsciAPIs
|
from qtpy.Qsci import QsciScintilla, QsciLexerPython, QsciAPIs
|
||||||
from qtpy.QtCore import QFile, QTextStream, Signal, QThread
|
|
||||||
from qtpy.QtCore import Qt
|
from qtpy.QtCore import Qt
|
||||||
|
from qtpy.QtCore import Signal, QThread
|
||||||
from qtpy.QtGui import QColor, QFont
|
from qtpy.QtGui import QColor, QFont
|
||||||
from qtpy.QtWidgets import (
|
from qtpy.QtWidgets import (
|
||||||
QApplication,
|
QApplication,
|
||||||
@ -29,7 +31,7 @@ class AutoCompleter(QThread):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, file_path: str, api: QsciAPIs, enable_docstring: bool = False):
|
def __init__(self, file_path: str, api: QsciAPIs, enable_docstring: bool = False):
|
||||||
super(AutoCompleter, self).__init__(None)
|
super().__init__(None)
|
||||||
self.file_path = file_path
|
self.file_path = file_path
|
||||||
self.script: Script = None
|
self.script: Script = None
|
||||||
self.api: QsciAPIs = api
|
self.api: QsciAPIs = api
|
||||||
@ -78,7 +80,7 @@ class AutoCompleter(QThread):
|
|||||||
full_docstring = signatures[0].docstring(raw=True)
|
full_docstring = signatures[0].docstring(raw=True)
|
||||||
compact_docstring = self.get_compact_docstring(full_docstring)
|
compact_docstring = self.get_compact_docstring(full_docstring)
|
||||||
return compact_docstring
|
return compact_docstring
|
||||||
elif signatures and self.enable_docstring is False:
|
if signatures and self.enable_docstring is False:
|
||||||
return signatures[0].to_string()
|
return signatures[0].to_string()
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(f"Signature Error:{err}")
|
print(f"Signature Error:{err}")
|
||||||
@ -91,7 +93,8 @@ class AutoCompleter(QThread):
|
|||||||
completions (list[Completion]): A list of Completion objects to be added to the API.
|
completions (list[Completion]): A list of Completion objects to be added to the API.
|
||||||
"""
|
"""
|
||||||
self.api.clear()
|
self.api.clear()
|
||||||
[self.api.add(i.name) for i in completions]
|
for i in completions:
|
||||||
|
self.api.add(i.name)
|
||||||
self.api.prepare()
|
self.api.prepare()
|
||||||
|
|
||||||
def get_completions(self, line: int, index: int, text: str):
|
def get_completions(self, line: int, index: int, text: str):
|
||||||
@ -176,7 +179,7 @@ class BECEditor(QWidget):
|
|||||||
def __init__(self, toolbar_enabled=True, docstring_tooltip=False):
|
def __init__(self, toolbar_enabled=True, docstring_tooltip=False):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
self.scriptRunnerThread = None
|
self.script_runner_thread = None
|
||||||
self.file_path = None
|
self.file_path = None
|
||||||
self.docstring_tooltip = docstring_tooltip
|
self.docstring_tooltip = docstring_tooltip
|
||||||
# TODO just temporary solution, could be extended to other languages
|
# TODO just temporary solution, could be extended to other languages
|
||||||
@ -267,7 +270,6 @@ class BECEditor(QWidget):
|
|||||||
|
|
||||||
def loaded_autocomplete(self):
|
def loaded_autocomplete(self):
|
||||||
"""Placeholder method for actions after autocompletion data is loaded."""
|
"""Placeholder method for actions after autocompletion data is loaded."""
|
||||||
pass
|
|
||||||
|
|
||||||
def set_editor_style(self):
|
def set_editor_style(self):
|
||||||
"""Sets the style and color scheme for the editor."""
|
"""Sets the style and color scheme for the editor."""
|
||||||
@ -317,9 +319,9 @@ class BECEditor(QWidget):
|
|||||||
def run_script(self):
|
def run_script(self):
|
||||||
"""Runs the current script in the editor."""
|
"""Runs the current script in the editor."""
|
||||||
script = self.editor.text()
|
script = self.editor.text()
|
||||||
self.scriptRunnerThread = ScriptRunnerThread(script)
|
self.script_runner_thread = ScriptRunnerThread(script)
|
||||||
self.scriptRunnerThread.outputSignal.connect(self.update_terminal)
|
self.script_runner_thread.outputSignal.connect(self.update_terminal)
|
||||||
self.scriptRunnerThread.start()
|
self.script_runner_thread.start()
|
||||||
|
|
||||||
def update_terminal(self, text):
|
def update_terminal(self, text):
|
||||||
"""Updates the terminal with new text.
|
"""Updates the terminal with new text.
|
||||||
|
@ -9,6 +9,8 @@ from qtpy.QtWidgets import QWidget
|
|||||||
|
|
||||||
|
|
||||||
class ToolBarAction(ABC):
|
class ToolBarAction(ABC):
|
||||||
|
"""Abstract base class for action creators for the toolbar."""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def create(self, target: QWidget):
|
def create(self, target: QWidget):
|
||||||
"""Creates and returns an action to be added to a toolbar.
|
"""Creates and returns an action to be added to a toolbar.
|
||||||
@ -21,10 +23,11 @@ class ToolBarAction(ABC):
|
|||||||
Returns:
|
Returns:
|
||||||
QAction: The action created for the toolbar.
|
QAction: The action created for the toolbar.
|
||||||
"""
|
"""
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class OpenFileAction: # (ToolBarAction):
|
class OpenFileAction: # (ToolBarAction):
|
||||||
|
"""Action creator for the 'Open File' action in the toolbar."""
|
||||||
|
|
||||||
def create(self, target: QWidget):
|
def create(self, target: QWidget):
|
||||||
"""Creates an 'Open File' action for the toolbar.
|
"""Creates an 'Open File' action for the toolbar.
|
||||||
|
|
||||||
@ -42,6 +45,8 @@ class OpenFileAction: # (ToolBarAction):
|
|||||||
|
|
||||||
|
|
||||||
class SaveFileAction:
|
class SaveFileAction:
|
||||||
|
"""Action creator for the 'Save File' action in the toolbar."""
|
||||||
|
|
||||||
def create(self, target):
|
def create(self, target):
|
||||||
"""Creates a 'Save File' action for the toolbar.
|
"""Creates a 'Save File' action for the toolbar.
|
||||||
|
|
||||||
@ -59,6 +64,8 @@ class SaveFileAction:
|
|||||||
|
|
||||||
|
|
||||||
class RunScriptAction:
|
class RunScriptAction:
|
||||||
|
"""Action creator for the 'Run Script' action in the toolbar."""
|
||||||
|
|
||||||
def create(self, target):
|
def create(self, target):
|
||||||
"""Creates a 'Run Script' action for the toolbar.
|
"""Creates a 'Run Script' action for the toolbar.
|
||||||
|
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
|
# pylint: disable=no-name-in-module
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import subprocess
|
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
from unittest.mock import MagicMock
|
||||||
|
from unittest.mock import patch, mock_open
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from unittest.mock import MagicMock, Mock, patch
|
|
||||||
from unittest.mock import patch, mock_open
|
|
||||||
from PyQt6.QtWidgets import QTextEdit
|
|
||||||
from qtpy.Qsci import QsciScintilla, QsciLexerPython, QsciAPIs
|
|
||||||
|
|
||||||
from bec_widgets.widgets import BECEditor
|
from qtpy.QtWidgets import QTextEdit
|
||||||
from bec_widgets.widgets.editor.editor import AutoCompleter, ScriptRunnerThread, BECEditor
|
from qtpy.Qsci import QsciScintilla
|
||||||
|
|
||||||
|
from bec_widgets.widgets.editor.editor import AutoCompleter, BECEditor
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="function")
|
@pytest.fixture(scope="function")
|
||||||
@ -66,7 +66,7 @@ def test_autocompleter_suggestions(mock_script, editor, qtbot):
|
|||||||
"docstring_enabled, expected_signature",
|
"docstring_enabled, expected_signature",
|
||||||
[(True, "Mocked signature with docstring"), (False, "Mocked signature")],
|
[(True, "Mocked signature with docstring"), (False, "Mocked signature")],
|
||||||
)
|
)
|
||||||
def test_autocompleter_signature(mock_script, editor, qtbot, docstring_enabled, expected_signature):
|
def test_autocompleter_signature(mock_script, editor, docstring_enabled, expected_signature):
|
||||||
"""Test if the autocompleter provides correct function signature based on docstring setting."""
|
"""Test if the autocompleter provides correct function signature based on docstring setting."""
|
||||||
# Set docstring mode based on parameter
|
# Set docstring mode based on parameter
|
||||||
editor.docstring_tooltip = docstring_enabled
|
editor.docstring_tooltip = docstring_enabled
|
||||||
@ -92,7 +92,7 @@ def test_autocompleter_signature(mock_script, editor, qtbot, docstring_enabled,
|
|||||||
assert signature == expected_signature
|
assert signature == expected_signature
|
||||||
|
|
||||||
|
|
||||||
def test_open_file(editor, qtbot):
|
def test_open_file(editor):
|
||||||
"""Test open_file method of BECEditor."""
|
"""Test open_file method of BECEditor."""
|
||||||
# Create a temporary file with some content
|
# Create a temporary file with some content
|
||||||
with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as temp_file:
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as temp_file:
|
||||||
@ -104,15 +104,13 @@ def test_open_file(editor, qtbot):
|
|||||||
editor.open_file()
|
editor.open_file()
|
||||||
|
|
||||||
# Verify if the editor's text is set to the file content
|
# Verify if the editor's text is set to the file content
|
||||||
assert (
|
assert editor.editor.text() == "test file content"
|
||||||
editor.editor.text() == "test file content"
|
|
||||||
), "Editor should contain the text from the opened file"
|
|
||||||
|
|
||||||
# Clean up by removing the temporary file
|
# Clean up by removing the temporary file
|
||||||
os.remove(temp_file.name)
|
os.remove(temp_file.name)
|
||||||
|
|
||||||
|
|
||||||
def test_save_file(editor, qtbot):
|
def test_save_file(editor):
|
||||||
"""Test save_file method of BECEditor."""
|
"""Test save_file method of BECEditor."""
|
||||||
# Set some text in the editor
|
# Set some text in the editor
|
||||||
editor.editor.setText("test save content")
|
editor.editor.setText("test save content")
|
||||||
@ -131,7 +129,7 @@ def test_save_file(editor, qtbot):
|
|||||||
mock_file().write.assert_called_with("test save content")
|
mock_file().write.assert_called_with("test save content")
|
||||||
|
|
||||||
|
|
||||||
def test_open_file_through_toolbar(editor, qtbot):
|
def test_open_file_through_toolbar(editor):
|
||||||
"""Test the open_file method through the ModularToolBar."""
|
"""Test the open_file method through the ModularToolBar."""
|
||||||
# Create a temporary file
|
# Create a temporary file
|
||||||
with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as temp_file:
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as temp_file:
|
||||||
@ -146,15 +144,13 @@ def test_open_file_through_toolbar(editor, qtbot):
|
|||||||
with patch("builtins.open", new_callable=mock_open, read_data="test file content"):
|
with patch("builtins.open", new_callable=mock_open, read_data="test file content"):
|
||||||
open_action.trigger()
|
open_action.trigger()
|
||||||
# Verify if the editor's text is set to the file content
|
# Verify if the editor's text is set to the file content
|
||||||
assert (
|
assert editor.editor.text() == "test file content"
|
||||||
editor.editor.text() == "test file content"
|
|
||||||
), "Editor should contain the text from the opened file"
|
|
||||||
|
|
||||||
# Clean up
|
# Clean up
|
||||||
os.remove(temp_file.name)
|
os.remove(temp_file.name)
|
||||||
|
|
||||||
|
|
||||||
def test_save_file_through_toolbar(editor, qtbot):
|
def test_save_file_through_toolbar(editor):
|
||||||
"""Test the save_file method through the ModularToolBar."""
|
"""Test the save_file method through the ModularToolBar."""
|
||||||
# Set some text in the editor
|
# Set some text in the editor
|
||||||
editor.editor.setText("test save content")
|
editor.editor.setText("test save content")
|
||||||
|
Reference in New Issue
Block a user