-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_sim_apps.py
75 lines (58 loc) · 2.25 KB
/
find_sim_apps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python
# encoding: utf-8
import sys
import argparse
import siminfo
from workflow import (Workflow, ICON_WEB, ICON_INFO, ICON_WARNING,
PasswordNotFound)
from workflow.background import run_in_background, is_running
from os.path import dirname
def search_key_for_app(app):
"""Generate a string search key for a app"""
elements = []
elements.append(app['name'])
elements.append(app['id'])
elements.append(app['device']['name'])
return u' '.join(elements)
def main(wf):
# build argument parser to parse script args and collect their
# values
parser = argparse.ArgumentParser()
parser.add_argument('query', nargs='?', default=None)
parser.add_argument('--apps', dest='appPaths',
const=True, default=False, action='store_const',
help='Produces path to apps instead')
# parse the script's arguments
args = parser.parse_args(wf.args)
####################################################################
# View/filter Simulator apps
####################################################################
query = args.query
appPaths = args.appPaths
apps = siminfo.getSimAppResults(wf.cachedir)
# If script was passed a query, use it to filter posts if we have some
if query and apps:
apps = wf.filter(query, apps, key=search_key_for_app, min_score=20)
if not apps: # we have no data to show, so show a warning and stop
wf.add_item('No apps found', icon=ICON_WARNING)
wf.send_feedback()
return 0
# Loop through the returned posts and add a item for each to
# the list of results for Alfred
for app in apps:
appName = '%s (%s - %s)' % (app['name'], app['device']['name'], app['device']['runtime'])
path = app['data_path']
if appPaths:
path = dirname(app['path'])
wf.add_item(title=appName,
subtitle=app['short_path'] + ' - ' + app['id'],
arg=path,
valid=True,
icon=app['icon'],
uid=app['data_path'])
# Send the results to Alfred as XML
wf.send_feedback()
return 0
if __name__ == u"__main__":
wf = Workflow()
sys.exit(wf.run(main))