-
Notifications
You must be signed in to change notification settings - Fork 5
/
gp.py
85 lines (71 loc) · 2.53 KB
/
gp.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
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf-8 -*-
__author__ = 'eveliotc'
__license__ = 'See LICENSE'
import alfred
from alfred import Item
from common import empty_result, join_query, json_to_obj, xml_result
from local import local_search
from mavencentral import maven_central_search
from jcenter import jcenter_search
CONFIGURATIONS = ('compile', 'provided', 'runtime', 'instrumentTest', 'androidTest', 'testCompile',)
DEFAULT_CONFIGURATION = 'compile'
EXCLUDE_PACKAGING = ('apk', 'pom')
DEFAULT_PACKAGING = 'jar'
args = alfred.args()
configuration = DEFAULT_CONFIGURATION
count = len(args)
if count < 1:
empty_result()
elif count == 1:
query = args[0]
elif args[0] in CONFIGURATIONS:
configuration = args[0]
query = join_query(args[1:])
else:
query = join_query(args)
if (len(query) == 0):
empty_result()
# TODO async this
try:
localDocs = local_search(query)
# TODO refactor doc to Item code below so we can send partial results here e.g. xml_result(results, False)
except: # pokemon catch em all
localDocs = [] # TODO do something about it like let user know to define $ANDROID_HOME, install local repo etc.
# TODO async this
(mavenCentralDocs, suggestions) = maven_central_search(query)
# TODO async this
jcenterDocs = jcenter_search(query)
fullDocs = []
fullDocs.extend(localDocs)
fullDocs.extend(mavenCentralDocs)
fullDocs.extend(jcenterDocs)
results = []
for doc in fullDocs:
doc = json_to_obj(doc)
packaging = doc.p
id = doc.id
if packaging not in EXCLUDE_PACKAGING:
# If not jar use @aar or whatever
atPackaging = u'' if packaging.lower() == DEFAULT_PACKAGING else '@' + doc.p
# In case of compiler dependency use provided
lineConfiguration = u'provided' if 'compiler' in id else configuration
depLine = u"%s '%s:%s%s'" % (lineConfiguration, id, doc.latestVersion, atPackaging)
source = doc.source if hasattr(doc, 'source') and len(doc.source) > 0 else 'Maven Central'
item = Item(
attributes={'uid': alfred.uid(id), 'arg': depLine},
title=doc.a,
subtitle=u'Paste (from %s): %s' % (source, depLine),
icon=u'icon.png'
)
results.append(item)
for suggestion in suggestions:
if isinstance(suggestion, dict):
for sug in suggestion['suggestion']:
item = Item(
attributes={'uid': alfred.uid(sug), 'arg': u'search=%s' % sug},
title=sug,
subtitle=u"Did you mean '%s'..." % sug,
icon=u'icon.png'
)
results.append(item)
xml_result(results)