Skip to content
Permalink
Browse files

Examples: remove use of Python 2/3 incompatible urllib/urlparse.

Plus some other rewriting to improve lisibility.
  • Loading branch information...
Patrick Monnerat authored and monsta committed Feb 11, 2019
1 parent 05c5dd5 commit 7842b86e8deac7b6cae3bea3d27b26ae7a399db0
Showing with 15 additions and 69 deletions.
  1. +4 −21 examples/background-image.py
  2. +1 −8 examples/block-size-column.py
  3. +1 −8 examples/md5sum-property-page.py
  4. +6 −22 examples/mixed.py
  5. +3 −10 examples/open-terminal.py
@@ -2,36 +2,19 @@
BACKGROUND_SCHEMA = 'org.mate.background'
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] = self._filepath(file)

if file.get_uri_scheme() == 'file':
self.bgsettings[BACKGROUND_KEY] = file.get_location().get_path()

def get_file_items(self, window, files):
if len(files) != 1:
@@ -52,7 +35,7 @@ def get_file_items(self, window, files):
label='Use as background image',
tip='Set the current image as a background image')
item.connect('activate', self.menu_activate_cb, file)
return item,
return [item]

def get_background_items(self, window, file):
return []
@@ -1,12 +1,5 @@
import os

try:
# Python 3.
from urllib.parse import unquote
except:
# Python 2.
from urllib import unquote

from gi.repository import GObject, Caja

class ColumnExtension(GObject.GObject, Caja.ColumnProvider, Caja.InfoProvider):
@@ -23,6 +16,6 @@ def update_file_info(self, file):
if file.get_uri_scheme() != 'file':
return

filename = unquote(file.get_uri()[7:])
filename = file.get_location().get_path()

file.add_string_attribute('block_size', str(os.stat(filename).st_blksize))
@@ -1,12 +1,5 @@
import hashlib

try:
# Python 3.
from urllib.parse import unquote
except:
# Python 2.
from urllib import unquote

from gi.repository import Caja, Gtk, GObject

class MD5SumPropertyPage(GObject.GObject, Caja.PropertyPageProvider):
@@ -24,7 +17,7 @@ def get_property_pages(self, files):
if file.is_directory():
return

filename = unquote(file.get_uri()[7:])
filename = file.get_location().get_path()

self.property_label = Gtk.Label('MD5Sum')
self.property_label.show()
@@ -8,14 +8,6 @@

import os

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, Gtk


@@ -30,16 +22,8 @@ class Mixed(GObject.GObject,

# Private methods.

def _basename(self, uri):
try:
uri = uri.get_uri() # In case a CajaFile is given.
except:
pass
(scheme, netloc, path, parameters, query, fragment) = urlparse(uri)
return os.path.basename(unquote(path))

def _file_has_mixed_name(self, cajafile):
name = self._basename(cajafile)
name = cajafile.get_name()
if name.upper() != name and name.lower() != name:
return 'mixed'
return ''
@@ -72,7 +56,7 @@ def get_file_items(self, window, cajafiles):
for cajafile in cajafiles:
mixed = cajafile.get_string_attribute('mixed')
if mixed:
filename = self._basename(cajafile)
filename = cajafile.get_name()
menuitem = Caja.MenuItem(
name = 'Mixed::FileMenu',
label = 'Mixed: %s has a mixed case name' % filename,
@@ -107,7 +91,7 @@ def get_property_pages(self, cajafiles):
page_label.show()
hbox = Gtk.HBox(homogeneous = False, spacing = 4)
hbox.show()
name_label = Gtk.Label(self._basename(cajafile))
name_label = Gtk.Label(cajafile.get_name())
name_label.show()
comment_label = Gtk.Label('has a mixed-case name')
comment_label.show()
@@ -126,9 +110,9 @@ def get_property_pages(self, cajafiles):
# Caja.LocationWidgetProvider implementation.

def get_widget(self, uri, window):
filename = self._basename(uri)
if not self._file_has_mixed_name(filename):
cajafile = Caja.FileInfo.create_for_uri(uri)
if not self._file_has_mixed_name(cajafile):
return None
label = Gtk.Label('In mixed-case directory %s' % filename)
label = Gtk.Label('In mixed-case directory ' + cajafile.get_name())
label.show()
return label
@@ -1,13 +1,6 @@
# This example is contributed by Martin Enlund
import os

try:
# Python 3.
from urllib.parse import unquote
except:
# Python 2.
from urllib import unquote

from gi.repository import Caja, GObject, Gio

TERMINAL_SCHEMA = 'org.mate.applications-terminal'
@@ -18,7 +11,7 @@ def __init__(self):
self.gsettings = Gio.Settings.new(TERMINAL_SCHEMA)

def _open_terminal(self, file):
filename = unquote(file.get_uri()[7:])
filename = file.get_location().get_path()
terminal = self.gsettings[TERMINAL_KEY]

os.chdir(filename)
@@ -42,11 +35,11 @@ def get_file_items(self, window, files):
label='Open Terminal' ,
tip='Open Terminal In %s' % file.get_name())
item.connect('activate', self.menu_activate_cb, file)
return item,
return [item]

def get_background_items(self, window, file):
item = Caja.MenuItem(name='CajaPython::openterminal_item',
label='Open Terminal Here',
tip='Open Terminal In This Directory')
item.connect('activate', self.menu_background_activate_cb, file)
return item,
return [item]

0 comments on commit 7842b86

Please sign in to comment.
You can’t perform that action at this time.