diff options
author | Patrick Monnerat <[email protected]> | 2019-01-26 15:22:58 +0100 |
---|---|---|
committer | Victor Kareh <[email protected]> | 2019-01-30 15:59:07 -0500 |
commit | 6447d7c97a71521f2cdc0765a81c20aec8b90e04 (patch) | |
tree | a9c36cae6fb6e7ef26b52845cd36c32fe87f07fd /examples/background-image.py | |
parent | a6a1bed0d97285ad9fabff9a5116f5b6a0e2c000 (diff) | |
download | python-caja-6447d7c97a71521f2cdc0765a81c20aec8b90e04.tar.bz2 python-caja-6447d7c97a71521f2cdc0765a81c20aec8b90e04.tar.xz |
Modernize examples
The example extension scripts need to be in sync with new versions of
referenced foreign packages.
This commit also makes them compatible with Python version 3 (retaining
Python 2 compatibility).
An additional example extension "mixed" is added: it implements all caja
extension features and can also be used as a new extension pattern. See
source header comment for a description.
Ref: https://github.com/mate-desktop/python-caja/pull/34#issuecomment-457257832
Diffstat (limited to 'examples/background-image.py')
-rw-r--r-- | examples/background-image.py | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/examples/background-image.py b/examples/background-image.py index 3ea0e3c..450b985 100644 --- a/examples/background-image.py +++ b/examples/background-image.py @@ -1,18 +1,37 @@ -from gi.repository import Caja, GObject, Gio - SUPPORTED_FORMATS = 'image/jpeg', 'image/png' BACKGROUND_SCHEMA = 'org.mate.background' -BACKGROUND_KEY = 'picture-uri' +BACKGROUND_KEY = 'picture-filename' + +try: + # Python 3. + from urllib.parse import unquote, urlparse +except: + # Python 2. + from urllib import unquote + from urlparse import urlparse + +from gi.repository import Caja, GObject, Gio + class BackgroundImageExtension(GObject.GObject, Caja.MenuProvider): def __init__(self): self.bgsettings = Gio.Settings.new(BACKGROUND_SCHEMA) + def _filepath(self, file): + try: + file = file.get_uri() + except: + pass + (scheme, netloc, path, parameters, query, fragment) = urlparse(file) + if scheme and unquote(scheme) != 'file': + return None + return unquote(path) + def menu_activate_cb(self, menu, file): if file.is_gone(): return - self.bgsettings[BACKGROUND_KEY] = file.get_uri() + self.bgsettings[BACKGROUND_KEY] = self._filepath(file) def get_file_items(self, window, files): if len(files) != 1: |