1 """
2 The B{0install search} command-line interface.
3 """
4
5
6
7
8 from __future__ import print_function
9
10 import sys
11
12 from zeroinstall.injector import qdom
13 from zeroinstall.cmd import UsageError
14 from zeroinstall import logger
15
16 if sys.version_info[0] > 2:
17 from urllib import request as urllib2
18 from urllib.parse import quote
19 else:
20 from urllib import quote
21 import urllib2
22
23 syntax = "QUERY"
24
27
28 -def handle(config, options, args):
29 if len(args) == 0:
30 raise UsageError()
31
32 url = config.mirror + '/search/?q=' + quote(' '.join(args))
33 logger.info("Fetching %s...", url)
34 root = qdom.parse(urllib2.urlopen(url))
35 assert root.name == 'results'
36
37 first = True
38 for child in root.childNodes:
39 if child.name != 'result': continue
40
41 if first:
42 first = False
43 else:
44 print()
45
46 print(child.attrs['uri'])
47 score = child.attrs['score']
48
49 details = {}
50 for detail in child.childNodes:
51 details[detail.name] = detail.content
52 print(" {name} - {summary} [{score}%]".format(
53 name = child.attrs['name'],
54 summary = details.get('summary', ''),
55 score = score))
56