[nautilus-python] examples: Call GObject constructor in our constructors



commit 991754d695a2087255bfa5f125ae9cf3296e7701
Author: Jan Tojnar <jtojnar gmail com>
Date:   Fri Aug 12 04:23:37 2022 +0200

    examples: Call GObject constructor in our constructors
    
    When overriding constructors, we need to call the parent constructor, otherwise GObject will not be 
properly initialized:
    
    >>> from importlib import import_module; ufia = import_module('examples.update-file-info-async')
    >>> ufia.UpdateFileInfoAsync()
    <update-file-info-async.UpdateFileInfoAsync object at 0x7f5fe326b5c0 (uninitialized at 0x(nil))>
    
    
    Furthermore, we can drop the constructors where we are not initializing any extra properties –
    then the constructor of the parent class (GObject will be called).
    
    We also need to ensure that GObject is the first parent class listed since otherwise,
    Python will try to call the constructor of the provider, which does not exist since it is an abstract 
class.
    
    We will keep it for TestExtension since it is the first example people reading the docs will see
    and hopefully that will reduce the amount of forgotten parent constructor calls.
    
    
    Noticed in https://github.com/linuxmint/nemo-extensions/commit/67a5d42489a1ae1715f620f281b62b8cc444d2d0

 examples/TestExtension.py            | 3 ++-
 examples/background-image.py         | 1 +
 examples/block-size-column.py        | 3 ---
 examples/location-widget-provider.py | 3 ---
 examples/md5sum-property-page.py     | 3 ---
 examples/open-terminal.py            | 5 +----
 examples/submenu.py                  | 3 ---
 examples/update-file-info-async.py   | 1 +
 8 files changed, 5 insertions(+), 17 deletions(-)
---
diff --git a/examples/TestExtension.py b/examples/TestExtension.py
index 2294a8c..cbd9a63 100644
--- a/examples/TestExtension.py
+++ b/examples/TestExtension.py
@@ -3,7 +3,8 @@ from gi.repository import Nautilus, GObject
 
 class TestExtension(GObject.GObject, Nautilus.MenuProvider):
     def __init__(self):
-        pass
+        super().__init__()
+        print("Initialized test extension")
 
     def menu_activate_cb(self, menu, file):
         print("menu_activate_cb", file)
diff --git a/examples/background-image.py b/examples/background-image.py
index 20371b5..6fb120d 100644
--- a/examples/background-image.py
+++ b/examples/background-image.py
@@ -7,6 +7,7 @@ BACKGROUND_KEY = "picture-uri"
 
 class BackgroundImageExtension(GObject.GObject, Nautilus.MenuProvider):
     def __init__(self):
+        super().__init__()
         self.bgsettings = Gio.Settings.new(BACKGROUND_SCHEMA)
 
     def menu_activate_cb(self, menu, file):
diff --git a/examples/block-size-column.py b/examples/block-size-column.py
index 6ded93e..251fff3 100644
--- a/examples/block-size-column.py
+++ b/examples/block-size-column.py
@@ -4,9 +4,6 @@ from gi.repository import GObject, Nautilus
 
 
 class ColumnExtension(GObject.GObject, Nautilus.ColumnProvider, Nautilus.InfoProvider):
-    def __init__(self):
-        pass
-
     def get_columns(self):
         column = Nautilus.Column(
             name="NautilusPython::block_size_column",
diff --git a/examples/location-widget-provider.py b/examples/location-widget-provider.py
index 84fff0d..005c7c1 100644
--- a/examples/location-widget-provider.py
+++ b/examples/location-widget-provider.py
@@ -2,9 +2,6 @@ from gi.repository import Nautilus, Gtk, GObject
 
 
 class LocationProviderExample(GObject.GObject, Nautilus.LocationWidgetProvider):
-    def __init__(self):
-        pass
-
     def get_widget(self, uri, window):
         entry = Gtk.Entry()
         entry.set_text(uri)
diff --git a/examples/md5sum-property-page.py b/examples/md5sum-property-page.py
index a441394..87332d0 100644
--- a/examples/md5sum-property-page.py
+++ b/examples/md5sum-property-page.py
@@ -4,9 +4,6 @@ from gi.repository import Nautilus, Gtk, GObject
 
 
 class MD5SumPropertyPage(GObject.GObject, Nautilus.PropertyPageProvider):
-    def __init__(self):
-        pass
-
     def get_property_pages(self, files):
         if len(files) != 1:
             return
diff --git a/examples/open-terminal.py b/examples/open-terminal.py
index 0647203..7a4b4ed 100644
--- a/examples/open-terminal.py
+++ b/examples/open-terminal.py
@@ -4,10 +4,7 @@ from urllib.parse import unquote
 from gi.repository import Nautilus, GObject
 
 
-class OpenTerminalExtension(Nautilus.MenuProvider, GObject.GObject):
-    def __init__(self):
-        pass
-
+class OpenTerminalExtension(GObject.GObject, Nautilus.MenuProvider):
     def _open_terminal(self, file):
         filename = unquote(file.get_uri()[7:])
 
diff --git a/examples/submenu.py b/examples/submenu.py
index 6fcadf6..2d893a4 100644
--- a/examples/submenu.py
+++ b/examples/submenu.py
@@ -2,9 +2,6 @@ from gi.repository import Nautilus, GObject
 
 
 class ExampleMenuProvider(GObject.GObject, Nautilus.MenuProvider):
-    def __init__(self):
-        pass
-
     def get_file_items(self, window, files):
         top_menuitem = Nautilus.MenuItem(
             name="ExampleMenuProvider::Foo",
diff --git a/examples/update-file-info-async.py b/examples/update-file-info-async.py
index 613d35b..7e54ecb 100644
--- a/examples/update-file-info-async.py
+++ b/examples/update-file-info-async.py
@@ -3,6 +3,7 @@ from gi.repository import Nautilus, GObject
 
 class UpdateFileInfoAsync(GObject.GObject, Nautilus.InfoProvider):
     def __init__(self):
+        super().__init__()
         self.timers = []
         pass
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]