Added support for multiple fnmatch filters

pull/37/head
Thomas Bouve 2021-02-07 23:44:51 +01:00
rodzic 09687ebfdb
commit 32ea5b05a8
5 zmienionych plików z 30 dodań i 3 usunięć

Wyświetl plik

@ -95,6 +95,10 @@ fc.selected_filename
## Release notes
### 0.4.2
- Added ability to specify a list of `fnmatch` pattern strings for `filter_pattern`
### 0.4.1
- Fixed issue with `select_default` not being applied on `reset`

Wyświetl plik

@ -1,3 +1,3 @@
from .filechooser import FileChooser
__version__ = '0.4.1'
__version__ = '0.4.2'

Wyświetl plik

@ -32,6 +32,19 @@ def has_parent(path):
return os.path.basename(path) != ''
def match_item(item, filter_pattern):
"""Check if a string matches one or more fnmatch patterns."""
idx = 0
found = False
patterns = filter_pattern if isinstance(filter_pattern, list) else [filter_pattern]
while idx < len(patterns) and not found:
found |= fnmatch.fnmatch(item, patterns[idx])
idx += 1
return found
def get_dir_contents(
path,
show_hidden=False,
@ -52,7 +65,7 @@ def get_dir_contents(
dirs.append(item)
elif append and not show_only_dirs:
if filter_pattern:
if fnmatch.fnmatch(item, filter_pattern):
if match_item(item, filter_pattern):
files.append(item)
else:
files.append(item)

Wyświetl plik

@ -113,6 +113,16 @@
"fdialog.filter_pattern = '*.txt'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Set multiple file filter patterns (uses https://docs.python.org/3/library/fnmatch.html)\n",
"fdialog.filter_pattern = ['*.jpg', '*.png']"
]
},
{
"cell_type": "code",
"execution_count": null,

Wyświetl plik

@ -10,7 +10,7 @@ def read(fname):
setup(
name='ipyfilechooser',
version='0.4.1',
version='0.4.2',
author='Thomas Bouve (@crahan)',
author_email='crahan@n00.be',
description=(