From e6521b047d74504e04f85ee1e6a930fddf5058d5 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 8 Aug 2015 14:10:27 -0400 Subject: [PATCH] Added get_file_list function and tests. --- qutebrowser/browser/dirbrowser.py | 33 +++++++++++-------- tests/browser/test_dirbrowser.py | 55 +++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 tests/browser/test_dirbrowser.py diff --git a/qutebrowser/browser/dirbrowser.py b/qutebrowser/browser/dirbrowser.py index 6145e4819..e82eba958 100644 --- a/qutebrowser/browser/dirbrowser.py +++ b/qutebrowser/browser/dirbrowser.py @@ -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) diff --git a/tests/browser/test_dirbrowser.py b/tests/browser/test_dirbrowser.py new file mode 100644 index 000000000..cca017338 --- /dev/null +++ b/tests/browser/test_dirbrowser.py @@ -0,0 +1,55 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2015 Antoni Boucher (antoyo) +# +# 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 . + +"""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)