Merge lp:~thesquash/deja-dup-caja/deja-dup-caja into lp:deja-dup-caja

Proposed by Gordon N. Squash
Status: Approved
Approved by: costales
Approved revision: 25
Proposed branch: lp:~thesquash/deja-dup-caja/deja-dup-caja
Merge into: lp:deja-dup-caja
Diff against target: 183 lines (+30/-40)
1 file modified
caja-extension/dejadup.py (+30/-40)
To merge this branch: bzr merge lp:~thesquash/deja-dup-caja/deja-dup-caja
Reviewer Review Type Date Requested Status
costales Approve
Review via email: mp+403424@code.launchpad.net

Commit message

Use GSettings GIO API instead of gsettings utility program to find Deja Dup preferences

Description of the change

Deja Dup Caja calls an external program (specifically gsettings) to discover the user's Deja Dup preferences. This method is clunky and involves parsing strings in weird ways. This commit uses the much cleaner and more direct GSettings GIO API to read the Deja Dup preferences.

To post a comment you must log in.
Revision history for this message
costales (costales) wrote :

Thank you so much Gordon!!

review: Approve

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'caja-extension/dejadup.py'
2--- caja-extension/dejadup.py 2021-04-07 09:57:09 +0000
3+++ caja-extension/dejadup.py 2021-05-28 01:02:10 +0000
4@@ -7,24 +7,24 @@
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8-#
9+#
10 # Deja Dup Caja is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14-#
15+#
16 # You should have received a copy of the GNU General Public License
17 # along with Deja Dup Caja; if not, see http://www.gnu.org/licenses
18 # for more information.
19
20-import gettext, subprocess, os, ast
21+import gettext, os
22 # Python 2 or 3
23 try:
24 from urllib import unquote
25 except ImportError:
26 from urllib.parse import unquote
27
28-from gi.repository import Caja, GObject
29+from gi.repository import Caja, GObject, Gio
30
31 # i18n
32 gettext.textdomain('deja-dup-caja')
33@@ -34,50 +34,40 @@
34 class DejaDup:
35 def __init__(self):
36 pass
37-
38- def _run_cmd(self, cmd, background=True):
39- """Run command into shell"""
40- if background:
41- proc = subprocess.Popen(cmd, shell=False)
42- else:
43- proc = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
44- stdout,stderr = proc.communicate()
45- return stdout,stderr
46-
47+
48 def backup(self, item):
49 """Call to backup"""
50 self._run_cmd(['deja-dup', '--backup', item])
51-
52+
53 def restore(self, item):
54 """Call to restore"""
55 self._run_cmd(['deja-dup', '--restore', item])
56-
57+
58 def get_dejadup_paths(self, type_list):
59 """Get include/exclude paths from Deja Dup"""
60 new_paths = []
61-
62- cmd = ['gsettings', 'get', 'org.gnome.DejaDup', type_list]
63- stdout,stderr = self._run_cmd(cmd, False)
64-
65+
66+ try:
67+ settings = Gio.Settings.new('org.gnome.DejaDup')
68+ paths = settings.get_value(type_list)
69 # Error
70- if stderr:
71+ except:
72 return []
73+
74 # OK
75- stdout_str = [stdout][0].decode() # With python3 needs this convertion
76- paths = ast.literal_eval(stdout_str) # Convert shell dump to list
77 for path in paths:
78 try: # Is a shell variable?
79 new_paths.append(os.environ[path.replace('$', '')])
80 except:
81 new_paths.append(path)
82-
83+
84 return new_paths
85
86
87 class Utils:
88 def __init__(self):
89 pass
90-
91+
92 def get_filepath(self, items):
93 """Get just the path from a complete path and filename"""
94 try:
95@@ -85,7 +75,7 @@
96 except:
97 item = items
98 return unquote(item.get_uri()[7:])
99-
100+
101 def search_paths(self, item_path, paths):
102 """Check if at least one path is in the pattern path"""
103 for path in paths:
104@@ -98,57 +88,57 @@
105 def __init__(self):
106 self.dejadup = DejaDup()
107 self.utils = Utils()
108-
109+
110 def _backup_activate(self, menu, item):
111 """Backup file or folder"""
112 self.dejadup.backup(item)
113-
114+
115 def _restore_activate(self, menu, item):
116 """Restore file or folder"""
117 self.dejadup.restore(item)
118-
119+
120 def _previous_checks(self, items):
121 """Checks before generate the contextual menu"""
122 # Show only for 1 item selected
123 if len(items) != 1:
124 return False
125 item = items[0]
126-
127+
128 # Check exists yet
129 if item.is_gone():
130 return False
131-
132+
133 # Only handle files
134 if item.get_uri_scheme() != 'file':
135 return False
136-
137+
138 return True
139-
140+
141 def get_background_items(self, window, current_folder):
142 """Caja invoke this when user clicks in an empty area"""
143 Menu = Caja.MenuItem(
144 name="DejaDupCajaMenu::Restore",
145 label=_("Revert to Previous Version..."),
146 icon="deja-dup")
147-
148+
149 # Get complete path
150 item = self.utils.get_filepath(current_folder)
151-
152+
153 Menu.connect('activate', self._restore_activate, item)
154 return [Menu]
155-
156+
157 def get_file_items(self, window, selected_items):
158 """Caja invoke this when user clicks in a file or folder"""
159 if not self._previous_checks(selected_items):
160 return
161-
162+
163 # Get complete path
164 item = self.utils.get_filepath(selected_items)
165-
166+
167 # Get paths
168 include_paths = self.dejadup.get_dejadup_paths('include-list')
169 exclude_paths = self.dejadup.get_dejadup_paths('exclude-list')
170-
171+
172 # Check backup or restore menu entry
173 # Is the path into the exclude? > Backup
174 if self.utils.search_paths(item, exclude_paths):
175@@ -159,7 +149,7 @@
176 # Not include/exclude > Backup
177 else:
178 backup = True
179-
180+
181 # Generate menu and trigger event
182 if backup:
183 Menu = Caja.MenuItem(

Subscribers

People subscribed via source and target branches

to all changes: