def print_results(options, items): query_regexp = options.query_regexp try: query_regexp = re.compile('({0})'.format(query_regexp)) except re.error: query_regexp = re.compile(r'\Zx') # never match anything for item in items: colors.print('{path}:{line}:', pkg=item['package'], path=item['path'], line=item['line']) for line in item['ctxp2'], item['ctxp1']: line = html.unescape(line) colors.print('{t.dim}|{t.off} {line}', line=line) line = html.unescape(item['context']) template = '{t.dim}>{t.off} ' chunkdict = {} for i, (chunk, matched) in enumerate(xsplit(query_regexp, line)): chunkdict['l{0}'.format(i)] = chunk template += '{t.bold}' if matched: template += '{t.yellow}' template += '{l' + str(i) + '}{t.off}' colors.print(template, **chunkdict) for line in item['ctxn1'], item['ctxn2']: line = html.unescape(line) colors.print('{t.dim}|{t.off} {line}', line=line) colors.print('{t.dim}(pathrank {pathrank:.4f}, rank {rank:.4f}){t.off}', pathrank=item['pathrank'], rank=item['ranking'], ) print() sys.stdout.flush()
def send_query(options): query = options.query colors.print('Query: {t.bold}{q}{t.off}', q=query) sys.stdout.flush() query = dict(Query=('q=' + urllib.parse.quote(query))) query = json.dumps(query) socket = websocket.WebSocket() socket.connect( 'wss://{host}/instantws'.format(host=host), header=['User-Agent: ' + user_agent] ) socket.send(query) n_pages = None while True: msg = socket.recv() msg = json.loads(msg) tp = msg.get('Type', 'default') if tp == 'progress': if msg['FilesProcessed'] != msg['FilesTotal']: continue query_id = msg['QueryId'] n_results = msg['Results'] colors.print('Results: {n}', n=n_results) sys.stdout.flush() if n_results == 0: break packages = wget_json(query_id, 'packages')['Packages'] ts = time.time() colors.print('Packages: {n} ({pkgs})', n=len(packages), pkgs=' '.join(packages), ) print() sys.stdout.flush() for n in range(n_pages): new_ts = time.time() td = new_ts - ts if td < options.delay: time.sleep(options.delay - td) ts = new_ts data = wget_json(query_id, 'page_{n}'.format(n=n)) print_results(options, data) break elif tp == 'pagination': n_pages = msg['ResultPages'] elif tp == 'error': if msg['ErrorType'] == 'invalidquery': print('DCS error: invalid query', file=sys.stderr) sys.exit(1) elif msg['ErrorType'] == 'backendunavailable': print('DCS error: backend server is not available', file=sys.stderr) sys.exit(1) raise NotImplementedError(msg) elif tp == 'default': continue else: raise NotImplementedError(msg)