Added get_file_list function and tests.
This commit is contained in:
parent
b8809f879d
commit
e6521b047d
@ -25,6 +25,24 @@ from qutebrowser.utils import jinja
|
||||
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):
|
||||
"""Get the directory browser web page.
|
||||
|
||||
@ -42,24 +60,13 @@ def dirbrowser(urlstring):
|
||||
folder = resource_filename('img/folder.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:
|
||||
parent = None
|
||||
else:
|
||||
parent = os.path.dirname(urlstring)
|
||||
all_files = os.listdir(urlstring)
|
||||
files = sorted([{'name': file, 'absname': os.path.join(urlstring, file)}
|
||||
for file in all_files if is_file(file)],
|
||||
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())
|
||||
files = get_file_list(urlstring, all_files, os.path.isfile)
|
||||
directories = get_file_list(urlstring, all_files, os.path.isdir)
|
||||
html = template.render(title=title, url=urlstring, icon='', parent=parent,
|
||||
files=files, directories=directories, folder=folder,
|
||||
file=file)
|
||||
|
55
tests/browser/test_dirbrowser.py
Normal file
55
tests/browser/test_dirbrowser.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user