Beispiel #1
0
Datei: app.py Projekt: nbstar/dxr
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'])
Beispiel #2
0
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])
Beispiel #3
0
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)

    plugin_headers = concat_plugin_headers(plugins_named(frozen['enabled_plugins']))
    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'] + plugin_headers)

    if not files_and_folders:
        raise NotFound

    return render_template(
        'folder.html',
        # Common template variables:
        www_root=config.www_root,
        tree=tree,
        tree_tuples=_tree_tuples('.parallel', path=path),
        generated_date=frozen['generated_date'],
        google_analytics_key=config.google_analytics_key,
        paths_and_names=_linked_pathname(path, tree),
        plugin_headers=plugin_headers,
        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'),
             [f.get(h, [''])[0] for h in plugin_headers],
             url_for('.browse', tree=tree, path=f.get('link', f['path'])[0]))
            for f in files_and_folders])
Beispiel #4
0
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'])
Beispiel #5
0
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,
                                       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(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
        ])
Beispiel #6
0
Datei: app.py Projekt: nbstar/dxr
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])
Beispiel #7
0
def parallel(tree, path=''):
    """If a file or dir parallel to the given path exists in the given tree,
    redirect to it. Otherwise, redirect to the root of the given tree.

    Deferring this test lets us avoid doing 50 queries when drawing the Switch
    Tree menu when 50 trees are indexed: we check only when somebody actually
    chooses something.

    """
    config = current_app.dxr_config
    files = filtered_query(es_alias_or_not_found(tree),
                           FILE,
                           filter={'path': path.rstrip('/')},
                           size=1,
                           include=[])  # We don't really need anything.
    return redirect(('{root}/{tree}/source/{path}' if files else
                     '{root}/{tree}/source/').format(root=config.www_root,
                                                     tree=tree,
                                                     path=path))
Beispiel #8
0
Datei: app.py Projekt: klibby/dxr
def parallel(tree, path=''):
    """If a file or dir parallel to the given path exists in the given tree,
    redirect to it. Otherwise, redirect to the root of the given tree.

    Deferring this test lets us avoid doing 50 queries when drawing the Switch
    Tree menu when 50 trees are indexed: we check only when somebody actually
    chooses something.

    """
    config = current_app.dxr_config
    files = filtered_query(
        es_alias_or_not_found(tree),
        FILE,
        filter={'path': path.rstrip('/')},
        size=1,
        include=[])  # We don't really need anything.
    return redirect(('{root}/{tree}/source/{path}' if files else
                     '{root}/{tree}/source/').format(root=config.www_root,
                                                     tree=tree,
                                                     path=path))