Merge lp:~stolowski/phablet-extras/unity-lens-mock-new-api into lp:phablet-extras/unity-lens-mock
- unity-lens-mock-new-api
- Merge into unity-lens-mock
Proposed by
Paweł Stołowski
Status: | Superseded |
---|---|
Proposed branch: | lp:~stolowski/phablet-extras/unity-lens-mock-new-api |
Merge into: | lp:phablet-extras/unity-lens-mock |
Diff against target: |
325 lines (+18/-63) 4 files modified
debian/changelog (+6/-0) debian/control (+1/-0) unity-lens-mock (+11/-10) unity_lens_mock/scope.py (+0/-53) |
To merge this branch: | bzr merge lp:~stolowski/phablet-extras/unity-lens-mock-new-api |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Michał Sawicz | Approve | ||
Review via email: mp+167721@code.launchpad.net |
This proposal has been superseded by a proposal from 2013-06-19.
Commit message
Updated for new scopes API.
Description of the change
Updated for new scopes API.
Please have a look at https:/
To post a comment you must log in.
- 16. By Paweł Stołowski
-
Require unity-scopes-
runner.
Revision history for this message
Paweł Stołowski (stolowski) wrote : | # |
> This should depend on unity-scopes-
Right, done.
Revision history for this message
Michał Sawicz (saviq) wrote : | # |
This looks good, before top-approving, will verify again with:
https:/
https:/
review:
Approve
Unmerged revisions
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'debian/changelog' |
2 | --- debian/changelog 2013-02-15 17:08:02 +0000 |
3 | +++ debian/changelog 2013-06-19 14:03:24 +0000 |
4 | @@ -1,3 +1,9 @@ |
5 | +unity-lens-mock (0.5) UNRELEASED; urgency=low |
6 | + |
7 | + * Ported to new scopes API. |
8 | + |
9 | + -- Pawel Stolowski <pawel.stolowski@ubuntu.com> Wed, 05 Jun 2013 16:47:30 +0200 |
10 | + |
11 | unity-lens-mock (0.4) quantal; urgency=low |
12 | |
13 | * Search in description, too |
14 | |
15 | === modified file 'debian/control' |
16 | --- debian/control 2013-02-05 12:21:24 +0000 |
17 | +++ debian/control 2013-06-19 14:03:24 +0000 |
18 | @@ -15,6 +15,7 @@ |
19 | Depends: ${misc:Depends}, |
20 | ${python:Depends}, |
21 | gir1.2-unity-5.0, |
22 | + unity-scopes-runner (>= 7.0.3) |
23 | Breaks: unity (<< 6.0.0) |
24 | Description: Unity Lens driven by JSON files |
25 | This Lens allows you to provide data via JSON files for testing and mocking Unity |
26 | |
27 | === modified file 'unity-lens-mock' (properties changed: -x to +x) |
28 | --- unity-lens-mock 2013-02-01 14:41:46 +0000 |
29 | +++ unity-lens-mock 2013-06-19 14:03:24 +0000 |
30 | @@ -4,6 +4,8 @@ |
31 | -- unity-lens-mock |
32 | |
33 | Reads a json file and starts a Unity lens / scope pair based on data in the file. |
34 | + It's meant to be run via scope-runner, e.g. |
35 | + /usr/share/unity-scopes/scope-runner-dbus.py /usr/bin/unity-lens-mock <data file> |
36 | |
37 | @author: Michał Sawicz <michal.sawicz@canonical.com> |
38 | |
39 | @@ -22,11 +24,11 @@ |
40 | |
41 | from gi.repository import GObject |
42 | |
43 | -from unity_lens_mock.lens import MockLens |
44 | +from unity_lens_mock.scope import MockScope |
45 | |
46 | __all__ = [] |
47 | -__version__ = 0.1 |
48 | -__updated__ = '2013-01-30' |
49 | +__version__ = 0.5 |
50 | +__updated__ = '2013-06-05' |
51 | |
52 | __program_name__ = os.path.basename(sys.argv[0]) |
53 | __program_version__ = "v%s" % __version__ |
54 | @@ -36,19 +38,22 @@ |
55 | |
56 | DEBUG = 1 |
57 | |
58 | -if __name__ == "__main__": |
59 | +def load_scope(): |
60 | try: |
61 | # setup option parser |
62 | parser = OptionParser( |
63 | version=__program_version_string__, |
64 | usage=__program_usage_string__ |
65 | ) |
66 | - |
67 | + |
68 | # process options |
69 | (opts, args) = parser.parse_args(sys.argv) |
70 | |
71 | - if len(args) != 2: |
72 | + if len(args) < 2: |
73 | raise ValueError("provide a path to the json file") |
74 | + |
75 | + data = json.load(open(args[-1]), encoding='utf-8') |
76 | + return MockScope(data) |
77 | |
78 | except Exception, e: |
79 | indent = len(__program_name__) * " " |
80 | @@ -56,7 +61,3 @@ |
81 | sys.stderr.write(indent + " for help use --help" + "\n") |
82 | raise SystemExit(2) |
83 | |
84 | - |
85 | - mockLens = MockLens(json.load(open(args[1]), encoding='utf-8')) |
86 | - |
87 | - GObject.MainLoop().run() |
88 | |
89 | === renamed file 'unity_lens_mock/lens.py' => 'unity_lens_mock/scope.py' |
90 | --- unity_lens_mock/lens.py 2013-02-01 11:22:22 +0000 |
91 | +++ unity_lens_mock/scope.py 2013-06-19 14:03:24 +0000 |
92 | @@ -8,8 +8,6 @@ |
93 | Unity |
94 | ) |
95 | |
96 | -from scope import MockScope |
97 | - |
98 | __rendererMap__ = { |
99 | 'vertical_tile': Unity.CategoryRenderer.VERTICAL_TILE |
100 | } |
101 | @@ -18,70 +16,83 @@ |
102 | 'radio': Unity.RadioOptionFilter |
103 | } |
104 | |
105 | -class MockLens(object): |
106 | - '''Mock Unity lens''' |
107 | +__resultTypeMap__ = [ |
108 | + Unity.ResultType.DEFAULT |
109 | +] |
110 | + |
111 | +class MockSearch (Unity.ScopeSearchBase): |
112 | + def __init__(self, search_context, data): |
113 | + Unity.ScopeSearchBase.__init__(self) |
114 | + self.__data = data |
115 | + self.set_search_context(search_context) |
116 | + |
117 | + def __search(self): |
118 | + result_set = self.search_context.result_set |
119 | + query = self.search_context.search_query.lower() |
120 | + if query == "": |
121 | + if (self.search_context.search_type == Unity.SearchType.DEFAULT): |
122 | + for entry in self.__data['data']: |
123 | + result_set.add_result(Unity.ScopeResult.create(entry[0], entry[1], entry[2], __resultTypeMap__[entry[3]], entry[4], entry[5], entry[6], entry[7], {})) |
124 | + else: |
125 | + for entry in self.__data['global_data']: |
126 | + result_set.add_result(Unity.ScopeResult.create(entry[0], entry[1], entry[2], __resultTypeMap__[entry[3]], entry[4], entry[5], entry[6], entry[7], {})) |
127 | + else: |
128 | + if (self.search_context.search_type == Unity.SearchType.DEFAULT): |
129 | + for entry in self.__data.get('search_data', self.__data.get('common_search_data', self.__data.get('data', ()))): |
130 | + if (query in entry[5].lower() or query in entry[6].lower()): |
131 | + result_set.add_result(Unity.ScopeResult.create(entry[0], entry[1], entry[2], __resultTypeMap__[entry[3]], entry[4], entry[5], entry[6], entry[7], {})) |
132 | + else: |
133 | + for entry in self.__data.get('global_search_data', self.__data.get('common_search_data', self.__data.get('global_data', ()))): |
134 | + if (query in entry[5].lower() or query in entry[6].lower()): |
135 | + result_set.add_result(Unity.ScopeResult.create(entry[0], entry[1], entry[2], __resultTypeMap__[entry[3]], entry[4], entry[5], entry[6], entry[7], {})) |
136 | + |
137 | + def do_run(self): |
138 | + try: |
139 | + result_set = self.search_context.result_set |
140 | + self.__search() |
141 | + except Exception as error: |
142 | + print(error) |
143 | + |
144 | +class MockScope(Unity.AbstractScope): |
145 | + '''Mock Unity Scope''' |
146 | |
147 | __data = None |
148 | - __lens = None |
149 | |
150 | def __init__(self, data): |
151 | '''Command line options.''' |
152 | + Unity.AbstractScope.__init__(self) |
153 | self.__data = data |
154 | - self.__requestName(self.__data['lens']['dbusname']) |
155 | - |
156 | - self.__lens = Unity.Lens.new(self.__data['lens']['dbuspath'], self.__data['lens']['id']) |
157 | - |
158 | - self.__lens.props.search_hint = self.__data['lens']['searchhint'] |
159 | - self.__lens.props.visible = self.__data['lens']['visible'] |
160 | - self.__lens.props.search_in_global = self.__data['lens']['global'] |
161 | - |
162 | - self.__populate_categories() |
163 | - self.__populate_filters() |
164 | - |
165 | - for scope in self.__data['scopes']: |
166 | - s = MockScope(scope) |
167 | - self.__lens.add_local_scope(s.getUnityScope()) |
168 | - |
169 | - self.__lens.export() |
170 | - |
171 | - def __requestName(self, dbusname): |
172 | - '''Connect to the session bus, exit if there is a running instance.''' |
173 | - try: |
174 | - session_bus_connection = Gio.bus_get_sync(Gio.BusType.SESSION, None) |
175 | - session_bus = Gio.DBusProxy.new_sync(session_bus_connection, 0, None, |
176 | - 'org.freedesktop.DBus', |
177 | - '/org/freedesktop/DBus', |
178 | - 'org.freedesktop.DBus', None) |
179 | - result = session_bus.call_sync('RequestName', |
180 | - GLib.Variant("(su)", (dbusname, 0x4)), |
181 | - 0, -1, None) |
182 | - |
183 | - # Unpack variant response with signature "(u)". 1 means we got it. |
184 | - result = result.unpack()[0] |
185 | - |
186 | - if result != 1: |
187 | - print >> sys.stderr, "Failed to own dbusname %s. Bailing out." % dbusname |
188 | - raise SystemExit(1) |
189 | - except: |
190 | - raise SystemExit(1) |
191 | - |
192 | - def __populate_categories(self): |
193 | + self.__unique_name = self.__data['scope']['dbusname'] |
194 | + self.__unique_path = self.__data['scope']['dbuspath'] |
195 | + |
196 | + #self.__lens.props.visible = self.__data['lens']['visible'] |
197 | + #self.__lens.props.search_in_global = self.__data['lens']['global'] |
198 | + |
199 | + def do_get_schema(self): |
200 | + schema = Unity.Schema.new() |
201 | + return schema |
202 | + |
203 | + def do_get_search_hint(self): |
204 | + return self.__data['scope']['searchhint'] |
205 | + |
206 | + def do_get_categories(self): |
207 | '''Populate categories''' |
208 | - categories = [] |
209 | - for category in self.__data['lens']['categories']: |
210 | - categories.append( |
211 | - Unity.Category.new( |
212 | - category['name'], |
213 | - Gio.ThemedIcon.new(category['iconpath']), |
214 | - __rendererMap__[category['renderer']] |
215 | - )) |
216 | - |
217 | - self.__lens.props.categories = categories |
218 | - |
219 | - def __populate_filters(self): |
220 | + cs = Unity.CategorySet.new () |
221 | + for category in self.__data['scope']['categories']: |
222 | + cat = Unity.Category.new( |
223 | + category['id'], |
224 | + category['name'], |
225 | + Gio.ThemedIcon.new(category['iconpath']), |
226 | + __rendererMap__[category['renderer']] |
227 | + ) |
228 | + cs.add(cat) |
229 | + return cs |
230 | + |
231 | + def do_get_filters(self): |
232 | + fs = Unity.FilterSet.new () |
233 | filters = [] |
234 | try: |
235 | - for filterData in self.__data['lens']['filters']: |
236 | + for filterData in self.__data['scope']['filters']: |
237 | f = __filterMap__[filterData['type']].new( |
238 | filterData['id'], |
239 | filterData['name'], |
240 | @@ -90,8 +101,23 @@ |
241 | ) |
242 | for optionData in filterData['options']: |
243 | f.add_option(*optionData) |
244 | - filters.append(f) |
245 | + fs.add(f) |
246 | except KeyError: |
247 | pass |
248 | - |
249 | - self.__lens.props.filters = filters |
250 | \ No newline at end of file |
251 | + return fs |
252 | + |
253 | + def do_get_group_name(self): |
254 | + return self.__unique_name |
255 | + |
256 | + def do_get_unique_name(self): |
257 | + return self.__unique_path |
258 | + |
259 | + def do_create_search_for_query (self, search_context): |
260 | + se = MockSearch(search_context, self.__data['scope']) |
261 | + return se |
262 | + |
263 | + def do_create_previewer(self, result, metadata): |
264 | + return None #TODO |
265 | + |
266 | + def do_activate (self, result, metadata, id): |
267 | + return Unity.ActivationResponse.new (Unity.HandledType.NOT_HANDLED, uri) |
268 | |
269 | === removed file 'unity_lens_mock/scope.py' |
270 | --- unity_lens_mock/scope.py 2013-02-15 17:08:02 +0000 |
271 | +++ unity_lens_mock/scope.py 1970-01-01 00:00:00 +0000 |
272 | @@ -1,53 +0,0 @@ |
273 | -# encoding: utf-8 |
274 | - |
275 | -from gi.repository import ( |
276 | - Unity, |
277 | - Gio |
278 | -) |
279 | - |
280 | -class MockScope(object): |
281 | - |
282 | - __scope = None |
283 | - __data = None |
284 | - |
285 | - def __init__ (self, data): |
286 | - self.__data = data |
287 | - self.__scope = Unity.Scope.new(self.__data['dbuspath']) |
288 | - |
289 | - # Listen for changes and requests |
290 | - self.__scope.connect("search_changed", self._on_search_changed) |
291 | - self.__scope.connect("filters-changed", self._on_filters_changed) |
292 | - self.__scope.connect("activate-uri", self._activate_uri) |
293 | - |
294 | - def getUnityScope(self): |
295 | - return self.__scope |
296 | - |
297 | - def _activate_uri (self, scope, uri): |
298 | - return Unity.ActivationResponse.new (Unity.HandledType.NOT_HANDLED, uri) |
299 | - |
300 | - def _on_search_changed (self, scope, search, searchType, cancellable): |
301 | - search.props.results_model.clear() |
302 | - |
303 | - if search.props.search_string == "": |
304 | - if (searchType == Unity.SearchType.DEFAULT): |
305 | - for entry in self.__data['data']: |
306 | - search.props.results_model.append(*entry) |
307 | - else: |
308 | - for entry in self.__data['global_data']: |
309 | - search.props.results_model.append(*entry) |
310 | - else: |
311 | - if (searchType == Unity.SearchType.DEFAULT): |
312 | - for entry in self.__data.get('search_data', self.__data.get('common_search_data', self.__data.get('data', ()))): |
313 | - query = search.props.search_string.lower() |
314 | - if (query in entry[4].lower() or query in entry[5].lower()): |
315 | - search.props.results_model.append(*entry) |
316 | - else: |
317 | - for entry in self.__data.get('global_search_data', self.__data.get('common_search_data', self.__data.get('global_data', ()))): |
318 | - query = search.props.search_string.lower() |
319 | - if (query in entry[4].lower() or query in entry[5].lower()): |
320 | - search.props.results_model.append(*entry) |
321 | - |
322 | - search.finished() |
323 | - |
324 | - def _on_filters_changed(self, scope): |
325 | - pass |
This should depend on unity-scopes- runner, probably?