def _browse_folder(tree, path, config): """Return a rendered folder listing for folder ``path``. Search for FILEs having folder == path. If any matches, render the folder listing. Otherwise, raise NotFound. """ def item_or_list(item): """If item is a list, return its first element. Otherwise, just return it. """ # TODO @pelmers: remove this function when format bumps to 20 if isinstance(item, list): return item[0] return item frozen = frozen_config(tree) files_and_folders = filtered_query( frozen['es_alias'], FILE, filter={'folder': path}, sort=[{'is_folder': 'desc'}, 'name'], size=1000000, include=['name', 'modified', 'size', 'link', 'path', 'is_binary', 'is_folder']) if not files_and_folders: raise NotFound return render_template( 'folder.html', # Common template variables: www_root=config.www_root, tree=tree, tree_tuples=[ (t['name'], url_for('.parallel', tree=t['name'], path=path), t['description']) for t in frozen_configs()], generated_date=frozen['generated_date'], google_analytics_key=config.google_analytics_key, paths_and_names=_linked_pathname(path, tree), filters=filter_menu_items( plugins_named(frozen['enabled_plugins'])), # Autofocus only at the root of each tree: should_autofocus_query=path == '', # Folder template variables: name=basename(path) or tree, path=path, files_and_folders=[ (_icon_class_name(f), f['name'], decode_es_datetime(item_or_list(f['modified'])) if 'modified' in f else None, f.get('size'), url_for('.browse', tree=tree, path=f.get('link', f['path'])[0])) for f in files_and_folders])
def browse(tree, path=''): """Show a directory listing or a single file from one of the trees. Raise NotFound if path does not exist as either a folder or file. """ config = current_app.dxr_config try: # Strip any trailing slash because we do not store it in ES. return _browse_folder(tree, path.rstrip('/'), config) except NotFound: frozen = frozen_config(tree) # Grab the FILE doc, just for the sidebar nav links: files = filtered_query( frozen['es_alias'], FILE, filter={'path': path}, size=1, include=['links']) if not files: raise NotFound lines = filtered_query( frozen['es_alias'], LINE, filter={'path': path}, sort=['number'], size=1000000, include=['content', 'refs', 'regions', 'annotations']) # Deref the content field in each document. We can do this because we # do not store empty lines in ES. for doc in lines: doc['content'] = doc['content'][0] return _browse_file(tree, path, lines, files[0], config, frozen['generated_date'])
def _browse_folder(tree, path, config): """Return a rendered folder listing for folder ``path``. Search for FILEs having folder == path. If any matches, render the folder listing. Otherwise, raise NotFound. """ frozen = frozen_config(tree) files_and_folders = filtered_query( frozen['es_alias'], FILE, filter={'folder': path}, sort=[{'is_folder': 'desc'}, 'name'], size=10000, exclude=['raw_data']) if not files_and_folders: raise NotFound return render_template( 'folder.html', # Common template variables: www_root=config.www_root, tree=tree, tree_tuples=[ (t['name'], url_for('.parallel', tree=t['name'], path=path), t['description']) for t in frozen_configs()], generated_date=frozen['generated_date'], google_analytics_key=config.google_analytics_key, paths_and_names=_linked_pathname(path, tree), filters=filter_menu_items( plugins_named(frozen['enabled_plugins'])), # Autofocus only at the root of each tree: should_autofocus_query=path == '', # Folder template variables: name=basename(path) or tree, path=path, files_and_folders=[ (_icon_class_name(f), f['name'], decode_es_datetime(f['modified']) if 'modified' in f else None, f.get('size'), url_for('.browse', tree=tree, path=f['path'][0]), f.get('is_binary', [False])[0]) for f in files_and_folders])
def _build_common_file_template(tree, path, is_binary, date, config): """Return a dictionary of the common required file template parameters. """ return { # Common template variables: 'www_root': config.www_root, 'tree': tree, 'tree_tuples': _tree_tuples('.parallel', path=path), 'generated_date': date, 'google_analytics_key': config.google_analytics_key, 'filters': filter_menu_items( plugins_named(frozen_config(tree)['enabled_plugins'])), # File template variables 'paths_and_names': _linked_pathname(path, tree), 'icon_url': url_for('.static', filename='icons/mimetypes/%s.png' % icon(path, is_binary)), 'path': path, 'name': basename(path) }
def search(tree): """Normalize params, and dispatch between JSON- and HTML-returning searches, based on Accept header. """ # Normalize querystring params: config = current_app.dxr_config frozen = frozen_config(tree) req = request.values query_text = req.get('q', '') offset = non_negative_int(req.get('offset'), 0) limit = min(non_negative_int(req.get('limit'), 100), 1000) # Make a Query: query = Query(partial(current_app.es.search, index=frozen['es_alias']), query_text, plugins_named(frozen['enabled_plugins'])) # Fire off one of the two search routines: searcher = _search_json if _request_wants_json() else _search_html return searcher(query, tree, query_text, offset, limit, config)
def browse(tree, path=''): """Show a directory listing or a single file from one of the trees. Raise NotFound if path does not exist as either a folder or file. """ config = current_app.dxr_config try: # Strip any trailing slash because we do not store it in ES. return _browse_folder(tree, path.rstrip('/'), config) except NotFound: frozen = frozen_config(tree) # Grab the FILE doc, just for the sidebar nav links and the symlink target: files = filtered_query( frozen['es_alias'], FILE, filter={'path': path}, size=1, include=['link', 'links', 'is_binary']) if not files: raise NotFound file_doc = files[0] if 'link' in file_doc: # Then this path is a symlink, so redirect to the real thing. return redirect(url_for('.browse', tree=tree, path=file_doc['link'][0])) lines = filtered_query( frozen['es_alias'], LINE, filter={'path': path}, sort=['number'], size=1000000, include=['content', 'refs', 'regions', 'annotations']) # Deref the content field in each document. We can do this because we # do not store empty lines in ES. for doc in lines: doc['content'] = doc['content'][0] return _browse_file(tree, path, lines, file_doc, config, file_doc.get('is_binary', [False])[0], frozen['generated_date'])
def _build_common_file_template(tree, path, date, config): """Return a dictionary of the common required file template parameters. """ return { # Common template variables: 'www_root': config.www_root, 'tree': tree, 'tree_tuples': [(t['name'], url_for('.parallel', tree=t['name'], path=path), t['description']) for t in frozen_configs()], 'generated_date': date, 'google_analytics_key': config.google_analytics_key, 'filters': filter_menu_items( plugins_named(frozen_config(tree)['enabled_plugins'])), # File template variables 'paths_and_names': _linked_pathname(path, tree), 'icon': icon(path), 'path': path, 'name': basename(path) }
def _search_html(query, tree, query_text, offset, limit, config): """Return the rendered template for search.html. """ frozen = frozen_config(tree) # Try a normal search: template_vars = { 'filters': filter_menu_items( plugins_named(frozen['enabled_plugins'])), 'generated_date': frozen['generated_date'], 'google_analytics_key': config.google_analytics_key, 'query': query_text, 'search_url': url_for('.search', tree=tree, q=query_text, redirect='false'), 'top_of_tree': url_for('.browse', tree=tree), 'tree': tree, 'tree_tuples': _tree_tuples('.search', q=query_text), 'www_root': config.www_root} return render_template('search.html', **template_vars)
def _build_common_file_template(tree, path, is_binary, date, config): """Return a dictionary of the common required file template parameters. """ return { # Common template variables: 'www_root': config.www_root, 'tree': tree, 'tree_tuples': [(t['name'], url_for('.parallel', tree=t['name'], path=path), t['description']) for t in frozen_configs()], 'generated_date': date, 'google_analytics_key': config.google_analytics_key, 'filters': filter_menu_items( plugins_named(frozen_config(tree)['enabled_plugins'])), # File template variables 'paths_and_names': _linked_pathname(path, tree), 'icon_url': url_for('.static', filename='icons/mimetypes/%s.png' % icon(path, is_binary)), 'path': path, 'name': basename(path) }