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