mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-24 17:06:18 -05:00
Using folder outline for empty folders.
This commit is contained in:
parent
207991ad80
commit
f1f1f42a1c
4 changed files with 35 additions and 70 deletions
|
|
@ -29,7 +29,7 @@ function depthIndentation(path) {
|
|||
|
||||
function Folder({folder, onFolderClick, sortData, selectedPath, onFileClick, onSelect}) {
|
||||
// Creates a single table row for a folder, or a row for itself and indented rows for its children.
|
||||
let {path, children} = folder;
|
||||
let {path, children, is_empty} = folder;
|
||||
const pathWithNoTrailingSlash = path.substring(0, path.length - 1);
|
||||
const name = path.substring(pathWithNoTrailingSlash.lastIndexOf('/') + 1);
|
||||
const f = <TableRow key={path}>
|
||||
|
|
@ -38,10 +38,10 @@ function Folder({folder, onFolderClick, sortData, selectedPath, onFileClick, onS
|
|||
</TableCell>
|
||||
<TableCell onClick={() => onFolderClick(path)} className='file-path'>
|
||||
{depthIndentation(pathWithNoTrailingSlash)}
|
||||
<Icon name='folder'/>
|
||||
{is_empty ? <Icon name='folder outline'/> : <Icon name='folder'/>}
|
||||
{name}
|
||||
</TableCell>
|
||||
<TableCell>({folder['child_count']})</TableCell>
|
||||
<TableCell/>
|
||||
</TableRow>;
|
||||
if (children && !_.isEmpty(children)) {
|
||||
// Folder has children, recursively display them.
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import subprocess
|
|||
from pathlib import Path
|
||||
from typing import List, Tuple, Union, Dict
|
||||
|
||||
import cachetools.func
|
||||
import psycopg2
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
|
|
@ -41,26 +40,6 @@ __all__ = ['list_directories_contents', 'delete_file', 'split_path_stem_and_suff
|
|||
'split_file_name_words']
|
||||
|
||||
|
||||
def filter_parent_directories(directories: List[Path]) -> List[Path]:
|
||||
"""
|
||||
Remove parent directories if their children are in the list.
|
||||
|
||||
>>> filter_parent_directories([Path('foo'), Path('foo/bar'), Path('baz')])
|
||||
[Path('foo/bar'), Path('baz')]
|
||||
"""
|
||||
unique_children = set()
|
||||
for directory in sorted(directories):
|
||||
for parent in directory.parents:
|
||||
# Remove any parent of this child.
|
||||
if parent in unique_children:
|
||||
unique_children.remove(parent)
|
||||
unique_children.add(directory)
|
||||
|
||||
# Restore the original order.
|
||||
new_directories = [i for i in directories if i in unique_children]
|
||||
return new_directories
|
||||
|
||||
|
||||
def _get_file_dict(file: pathlib.Path) -> Dict:
|
||||
media_directory = get_media_directory()
|
||||
return dict(
|
||||
|
|
@ -70,27 +49,23 @@ def _get_file_dict(file: pathlib.Path) -> Dict:
|
|||
)
|
||||
|
||||
|
||||
@cachetools.func.ttl_cache(maxsize=1000, ttl=30)
|
||||
def _get_directory_child_count(directory: pathlib.Path) -> int:
|
||||
return len(list(directory.iterdir()))
|
||||
|
||||
|
||||
def _get_directory_dict(directory: pathlib.Path, directories: List[pathlib.Path]) -> Dict:
|
||||
def __get_directory_dict(directory: pathlib.Path) -> Dict:
|
||||
media_directory = get_media_directory()
|
||||
d = dict(
|
||||
return dict(
|
||||
path=f'{directory.relative_to(media_directory)}/',
|
||||
child_count=_get_directory_child_count(directory),
|
||||
is_empty=not next(directory.iterdir(), False),
|
||||
)
|
||||
|
||||
|
||||
def _get_recursive_directory_dict(directory: pathlib.Path, directories: List[pathlib.Path]) -> Dict:
|
||||
d = __get_directory_dict(directory)
|
||||
if directory in directories:
|
||||
children = dict()
|
||||
for path in directory.iterdir():
|
||||
if path.is_dir() and path in directories:
|
||||
children[f'{path.name}/'] = _get_directory_dict(path, directories)
|
||||
children[f'{path.name}/'] = _get_recursive_directory_dict(path, directories)
|
||||
elif path.is_dir():
|
||||
children[f'{path.name}/'] = dict(
|
||||
path=f'{path.relative_to(media_directory)}/',
|
||||
child_count=_get_directory_child_count(path),
|
||||
)
|
||||
children[f'{path.name}/'] = __get_directory_dict(path)
|
||||
else:
|
||||
children[path.name] = _get_file_dict(path)
|
||||
d['children'] = children
|
||||
|
|
@ -121,7 +96,7 @@ def list_directories_contents(directories_: List[str]) -> Dict:
|
|||
# Never show ignored directories.
|
||||
continue
|
||||
if path.is_dir():
|
||||
paths[f'{path.name}/'] = _get_directory_dict(path, directories)
|
||||
paths[f'{path.name}/'] = _get_recursive_directory_dict(path, directories)
|
||||
else:
|
||||
paths[path.name] = _get_file_dict(path)
|
||||
|
||||
|
|
|
|||
|
|
@ -34,16 +34,16 @@ def test_list_files_api(test_client, make_files_structure, test_directory):
|
|||
|
||||
# Requesting no directories results in the top-level results.
|
||||
expected = {
|
||||
'archives/': {'path': 'archives/'},
|
||||
'empty directory/': {'path': 'empty directory/'},
|
||||
'videos/': {'path': 'videos/'}
|
||||
'archives/': {'path': 'archives/', 'is_empty': False},
|
||||
'empty directory/': {'path': 'empty directory/', 'is_empty': True},
|
||||
'videos/': {'path': 'videos/', 'is_empty': False}
|
||||
}
|
||||
check_get_files([], expected)
|
||||
# empty directory is empty
|
||||
expected = {
|
||||
'archives/': {'path': 'archives/'},
|
||||
'empty directory/': {'path': 'empty directory/', 'children': {}},
|
||||
'videos/': {'path': 'videos/'}
|
||||
'archives/': {'path': 'archives/', 'is_empty': False},
|
||||
'empty directory/': {'path': 'empty directory/', 'children': {}, 'is_empty': True},
|
||||
'videos/': {'path': 'videos/', 'is_empty': False}
|
||||
}
|
||||
check_get_files(['empty directory'], expected)
|
||||
|
||||
|
|
@ -53,11 +53,12 @@ def test_list_files_api(test_client, make_files_structure, test_directory):
|
|||
'children': {
|
||||
'foo.txt': {'path': 'archives/foo.txt', 'size': 0, 'mimetype': 'inode/x-empty'},
|
||||
'bar.txt': {'path': 'archives/bar.txt', 'size': 12, 'mimetype': 'text/plain'},
|
||||
'baz/': {'path': 'archives/baz/'},
|
||||
}
|
||||
'baz/': {'path': 'archives/baz/', 'is_empty': False},
|
||||
},
|
||||
'is_empty': False,
|
||||
},
|
||||
'empty directory/': {'path': 'empty directory/'},
|
||||
'videos/': {'path': 'videos/'}
|
||||
'empty directory/': {'path': 'empty directory/', 'is_empty': True},
|
||||
'videos/': {'path': 'videos/', 'is_empty': False}
|
||||
}
|
||||
check_get_files(['archives'], expected)
|
||||
|
||||
|
|
@ -73,12 +74,14 @@ def test_list_files_api(test_client, make_files_structure, test_directory):
|
|||
'children': {
|
||||
'bar.txt': {'path': 'archives/baz/bar.txt', 'size': 0, 'mimetype': 'inode/x-empty'},
|
||||
'foo.txt': {'path': 'archives/baz/foo.txt', 'size': 0, 'mimetype': 'inode/x-empty'},
|
||||
}
|
||||
},
|
||||
'is_empty': False,
|
||||
}
|
||||
}
|
||||
},
|
||||
'is_empty': False
|
||||
},
|
||||
'empty directory/': {'path': 'empty directory/'},
|
||||
'videos/': {'path': 'videos/'},
|
||||
'empty directory/': {'path': 'empty directory/', 'is_empty': True},
|
||||
'videos/': {'path': 'videos/', 'is_empty': False},
|
||||
}
|
||||
check_get_files(['archives', 'archives/baz'], expected)
|
||||
# Requesting only a subdirectory also returns `archives` contents.
|
||||
|
|
@ -90,16 +93,17 @@ def test_list_files_api(test_client, make_files_structure, test_directory):
|
|||
'children': {
|
||||
'foo.txt': {'path': 'archives/foo.txt', 'size': 0, 'mimetype': 'inode/x-empty'},
|
||||
'bar.txt': {'path': 'archives/bar.txt', 'size': 12, 'mimetype': 'text/plain'},
|
||||
'baz/': {'path': 'archives/baz/'},
|
||||
}
|
||||
'baz/': {'path': 'archives/baz/', 'is_empty': False},
|
||||
}, 'is_empty': False
|
||||
},
|
||||
'empty directory/': {'path': 'empty directory/'},
|
||||
'empty directory/': {'path': 'empty directory/', 'is_empty': True},
|
||||
'videos/': {
|
||||
'path': 'videos/',
|
||||
'children': {
|
||||
'other video.mp4': {'path': 'videos/other video.mp4', 'size': 0, 'mimetype': 'inode/x-empty'},
|
||||
'some video.mp4': {'path': 'videos/some video.mp4', 'size': 0, 'mimetype': 'inode/x-empty'},
|
||||
}
|
||||
},
|
||||
'is_empty': False
|
||||
}
|
||||
}
|
||||
check_get_files(['archives', 'videos'], expected)
|
||||
|
|
|
|||
|
|
@ -25,20 +25,6 @@ def assert_files(session: Session, expected):
|
|||
assert files == set(expected)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'directories,expected', [
|
||||
([Path('foo')], [Path('foo')]),
|
||||
([Path('foo'), Path('bar')], [Path('foo'), Path('bar')]),
|
||||
([Path('foo'), Path('foo/bar'), Path('baz')], [Path('foo/bar'), Path('baz')]),
|
||||
([Path('foo/bar'), Path('foo'), Path('baz')], [Path('foo/bar'), Path('baz')]),
|
||||
([Path('baz'), Path('foo/bar'), Path('foo')], [Path('baz'), Path('foo/bar')]),
|
||||
([Path('foo/bar'), Path('foo'), Path('foo/bar/baz')], [Path('foo/bar/baz')]),
|
||||
]
|
||||
)
|
||||
def test_filter_parent_directories(directories, expected):
|
||||
assert lib.filter_parent_directories(directories) == expected
|
||||
|
||||
|
||||
def test_delete_file(make_files_structure, test_directory):
|
||||
"""
|
||||
File in the media directory can be deleted.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue