Added get_file_list function and tests.

This commit is contained in:
Antoni Boucher 2015-08-08 14:10:27 -04:00
parent b8809f879d
commit e6521b047d
2 changed files with 75 additions and 13 deletions

View File

@ -25,6 +25,24 @@ from qutebrowser.utils import jinja
from qutebrowser.utils.utils import resource_filename from qutebrowser.utils.utils import resource_filename
def get_file_list(basedir, all_files, filterfunc):
"""Get a list of files filtered by a filter function and sorted by name.
Args:
basedir: The parent directory of all files.
all_files: The list of files to filter and sort.
filterfunc: The filter function.
Return:
A list of dicts. Each dict contains the name and absname keys.
"""
items = [{'name': filename, 'absname':
os.path.abspath(os.path.join(basedir, filename))}
for filename in all_files
if filterfunc(os.path.join(basedir, filename))]
return sorted(items, key=lambda v: v['name'].lower())
def dirbrowser(urlstring): def dirbrowser(urlstring):
"""Get the directory browser web page. """Get the directory browser web page.
@ -42,24 +60,13 @@ def dirbrowser(urlstring):
folder = resource_filename('img/folder.png') folder = resource_filename('img/folder.png')
file = resource_filename('img/file.png') file = resource_filename('img/file.png')
def is_file(file):
return os.path.isfile(os.path.join(urlstring, file))
def is_dir(file):
return os.path.isdir(os.path.join(urlstring, file))
if os.path.dirname(urlstring) == urlstring: if os.path.dirname(urlstring) == urlstring:
parent = None parent = None
else: else:
parent = os.path.dirname(urlstring) parent = os.path.dirname(urlstring)
all_files = os.listdir(urlstring) all_files = os.listdir(urlstring)
files = sorted([{'name': file, 'absname': os.path.join(urlstring, file)} files = get_file_list(urlstring, all_files, os.path.isfile)
for file in all_files if is_file(file)], directories = get_file_list(urlstring, all_files, os.path.isdir)
key=lambda v: v['name'].lower())
directories = sorted([{'name': file, 'absname': os.path.join(urlstring,
file)}
for file in all_files if is_dir(file)],
key=lambda v: v['name'].lower())
html = template.render(title=title, url=urlstring, icon='', parent=parent, html = template.render(title=title, url=urlstring, icon='', parent=parent,
files=files, directories=directories, folder=folder, files=files, directories=directories, folder=folder,
file=file) file=file)

View File

@ -0,0 +1,55 @@
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2015 Antoni Boucher (antoyo) <bouanto@zoho.com>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Tests for qutebrowser.browser.dirbrowser."""
import os
from qutebrowser.browser.dirbrowser import get_file_list
class TestFileList:
"""Test file list."""
def test_get_file_list(self):
"""Test get_file_list."""
basedir = os.path.abspath('./qutebrowser/utils')
all_files = os.listdir(basedir)
result = get_file_list(basedir, all_files, os.path.isfile)
assert {'name': 'testfile', 'absname': os.path.join(basedir,
'testfile')} in result
"""Test get_file_list with os.path.isdir filter function."""
basedir = os.path.abspath('./qutebrowser/utils')
all_files = os.listdir(basedir)
result = get_file_list(basedir, all_files, os.path.isdir)
print(result)
assert {'name': 'testfile', 'absname': os.path.join(basedir,
'testfile')} not in result
basedir = os.path.abspath('./qutebrowser')
all_files = os.listdir(basedir)
result = get_file_list(basedir, all_files, os.path.isfile)
assert ({'name': 'utils', 'absname': os.path.join(basedir, 'utils')}
not in result)
result = get_file_list(basedir, all_files, os.path.isdir)
assert ({'name': 'utils', 'absname': os.path.join(basedir, 'utils')}
in result)