Skip to content

Commit 7842b86

Browse files
Patrick Monneratmonsta
authored andcommitted
Examples: remove use of Python 2/3 incompatible urllib/urlparse.
Plus some other rewriting to improve lisibility.
1 parent 05c5dd5 commit 7842b86

File tree

5 files changed

+15
-69
lines changed

5 files changed

+15
-69
lines changed

‎examples/background-image.py‎

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,19 @@
22
BACKGROUND_SCHEMA = 'org.mate.background'
33
BACKGROUND_KEY = 'picture-filename'
44

5-
try:
6-
# Python 3.
7-
from urllib.parse import unquote, urlparse
8-
except:
9-
# Python 2.
10-
from urllib import unquote
11-
from urlparse import urlparse
12-
135
from gi.repository import Caja, GObject, Gio
146

157

168
class BackgroundImageExtension(GObject.GObject, Caja.MenuProvider):
179
def __init__(self):
1810
self.bgsettings = Gio.Settings.new(BACKGROUND_SCHEMA)
1911

20-
def _filepath(self, file):
21-
try:
22-
file = file.get_uri()
23-
except:
24-
pass
25-
(scheme, netloc, path, parameters, query, fragment) = urlparse(file)
26-
if scheme and unquote(scheme) != 'file':
27-
return None
28-
return unquote(path)
29-
3012
def menu_activate_cb(self, menu, file):
3113
if file.is_gone():
3214
return
33-
34-
self.bgsettings[BACKGROUND_KEY] = self._filepath(file)
15+
16+
if file.get_uri_scheme() == 'file':
17+
self.bgsettings[BACKGROUND_KEY] = file.get_location().get_path()
3518

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

5740
def get_background_items(self, window, file):
5841
return []

‎examples/block-size-column.py‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import os
22

3-
try:
4-
# Python 3.
5-
from urllib.parse import unquote
6-
except:
7-
# Python 2.
8-
from urllib import unquote
9-
103
from gi.repository import GObject, Caja
114

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

26-
filename = unquote(file.get_uri()[7:])
19+
filename = file.get_location().get_path()
2720

2821
file.add_string_attribute('block_size', str(os.stat(filename).st_blksize))

‎examples/md5sum-property-page.py‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import hashlib
22

3-
try:
4-
# Python 3.
5-
from urllib.parse import unquote
6-
except:
7-
# Python 2.
8-
from urllib import unquote
9-
103
from gi.repository import Caja, Gtk, GObject
114

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

27-
filename = unquote(file.get_uri()[7:])
20+
filename = file.get_location().get_path()
2821

2922
self.property_label = Gtk.Label('MD5Sum')
3023
self.property_label.show()

‎examples/mixed.py‎

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@
88

99
import os
1010

11-
try:
12-
# Python 3.
13-
from urllib.parse import unquote, urlparse
14-
except:
15-
# Python 2.
16-
from urllib import unquote
17-
from urlparse import urlparse
18-
1911
from gi.repository import Caja, GObject, Gtk
2012

2113

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

3123
# Private methods.
3224

33-
def _basename(self, uri):
34-
try:
35-
uri = uri.get_uri() # In case a CajaFile is given.
36-
except:
37-
pass
38-
(scheme, netloc, path, parameters, query, fragment) = urlparse(uri)
39-
return os.path.basename(unquote(path))
40-
4125
def _file_has_mixed_name(self, cajafile):
42-
name = self._basename(cajafile)
26+
name = cajafile.get_name()
4327
if name.upper() != name and name.lower() != name:
4428
return 'mixed'
4529
return ''
@@ -72,7 +56,7 @@ def get_file_items(self, window, cajafiles):
7256
for cajafile in cajafiles:
7357
mixed = cajafile.get_string_attribute('mixed')
7458
if mixed:
75-
filename = self._basename(cajafile)
59+
filename = cajafile.get_name()
7660
menuitem = Caja.MenuItem(
7761
name = 'Mixed::FileMenu',
7862
label = 'Mixed: %s has a mixed case name' % filename,
@@ -107,7 +91,7 @@ def get_property_pages(self, cajafiles):
10791
page_label.show()
10892
hbox = Gtk.HBox(homogeneous = False, spacing = 4)
10993
hbox.show()
110-
name_label = Gtk.Label(self._basename(cajafile))
94+
name_label = Gtk.Label(cajafile.get_name())
11195
name_label.show()
11296
comment_label = Gtk.Label('has a mixed-case name')
11397
comment_label.show()
@@ -126,9 +110,9 @@ def get_property_pages(self, cajafiles):
126110
# Caja.LocationWidgetProvider implementation.
127111

128112
def get_widget(self, uri, window):
129-
filename = self._basename(uri)
130-
if not self._file_has_mixed_name(filename):
113+
cajafile = Caja.FileInfo.create_for_uri(uri)
114+
if not self._file_has_mixed_name(cajafile):
131115
return None
132-
label = Gtk.Label('In mixed-case directory %s' % filename)
116+
label = Gtk.Label('In mixed-case directory ' + cajafile.get_name())
133117
label.show()
134118
return label

‎examples/open-terminal.py‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
# This example is contributed by Martin Enlund
22
import os
33

4-
try:
5-
# Python 3.
6-
from urllib.parse import unquote
7-
except:
8-
# Python 2.
9-
from urllib import unquote
10-
114
from gi.repository import Caja, GObject, Gio
125

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

2013
def _open_terminal(self, file):
21-
filename = unquote(file.get_uri()[7:])
14+
filename = file.get_location().get_path()
2215
terminal = self.gsettings[TERMINAL_KEY]
2316

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

4740
def get_background_items(self, window, file):
4841
item = Caja.MenuItem(name='CajaPython::openterminal_item',
4942
label='Open Terminal Here',
5043
tip='Open Terminal In This Directory')
5144
item.connect('activate', self.menu_background_activate_cb, file)
52-
return item,
45+
return [item]

0 commit comments

Comments
 (0)