Skip to content

Commit 6447d7c

Browse files
monneratvkareh
authored andcommitted
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: #34 (comment)
1 parent a6a1bed commit 6447d7c

File tree

7 files changed

+188
-13
lines changed

7 files changed

+188
-13
lines changed

examples/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ examples_DATA = \
88
location-widget-provider.py \
99
open-terminal.py \
1010
md5sum-property-page.py \
11+
mixed.py \
1112
submenu.py \
1213
update-file-info-async.py
1314

examples/background-image.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
1-
from gi.repository import Caja, GObject, Gio
2-
31
SUPPORTED_FORMATS = 'image/jpeg', 'image/png'
42
BACKGROUND_SCHEMA = 'org.mate.background'
5-
BACKGROUND_KEY = 'picture-uri'
3+
BACKGROUND_KEY = 'picture-filename'
4+
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+
13+
from gi.repository import Caja, GObject, Gio
14+
615

716
class BackgroundImageExtension(GObject.GObject, Caja.MenuProvider):
817
def __init__(self):
918
self.bgsettings = Gio.Settings.new(BACKGROUND_SCHEMA)
1019

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+
1130
def menu_activate_cb(self, menu, file):
1231
if file.is_gone():
1332
return
1433

15-
self.bgsettings[BACKGROUND_KEY] = file.get_uri()
34+
self.bgsettings[BACKGROUND_KEY] = self._filepath(file)
1635

1736
def get_file_items(self, window, files):
1837
if len(files) != 1:

examples/block-size-column.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import os
2-
import urllib
2+
3+
try:
4+
# Python 3.
5+
from urllib.parse import unquote
6+
except:
7+
# Python 2.
8+
from urllib import unquote
39

410
from gi.repository import GObject, Caja
511

@@ -17,6 +23,6 @@ def update_file_info(self, file):
1723
if file.get_uri_scheme() != 'file':
1824
return
1925

20-
filename = urllib.unquote(file.get_uri()[7:])
26+
filename = unquote(file.get_uri()[7:])
2127

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

examples/md5sum-property-page.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import hashlib
2-
import urllib
2+
3+
try:
4+
# Python 3.
5+
from urllib.parse import unquote
6+
except:
7+
# Python 2.
8+
from urllib import unquote
39

410
from gi.repository import Caja, Gtk, GObject
511

@@ -18,7 +24,7 @@ def get_property_pages(self, files):
1824
if file.is_directory():
1925
return
2026

21-
filename = urllib.unquote(file.get_uri()[7:])
27+
filename = unquote(file.get_uri()[7:])
2228

2329
self.property_label = Gtk.Label('MD5Sum')
2430
self.property_label.show()
@@ -35,7 +41,7 @@ def get_property_pages(self, files):
3541

3642
md5sum = hashlib.md5()
3743
with open(filename,'rb') as f:
38-
for chunk in iter(lambda: f.read(8192), ''):
44+
for chunk in iter(lambda: f.read(8192), b''):
3945
md5sum.update(chunk)
4046
f.close()
4147

examples/mixed.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# This Python caja extension only consider files/folders with a mixed
2+
# upper/lower case name. For those, the following is featured:
3+
# - an emblem on the icon,
4+
# - contextual menu entry.
5+
# - a list view "Mixed" column,
6+
# - a property page,
7+
# - A top area widget.
8+
9+
import os
10+
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+
19+
from gi.repository import Caja, GObject, Gtk
20+
21+
22+
class Mixed(GObject.GObject,
23+
Caja.InfoProvider,
24+
Caja.ColumnProvider,
25+
Caja.MenuProvider,
26+
Caja.PropertyPageProvider,
27+
Caja.LocationWidgetProvider):
28+
29+
emblem = 'favorite-symbolic.symbolic' # Use one of the stock emblems.
30+
31+
# Private methods.
32+
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+
41+
def _file_has_mixed_name(self, cajafile):
42+
name = self._basename(cajafile)
43+
if name.upper() != name and name.lower() != name:
44+
return 'mixed'
45+
return ''
46+
47+
# Caja.InfoProvider implementation.
48+
49+
def update_file_info(self, cajafile):
50+
mixed = self._file_has_mixed_name(cajafile)
51+
cajafile.add_string_attribute('mixed', mixed)
52+
if mixed:
53+
cajafile.add_emblem(self.emblem)
54+
55+
# Caja.ColumnProvider implementation.
56+
57+
def get_columns(self):
58+
return [
59+
Caja.Column(
60+
name = 'Mixed::mixed_column',
61+
attribute = 'mixed',
62+
label = 'Mixed',
63+
description = 'Column added by the mixed extension'
64+
)
65+
]
66+
67+
# Caja.MenuProvider implementation.
68+
69+
def get_file_items(self, window, cajafiles):
70+
menuitems = []
71+
if len(cajafiles) == 1:
72+
for cajafile in cajafiles:
73+
mixed = cajafile.get_string_attribute('mixed')
74+
if mixed:
75+
filename = self._basename(cajafile)
76+
menuitem = Caja.MenuItem(
77+
name = 'Mixed::FileMenu',
78+
label = 'Mixed: %s has a mixed case name' % filename,
79+
tip = '',
80+
icon = ''
81+
)
82+
menuitems.append(menuitem)
83+
84+
return menuitems
85+
86+
def get_background_items(self, window, folder):
87+
mixed = self._file_has_mixed_name(folder)
88+
if not mixed:
89+
return []
90+
return [
91+
Caja.MenuItem(
92+
name = 'Mixed::BackgroundMenu',
93+
label = 'Mixed: you are browsing a directory with a mixed case name',
94+
tip = '',
95+
icon = ''
96+
)
97+
]
98+
99+
# Caja.PropertyPageProvider implementation.
100+
101+
def get_property_pages(self, cajafiles):
102+
pages = []
103+
if len(cajafiles) == 1:
104+
for cajafile in cajafiles:
105+
if self._file_has_mixed_name(cajafile):
106+
page_label = Gtk.Label('Mixed')
107+
page_label.show()
108+
hbox = Gtk.HBox(homogeneous = False, spacing = 4)
109+
hbox.show()
110+
name_label = Gtk.Label(self._basename(cajafile))
111+
name_label.show()
112+
comment_label = Gtk.Label('has a mixed-case name')
113+
comment_label.show()
114+
hbox.pack_start(name_label, False, False, 0)
115+
hbox.pack_start(comment_label, False, False, 0)
116+
pages.append(
117+
Caja.PropertyPage(
118+
name = 'Mixed::PropertyPage',
119+
label = page_label,
120+
page = hbox
121+
)
122+
)
123+
124+
return pages
125+
126+
# Caja.LocationWidgetProvider implementation.
127+
128+
def get_widget(self, uri, window):
129+
filename = self._basename(uri)
130+
if not self._file_has_mixed_name(filename):
131+
return None
132+
label = Gtk.Label('In mixed-case directory %s' % filename)
133+
label.show()
134+
return label

examples/open-terminal.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# This example is contributed by Martin Enlund
22
import os
3-
import urllib
3+
4+
try:
5+
# Python 3.
6+
from urllib.parse import unquote
7+
except:
8+
# Python 2.
9+
from urllib import unquote
410

511
from gi.repository import Caja, GObject, Gio
612

@@ -12,7 +18,7 @@ def __init__(self):
1218
self.gsettings = Gio.Settings.new(TERMINAL_SCHEMA)
1319

1420
def _open_terminal(self, file):
15-
filename = urllib.unquote(file.get_uri()[7:])
21+
filename = unquote(file.get_uri()[7:])
1622
terminal = self.gsettings[TERMINAL_KEY]
1723

1824
os.chdir(filename)

examples/update-file-info-async.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ def __init__(self):
55
pass
66

77
def update_file_info_full(self, provider, handle, closure, file):
8-
print "update_file_info_full"
8+
print('update_file_info_full')
99
GLib.timeout_add_seconds(3, self.update_cb, provider, handle, closure)
1010
return Caja.OperationResult.IN_PROGRESS
1111

1212
def update_cb(self, provider, handle, closure):
13-
print "update_cb"
13+
print('update_cb')
1414
Caja.info_provider_update_complete_invoke(closure, provider, handle, Caja.OperationResult.FAILED)
15+
16+
def cancel_update(self, provider, handle):
17+
print('cancel_update')

0 commit comments

Comments
 (0)