Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Commit

Permalink
Merge browsing of binary files. Fixes bug 1052217. Close #386.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikrose committed May 11, 2015
2 parents 376b4ba + a5190be commit 53846ce
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 40 deletions.
3 changes: 2 additions & 1 deletion dxr/app.py
Expand Up @@ -264,7 +264,8 @@ def _browse_folder(tree, path, config):
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]))
url_for('.browse', tree=tree, path=f['path'][0]),
f.get('is_binary', [False])[0])
for f in files_and_folders])


Expand Down
52 changes: 24 additions & 28 deletions dxr/build.py
Expand Up @@ -486,34 +486,30 @@ def docs():
# indexing. We could interpose an external queueing system, but I'm
# willing to potentially sacrifice a little speed here for the easy
# management of self-throttling.
#
# Conditional until we figure out how to display arbitrary binary
# files:
if is_text or is_image(rel_path):
file_info = stat(path)
folder_name, file_name = split(rel_path)
# Hard-code the keys that are hard-coded in the browse()
# controller. Merge with the pluggable ones from needles:
doc = dict(# Some non-array fields:
folder=folder_name,
name=file_name,
size=file_info.st_size,
modified=datetime.fromtimestamp(file_info.st_mtime),
is_folder=False,

# And these, which all get mashed into arrays:
**needles)
links = [{'order': order,
'heading': heading,
'items': [{'icon': icon,
'title': title,
'href': href}
for icon, title, href in items]}
for order, heading, items in
chain.from_iterable(linkses)]
if links:
doc['links'] = links
yield es.index_op(doc, doc_type=FILE)
file_info = stat(path)
folder_name, file_name = split(rel_path)
# Hard-code the keys that are hard-coded in the browse()
# controller. Merge with the pluggable ones from needles:
doc = dict(# Some non-array fields:
folder=folder_name,
name=file_name,
size=file_info.st_size,
modified=datetime.fromtimestamp(file_info.st_mtime),
is_folder=False,

# And these, which all get mashed into arrays:
**needles)
links = [{'order': order,
'heading': heading,
'items': [{'icon': icon,
'title': title,
'href': href}
for icon, title, href in items]}
for order, heading, items in
chain.from_iterable(linkses)]
if links:
doc['links'] = links
yield es.index_op(doc, doc_type=FILE)

# Index all the lines. If it's an empty file (no lines), don't bother
# ES. It hates empty dicts.
Expand Down
3 changes: 2 additions & 1 deletion dxr/config.py
Expand Up @@ -207,7 +207,8 @@ def __init__(self, name, unvalidated_tree, sections, config):
Optional('enabled_plugins', default=plugin_list('*')): Plugins,
Optional('ignore_patterns',
default=['.hg', '.git', 'CVS', '.svn', '.bzr',
'.deps', '.libs']): WhitespaceList,
'.deps', '.libs', '.DS_Store', '.nfs*', '*~',
'._*']): WhitespaceList,
Optional('object_folder', default=None): AbsPath,
'source_folder': AbsPath,
Optional('source_encoding', default='utf-8'): basestring,
Expand Down
11 changes: 9 additions & 2 deletions dxr/plugins/core.py
Expand Up @@ -83,6 +83,10 @@
'type': 'binary',
'index': 'no'
},
'is_binary': { # assumed False if not present
'type': 'boolean',
'index': 'no'
},

# Sidebar nav links:
'links': {
Expand Down Expand Up @@ -381,6 +385,9 @@ def needles(self):
bytestring = (self.contents.encode('utf-8') if self.contains_text()
else self.contents)
yield 'raw_data', b64encode(bytestring)
# binary, but not an image
elif not self.contains_text():
yield 'is_binary', True

def needles_by_line(self):
"""Fill out line number and content for every line."""
Expand All @@ -389,8 +396,8 @@ def needles_by_line(self):
('content', text)]

def is_interesting(self):
"""Core plugin puts text and image files in the search index."""
return self.contains_text() or is_image(self.path)
"""Core plugin puts all files in the search index."""
return True


# Match file name and line number: filename:n. Strip leading slashes because
Expand Down
7 changes: 7 additions & 0 deletions dxr/static/css/dxr.css
Expand Up @@ -148,6 +148,13 @@ td#line-numbers span {
text-align: right;
padding: 0 0.5rem;
}
tr.binary_row > td {
padding-top: .5em;
padding-bottom: .5em;
}
tr.binary_row {
opacity: 0.6;
}
.highlighted,
.multihighlight {
background: none repeat scroll 0 0 rgb(255, 255, 204);
Expand Down
20 changes: 14 additions & 6 deletions dxr/static/templates/folder.html
Expand Up @@ -32,12 +32,20 @@
</tr>
</thead>
<tbody>
{% for icon, name, modified, size, relative_url in files_and_folders %}
<tr>
<td><a href="{{ relative_url }}" class="icon {{ icon }}">{{ name }}</a></td>
<td><a href="{{ relative_url }}">{{ '' if modified is none else modified.strftime("%Y %b %d %H:%m") }}</a></td>
<td><a href="{{ relative_url }}">{{ '' if size is none else size|filesizeformat }}</a></td>
</tr>
{% for icon, name, modified, size, relative_url, is_binary in files_and_folders %}
{% if is_binary %}
<tr class="binary_row">
<td><span class="icon {{ icon }}">{{ name }}</span></td>
<td>{{ '' if modified is none else modified.strftime("%Y %b %d %H:%m") }}</td>
<td>{{ '' if size is none else size|filesizeformat }}</td>
</tr>
{% else %}
<tr>
<td><a href="{{ relative_url }}" class="icon {{ icon }}">{{ name }}</a></td>
<td><a href="{{ relative_url }}">{{ '' if modified is none else modified.strftime("%Y %b %d %H:%m") }}</a></td>
<td><a href="{{ relative_url }}">{{ '' if size is none else size|filesizeformat }}</a></td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
Expand Down
6 changes: 4 additions & 2 deletions tests/test_binary_files/test_binary_files.py
Expand Up @@ -38,6 +38,8 @@ def test_svg(self):
ok_(response.status_code, 200)

def test_some_bytes(self):
"""We do not want some_bytes to show in the folder index."""
"""We want some_bytes to show in the folder index, but without its own page."""
response = self.client().get('/code/source/')
ok_('some_bytes' not in response.data)
ok_('some_bytes' in response.data)
response = self.client().get('/code/source/some_bytes')
ok_(response.status_code, 404)

0 comments on commit 53846ce

Please sign in to comment.