[nautilus-python] examples: Add type annotations to methods
- From: Jan Tojnar <jtojnar src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [nautilus-python] examples: Add type annotations to methods
- Date: Fri, 12 Aug 2022 11:35:59 +0000 (UTC)
commit 3cfb7b8f3454a419eea23abcfe784c8a8842d10e
Author: Jan Tojnar <jtojnar gmail com>
Date: Fri Aug 12 05:22:21 2022 +0200
examples: Add type annotations to methods
This should make it clearer what each function does, even though they are not checked.
Also switched the tuples for proper lists since the API expects `GList`s.
examples/TestExtension.py | 29 +++++++++++++++++++------
examples/background-image.py | 34 +++++++++++++++++++++--------
examples/block-size-column.py | 10 ++++++---
examples/md5sum-property-page.py | 16 +++++++++-----
examples/open-terminal.py | 47 ++++++++++++++++++++++++++++++----------
examples/submenu.py | 23 +++++++++++++++-----
6 files changed, 117 insertions(+), 42 deletions(-)
---
diff --git a/examples/TestExtension.py b/examples/TestExtension.py
index cbd9a63..b2fde59 100644
--- a/examples/TestExtension.py
+++ b/examples/TestExtension.py
@@ -1,4 +1,5 @@
-from gi.repository import Nautilus, GObject
+from gi.repository import Nautilus, GObject, Gtk
+from typing import List
class TestExtension(GObject.GObject, Nautilus.MenuProvider):
@@ -6,12 +7,20 @@ class TestExtension(GObject.GObject, Nautilus.MenuProvider):
super().__init__()
print("Initialized test extension")
- def menu_activate_cb(self, menu, file):
+ def menu_activate_cb(
+ self,
+ menu: Nautilus.MenuItem,
+ file: Nautilus.FileInfo,
+ ) -> None:
print("menu_activate_cb", file)
- def get_file_items(self, window, files):
+ def get_file_items(
+ self,
+ window: Gtk.Widget,
+ files: List[Nautilus.FileInfo],
+ ) -> List[Nautilus.MenuItem]:
if len(files) != 1:
- return
+ return []
file = files[0]
@@ -22,9 +31,15 @@ class TestExtension(GObject.GObject, Nautilus.MenuProvider):
)
item.connect("activate", self.menu_activate_cb, file)
- return [item]
+ return [
+ item,
+ ]
# Even though we're not using background items, Nautilus will generate
# a warning if the method isn't present
- def get_background_items(self, window, file):
- return None
+ def get_background_items(
+ self,
+ window: Gtk.Widget,
+ current_folder: Nautilus.FileInfo,
+ ) -> List[Nautilus.MenuItem]:
+ return []
diff --git a/examples/background-image.py b/examples/background-image.py
index 6fb120d..7be2e29 100644
--- a/examples/background-image.py
+++ b/examples/background-image.py
@@ -1,4 +1,5 @@
-from gi.repository import Nautilus, GObject, Gio
+from gi.repository import Nautilus, GObject, Gio, Gtk
+from typing import List
SUPPORTED_FORMATS = "image/jpeg", "image/png"
BACKGROUND_SCHEMA = "org.gnome.desktop.background"
@@ -10,26 +11,34 @@ class BackgroundImageExtension(GObject.GObject, Nautilus.MenuProvider):
super().__init__()
self.bgsettings = Gio.Settings.new(BACKGROUND_SCHEMA)
- def menu_activate_cb(self, menu, file):
+ def menu_activate_cb(
+ self,
+ menu: Nautilus.MenuItem,
+ file: Nautilus.FileInfo,
+ ) -> None:
if file.is_gone():
return
self.bgsettings[BACKGROUND_KEY] = file.get_uri()
- def get_file_items(self, window, files):
+ def get_file_items(
+ self,
+ window: Gtk.Widget,
+ files: List[Nautilus.FileInfo],
+ ) -> List[Nautilus.MenuItem]:
if len(files) != 1:
- return
+ return []
file = files[0]
# We're only going to put ourselves on images context menus
if not file.get_mime_type() in SUPPORTED_FORMATS:
- return
+ return []
# Gnome can only handle file:
# In the future we might want to copy the file locally
if file.get_uri_scheme() != "file":
- return
+ return []
item = Nautilus.MenuItem(
name="Nautilus::set_background_image",
@@ -37,9 +46,16 @@ class BackgroundImageExtension(GObject.GObject, Nautilus.MenuProvider):
tip="Set the current image as a background image",
)
item.connect("activate", self.menu_activate_cb, file)
- return (item,)
+
+ return [
+ item,
+ ]
# Current versions of Nautilus will throw a warning if get_background_items
# isn't present
- def get_background_items(self, window, file):
- return None
+ def get_background_items(
+ self,
+ window: Gtk.Widget,
+ current_folder: Nautilus.FileInfo,
+ ) -> List[Nautilus.MenuItem]:
+ return []
diff --git a/examples/block-size-column.py b/examples/block-size-column.py
index 251fff3..0607e8d 100644
--- a/examples/block-size-column.py
+++ b/examples/block-size-column.py
@@ -1,19 +1,23 @@
import os
from urllib.parse import unquote
from gi.repository import GObject, Nautilus
+from typing import List
class ColumnExtension(GObject.GObject, Nautilus.ColumnProvider, Nautilus.InfoProvider):
- def get_columns(self):
+ def get_columns(self) -> List[Nautilus.Column]:
column = Nautilus.Column(
name="NautilusPython::block_size_column",
attribute="block_size",
label="Block size",
description="Get the block size",
)
- return (column,)
- def update_file_info(self, file):
+ return [
+ column,
+ ]
+
+ def update_file_info(self, file: Nautilus.FileInfo) -> None:
if file.get_uri_scheme() != "file":
return
diff --git a/examples/md5sum-property-page.py b/examples/md5sum-property-page.py
index 87332d0..46cf01c 100644
--- a/examples/md5sum-property-page.py
+++ b/examples/md5sum-property-page.py
@@ -1,19 +1,23 @@
import hashlib
from urllib.parse import unquote
from gi.repository import Nautilus, Gtk, GObject
+from typing import List
class MD5SumPropertyPage(GObject.GObject, Nautilus.PropertyPageProvider):
- def get_property_pages(self, files):
+ def get_property_pages(
+ self,
+ files: List[Nautilus.FileInfo],
+ ) -> List[Nautilus.PropertyPage]:
if len(files) != 1:
- return
+ return []
file = files[0]
if file.get_uri_scheme() != "file":
- return
+ return []
if file.is_directory():
- return
+ return []
filename = unquote(file.get_uri()[7:])
@@ -34,10 +38,10 @@ class MD5SumPropertyPage(GObject.GObject, Nautilus.PropertyPageProvider):
self.value_label.set_text(md5sum)
self.value_label.show()
- return (
+ return [
Nautilus.PropertyPage(
name="NautilusPython::md5_sum",
label=self.property_label,
page=self.hbox,
),
- )
+ ]
diff --git a/examples/open-terminal.py b/examples/open-terminal.py
index 7a4b4ed..074f1fd 100644
--- a/examples/open-terminal.py
+++ b/examples/open-terminal.py
@@ -1,29 +1,42 @@
# This example is contributed by Martin Enlund
import os
from urllib.parse import unquote
-from gi.repository import Nautilus, GObject
+from gi.repository import Nautilus, GObject, Gtk
+from typing import List
class OpenTerminalExtension(GObject.GObject, Nautilus.MenuProvider):
- def _open_terminal(self, file):
+ def _open_terminal(self, file: Nautilus.FileInfo) -> None:
filename = unquote(file.get_uri()[7:])
os.chdir(filename)
os.system("gnome-terminal")
- def menu_activate_cb(self, menu, file):
+ def menu_activate_cb(
+ self,
+ menu: Nautilus.MenuItem,
+ file: Nautilus.FileInfo,
+ ) -> None:
self._open_terminal(file)
- def menu_background_activate_cb(self, menu, file):
+ def menu_background_activate_cb(
+ self,
+ menu: Nautilus.MenuItem,
+ file: Nautilus.FileInfo,
+ ) -> None:
self._open_terminal(file)
- def get_file_items(self, window, files):
+ def get_file_items(
+ self,
+ window: Gtk.Widget,
+ files: List[Nautilus.FileInfo],
+ ) -> List[Nautilus.MenuItem]:
if len(files) != 1:
- return
+ return []
file = files[0]
if not file.is_directory() or file.get_uri_scheme() != "file":
- return
+ return []
item = Nautilus.MenuItem(
name="NautilusPython::openterminal_file_item",
@@ -31,13 +44,23 @@ class OpenTerminalExtension(GObject.GObject, Nautilus.MenuProvider):
tip="Open Terminal In %s" % file.get_name(),
)
item.connect("activate", self.menu_activate_cb, file)
- return (item,)
- def get_background_items(self, window, file):
+ return [
+ item,
+ ]
+
+ def get_background_items(
+ self,
+ window: Gtk.Widget,
+ current_folder: Nautilus.FileInfo,
+ ) -> List[Nautilus.MenuItem]:
item = Nautilus.MenuItem(
name="NautilusPython::openterminal_file_item2",
label="Open Terminal",
- tip="Open Terminal In %s" % file.get_name(),
+ tip="Open Terminal In %s" % current_folder.get_name(),
)
- item.connect("activate", self.menu_background_activate_cb, file)
- return (item,)
+ item.connect("activate", self.menu_background_activate_cb, current_folder)
+
+ return [
+ item,
+ ]
diff --git a/examples/submenu.py b/examples/submenu.py
index 2d893a4..e8924b8 100644
--- a/examples/submenu.py
+++ b/examples/submenu.py
@@ -1,8 +1,13 @@
-from gi.repository import Nautilus, GObject
+from gi.repository import Nautilus, GObject, Gtk
+from typing import List
class ExampleMenuProvider(GObject.GObject, Nautilus.MenuProvider):
- def get_file_items(self, window, files):
+ def get_file_items(
+ self,
+ window: Gtk.Widget,
+ files: List[Nautilus.FileInfo],
+ ) -> List[Nautilus.MenuItem]:
top_menuitem = Nautilus.MenuItem(
name="ExampleMenuProvider::Foo",
label="Foo",
@@ -21,9 +26,15 @@ class ExampleMenuProvider(GObject.GObject, Nautilus.MenuProvider):
)
submenu.append_item(sub_menuitem)
- return (top_menuitem,)
+ return [
+ top_menuitem,
+ ]
- def get_background_items(self, window, file):
+ def get_background_items(
+ self,
+ window: Gtk.Widget,
+ current_folder: Nautilus.FileInfo,
+ ) -> List[Nautilus.MenuItem]:
submenu = Nautilus.Menu()
submenu.append_item(
Nautilus.MenuItem(
@@ -42,4 +53,6 @@ class ExampleMenuProvider(GObject.GObject, Nautilus.MenuProvider):
)
menuitem.set_submenu(submenu)
- return (menuitem,)
+ return [
+ menuitem,
+ ]
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]