From f08698d1816b5574b58b96c4466cf91eefd0248e Mon Sep 17 00:00:00 2001 From: Holger Badorreck <6099617+hoba87@users.noreply.github.com> Date: Tue, 23 Apr 2024 13:34:39 +0200 Subject: [PATCH] some general code care (#6) * layout changes * don't show deprecations for other packages * backend_kivy.py: sorted imports, replace deprecated distutils.version * relative imports, self->cls for classmethods * loosening matplotlib and kivy version requirements * linting errors * linting errors * isort * black code formatter * compatibility flake8, black --------- Co-authored-by: Holger Badorreck --- kivy_garden/matplotlib/__init__.py | 9 +- kivy_garden/matplotlib/_version.py | 2 +- kivy_garden/matplotlib/backend_kivy.py | 188 +++++++++--------- kivy_garden/matplotlib/backend_kivyagg.py | 15 +- .../matplotlib/tests/test_backend_kivy.py | 2 +- .../matplotlib/tests/test_backend_kivyagg.py | 2 +- kivy_garden/matplotlib/tests/test_events.py | 28 +-- pyproject.toml | 10 + pytest.ini | 2 + setup.cfg | 3 + setup.py | 5 +- 11 files changed, 145 insertions(+), 121 deletions(-) create mode 100644 pyproject.toml create mode 100644 pytest.ini diff --git a/kivy_garden/matplotlib/__init__.py b/kivy_garden/matplotlib/__init__.py index 4344e42..61a4cb2 100644 --- a/kivy_garden/matplotlib/__init__.py +++ b/kivy_garden/matplotlib/__init__.py @@ -6,16 +6,15 @@ """ from ._version import __version__ - -from kivy_garden.matplotlib.backend_kivy import ( +from .backend_kivy import ( FigureCanvasKivy, FigureManagerKivy, - RendererKivy, GraphicsContextKivy, - NavigationToolbar2Kivy, MPLKivyApp, + NavigationToolbar2Kivy, + RendererKivy, ) -from kivy_garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg +from .backend_kivyagg import FigureCanvasKivyAgg __all__ = ( FigureCanvasKivy.__name__, diff --git a/kivy_garden/matplotlib/_version.py b/kivy_garden/matplotlib/_version.py index e9856f7..1acd902 100644 --- a/kivy_garden/matplotlib/_version.py +++ b/kivy_garden/matplotlib/_version.py @@ -1 +1 @@ -__version__ = '0.1.1.dev0' +__version__ = "0.1.1.dev0" diff --git a/kivy_garden/matplotlib/backend_kivy.py b/kivy_garden/matplotlib/backend_kivy.py index 71f1ff5..5e6dd1a 100644 --- a/kivy_garden/matplotlib/backend_kivy.py +++ b/kivy_garden/matplotlib/backend_kivy.py @@ -61,7 +61,7 @@ my_mpl_kivy_widget = FigureCanvas(fig) fig.canvas.mpl_connect('button_press_event', callback_handler) -2. Use pyplot to write the application following matplotlib sintax as can be +2. Use pyplot to write the application following matplotlib syntax as can be seen in the second example below. In this case a Kivy application will be created automatically from the matplotlib instructions and a NavigationToolbar will be added to the main canvas. @@ -229,29 +229,35 @@ def close(event): unicode_literals, ) -import six +import numbers import os +import textwrap +import uuid + +import numpy as np +from packaging.version import Version +import six + import matplotlib +from matplotlib import _path, rcParams from matplotlib._pylab_helpers import Gcf from matplotlib.backend_bases import ( - RendererBase, - GraphicsContextBase, - FigureManagerBase, + Event, FigureCanvasBase, + FigureManagerBase, + GraphicsContextBase, + KeyEvent, + MouseEvent, NavigationToolbar2, + RendererBase, + ResizeEvent, + ShowBase, TimerBase, ) -from matplotlib.figure import Figure -from matplotlib.transforms import Affine2D -from matplotlib.backend_bases import (ShowBase, - Event, - ResizeEvent, - MouseEvent, - KeyEvent) from matplotlib.backends.backend_agg import FigureCanvasAgg +from matplotlib.figure import Figure from matplotlib.mathtext import MathTextParser -from matplotlib import rcParams -from matplotlib import _path +from matplotlib.transforms import Affine2D try: import kivy @@ -259,49 +265,51 @@ def close(event): raise ImportError("this backend requires Kivy to be installed.") from kivy.app import App +from kivy.base import EventLoop +from kivy.clock import Clock +from kivy.core.image import Image +from kivy.core.text import Label as CoreLabel +from kivy.core.window import Window +from kivy.graphics import ( + Color, + Line, + Mesh, + Rectangle, + Rotate, + StencilPop, + StencilPush, + StencilUnUse, + StencilUse, + Translate, +) +from kivy.graphics.context_instructions import PopMatrix, PushMatrix +from kivy.graphics.instructions import InstructionGroup +from kivy.graphics.tesselator import Tesselator from kivy.graphics.texture import Texture -from kivy.graphics import Rectangle -from kivy.uix.widget import Widget -from kivy.uix.floatlayout import FloatLayout -from kivy.uix.behaviors import FocusBehavior +from kivy.lang import Builder +from kivy.logger import Logger +from kivy.properties import ObjectProperty +from kivy.resources import resource_find from kivy.uix.actionbar import ( ActionBar, - ActionView, ActionButton, - ActionToggleButton, - ActionPrevious, ActionOverflow, + ActionPrevious, ActionSeparator, + ActionToggleButton, + ActionView, ) -from kivy.base import EventLoop -from kivy.core.text import Label as CoreLabel -from kivy.core.image import Image -from kivy.graphics import Color, Line -from kivy.graphics import Rotate, Translate -from kivy.graphics.instructions import InstructionGroup -from kivy.graphics.tesselator import Tesselator -from kivy.graphics.context_instructions import PopMatrix, PushMatrix -from kivy.graphics import StencilPush, StencilPop, StencilUse, StencilUnUse -from kivy.logger import Logger -from kivy.graphics import Mesh -from kivy.resources import resource_find -from kivy.uix.stencilview import StencilView -from kivy.core.window import Window +from kivy.uix.behaviors import FocusBehavior +from kivy.uix.floatlayout import FloatLayout from kivy.uix.popup import Popup -from kivy.properties import ObjectProperty -from kivy.lang import Builder -from kivy.clock import Clock -from distutils.version import LooseVersion +from kivy.uix.stencilview import StencilView +from kivy.uix.widget import Widget -_mpl_ge_1_5 = LooseVersion(matplotlib.__version__) >= LooseVersion("1.5.0") -_mpl_ge_2_0 = LooseVersion(matplotlib.__version__) >= LooseVersion("2.0.0") +kivy.require("1.9.1") -import numpy as np -import textwrap -import uuid -import numbers +_mpl_ge_1_5 = Version(matplotlib.__version__) >= Version("1.5.0") +_mpl_ge_2_0 = Version(matplotlib.__version__) >= Version("2.0.0") -kivy.require("1.9.1") toolbar = None my_canvas = None @@ -347,7 +355,7 @@ class Show(ShowBase): """ @classmethod - def mainloop(self): + def mainloop(cls): app = App.get_running_app() if app is None: app = MPLKivyApp(figure=my_canvas, toolbar=toolbar) @@ -989,8 +997,10 @@ def _init_toolbar(self): """ basedir = os.path.join(rcParams["datapath"], "images") actionview = ActionView() - actionprevious = ActionPrevious(title="Navigation", - with_previous=False) + actionprevious = ActionPrevious( + title="Navigation", + with_previous=False, + ) actionoverflow = ActionOverflow() actionview.add_widget(actionprevious) actionview.add_widget(actionoverflow) @@ -1212,13 +1222,16 @@ def on_touch_down(self, touch): newcoord = self.to_widget(touch.x, touch.y, relative=True) x = newcoord[0] y = newcoord[1] + if super(FigureCanvasKivy, self).on_touch_down(touch): return True if self.collide_point(*touch.pos): self.motion_notify_event(x, y) touch.grab(self) - if 'button' in touch.profile and touch.button in ("scrollup", - "scrolldown"): + if "button" in touch.profile and touch.button in ( + "scrollup", + "scrolldown", + ): self.scroll_event(x, y, 5) else: self.button_press_event(x, y, self.get_mouse_button(touch)) @@ -1269,8 +1282,10 @@ def on_touch_up(self, touch): x = newcoord[0] y = newcoord[1] if touch.grab_current is self: - if 'button' in touch.profile and touch.button in ("scrollup", - "scrolldown"): + if "button" in touch.profile and touch.button in ( + "scrollup", + "scrolldown", + ): self.scroll_event(x, y, 5) else: self.button_release_event(x, y, self.get_mouse_button(touch)) @@ -1283,13 +1298,13 @@ def keyboard_on_key_down(self, window, keycode, text, modifiers): """Kivy event to trigger matplotlib `key_press_event`.""" self.key_press_event(key=keycode[1]) return super(FigureCanvasKivy, self).keyboard_on_key_down( - window, keycode, text, modifiers) + window, keycode, text, modifiers + ) def keyboard_on_key_up(self, window, keycode): """Kivy event to trigger matplotlib `key_release_event`.""" self.key_release_event(key=keycode[1]) - return super(FigureCanvasKivy, self).keyboard_on_key_up( - window, keycode) + return super(FigureCanvasKivy, self).keyboard_on_key_up(window, keycode) def _on_mouse_pos(self, *args): """Kivy Event to trigger the following matplotlib events: @@ -1319,67 +1334,58 @@ def leave_notify_event(self, gui_event=None): self.callbacks.process("figure_leave_event", event) def resize_event(self): - event = ResizeEvent('resize_event', self) - self.callbacks.process('resize_event', event) + event = ResizeEvent("resize_event", self) + self.callbacks.process("resize_event", event) def motion_notify_event(self, x, y, gui_event=None): event = MouseEvent( - 'motion_notify_event', - canvas=self, - x=x, - y=y, - guiEvent=gui_event) - self.callbacks.process('motion_notify_event', event) + "motion_notify_event", canvas=self, x=x, y=y, guiEvent=gui_event + ) + self.callbacks.process("motion_notify_event", event) - def button_press_event(self, x, y, button, - dblclick=False, gui_event=None): + def button_press_event(self, x, y, button, dblclick=False, gui_event=None): event = MouseEvent( - 'button_press_event', + "button_press_event", canvas=self, x=x, y=y, button=button, dblclick=dblclick, - guiEvent=gui_event) - self.callbacks.process('button_press_event', event) + guiEvent=gui_event, + ) + self.callbacks.process("button_press_event", event) - def button_release_event(self, x, y, button, - dblclick=False, gui_event=None): + def button_release_event( + self, x, y, button, dblclick=False, gui_event=None + ): event = MouseEvent( - 'button_release_event', + "button_release_event", canvas=self, x=x, y=y, button=button, dblclick=dblclick, - guiEvent=gui_event) - self.callbacks.process('button_release_event', event) + guiEvent=gui_event, + ) + self.callbacks.process("button_release_event", event) def scroll_event(self, x, y, step, gui_event=None): event = MouseEvent( - 'scroll_event', - canvas=self, - x=x, - y=y, - step=step, - guiEvent=gui_event) - self.callbacks.process('scroll_event', event) + "scroll_event", canvas=self, x=x, y=y, step=step, guiEvent=gui_event + ) + self.callbacks.process("scroll_event", event) def key_press_event(self, key, gui_event=None): event = KeyEvent( - 'key_press_event', - canvas=self, - key=key, - guiEvent=gui_event) - self.callbacks.process('key_press_event', event) + "key_press_event", canvas=self, key=key, guiEvent=gui_event + ) + self.callbacks.process("key_press_event", event) def key_release_event(self, key, gui_event=None): event = KeyEvent( - 'key_release_event', - canvas=self, - key=key, - guiEvent=gui_event) - self.callbacks.process('key_release_event', event) + "key_release_event", canvas=self, key=key, guiEvent=gui_event + ) + self.callbacks.process("key_release_event", event) def _on_pos_changed(self, *args): self.draw() diff --git a/kivy_garden/matplotlib/backend_kivyagg.py b/kivy_garden/matplotlib/backend_kivyagg.py index 8253908..3ccea49 100644 --- a/kivy_garden/matplotlib/backend_kivyagg.py +++ b/kivy_garden/matplotlib/backend_kivyagg.py @@ -71,11 +71,11 @@ def my_callback(event): unicode_literals, ) -__all__ = "FigureCanvasKivyAgg" +__all__ = ("FigureCanvasKivyAgg",) -from matplotlib.figure import Figure +from matplotlib.backend_bases import ShowBase, register_backend from matplotlib.backends.backend_agg import FigureCanvasAgg -from matplotlib.backend_bases import register_backend, ShowBase +from matplotlib.figure import Figure try: import kivy @@ -83,12 +83,13 @@ def my_callback(event): raise ImportError("this backend requires Kivy to be installed.") from kivy.app import App +from kivy.base import EventLoop +from kivy.core.image import Image +from kivy.graphics import Color, Rectangle from kivy.graphics.texture import Texture -from kivy.graphics import Rectangle, Color from kivy.properties import ObjectProperty -from kivy.base import EventLoop from kivy.uix.floatlayout import FloatLayout -from kivy.core.image import Image + from kivy_garden.matplotlib.backend_kivy import ( FigureCanvasKivy, FigureManagerKivy, @@ -152,7 +153,7 @@ class Show(ShowBase): """ @classmethod - def mainloop(self): + def mainloop(cls): global my_canvas global toolbar app = App.get_running_app() diff --git a/kivy_garden/matplotlib/tests/test_backend_kivy.py b/kivy_garden/matplotlib/tests/test_backend_kivy.py index 02d1b9f..7544353 100644 --- a/kivy_garden/matplotlib/tests/test_backend_kivy.py +++ b/kivy_garden/matplotlib/tests/test_backend_kivy.py @@ -2,7 +2,7 @@ def test_matplotlib_use_backend(): import matplotlib import matplotlib.pyplot as plt - plt.close('all') + plt.close("all") matplotlib.use("module://kivy_garden.matplotlib.backend_kivy") plt.plot([1, 2, 3, 4]) diff --git a/kivy_garden/matplotlib/tests/test_backend_kivyagg.py b/kivy_garden/matplotlib/tests/test_backend_kivyagg.py index 6a501a3..f389c1f 100644 --- a/kivy_garden/matplotlib/tests/test_backend_kivyagg.py +++ b/kivy_garden/matplotlib/tests/test_backend_kivyagg.py @@ -2,7 +2,7 @@ def test_matplotlib_use_backend(): import matplotlib import matplotlib.pyplot as plt - plt.close('all') + plt.close("all") matplotlib.use("module://kivy_garden.matplotlib.backend_kivyagg") plt.plot([1, 2, 3, 4]) diff --git a/kivy_garden/matplotlib/tests/test_events.py b/kivy_garden/matplotlib/tests/test_events.py index f41ea76..32db16e 100644 --- a/kivy_garden/matplotlib/tests/test_events.py +++ b/kivy_garden/matplotlib/tests/test_events.py @@ -5,8 +5,8 @@ class TestEvents(GraphicUnitTest): @classmethod def setUpClass(cls): - from matplotlib.figure import Figure from kivy_garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg + from matplotlib.figure import Figure fig = Figure() ax = fig.add_subplot(111) @@ -23,15 +23,15 @@ def test_touch_down(self): def test_touch_down_scrolldown(self): self.render(self.figure_canvas) touch = UnitTestTouch(x=0, y=0) - touch.profile = 'button' - touch.button = 'scrolldown' + touch.profile = "button" + touch.button = "scrolldown" touch.touch_down() def test_touch_down_scrolluo(self): self.render(self.figure_canvas) touch = UnitTestTouch(x=0, y=0) - touch.profile = 'button' - touch.button = 'scrollup' + touch.profile = "button" + touch.button = "scrollup" touch.touch_down() def test_touch_up(self): @@ -43,16 +43,16 @@ def test_touch_up(self): def test_touch_up_scrolldown(self): self.render(self.figure_canvas) touch = UnitTestTouch(x=0, y=0) - touch.profile = 'button' - touch.button = 'scrolldown' + touch.profile = "button" + touch.button = "scrolldown" touch.touch_down() touch.touch_up() def test_touch_up_scrolluo(self): self.render(self.figure_canvas) touch = UnitTestTouch(x=0, y=0) - touch.profile = 'button' - touch.button = 'scrollup' + touch.profile = "button" + touch.button = "scrollup" touch.touch_down() touch.touch_up() @@ -66,12 +66,13 @@ def test_on_key_down(self): EventLoop.ensure_window() window = EventLoop.window - key = 'tab' + key = "tab" key_code = (key, Keyboard.keycodes[key]) print(Keyboard.keycodes) self.figure_canvas.keyboard_on_key_down( - window, key_code, text='tab', modifiers=[]) + window, key_code, text="tab", modifiers=[] + ) def test_on_key_up(self): from kivy.base import EventLoop @@ -79,8 +80,7 @@ def test_on_key_up(self): EventLoop.ensure_window() window = EventLoop.window - key = 'tab' + key = "tab" key_code = (key, Keyboard.keycodes[key]) print(Keyboard.keycodes) - self.figure_canvas.keyboard_on_key_up( - window, key_code) + self.figure_canvas.keyboard_on_key_up(window, key_code) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..108ce81 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,10 @@ +[tool.isort] +profile = "black" +src_paths = ["kivy_garden"] +force_sort_within_sections=true +line_length=80 +multi_line_output=3 + +[tool.black] +line-length=80 +target-version=['py37','py38','py39','py310','py311'] diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..889831c --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +filterwarnings = ignore::DeprecationWarning:(?!kivy_garden.matplotlib) \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index a436359..460b529 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,7 +6,10 @@ extend-ignore = E126, E127, E128, + E203, E402, + E701, + E704, E731, E741, F401, diff --git a/setup.py b/setup.py index 54db180..ec262c7 100644 --- a/setup.py +++ b/setup.py @@ -42,7 +42,10 @@ keywords='Kivy kivy-garden', packages=find_namespace_packages(include=['kivy_garden.*']), - install_requires=['matplotlib>=3.7.2', 'kivy>=2.1.0'], + install_requires=[ + 'matplotlib>=3', + 'kivy>=1.9.1' + ], extras_require={ 'dev': ['pytest>=3.6', 'pytest-cov', 'pytest-asyncio', 'sphinx_rtd_theme'],