Skip to content

Commit

Permalink
ensure path separators are consistent (#805)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximlt authored Jul 31, 2023
1 parent ea6b366 commit 978af55
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import re
import datetime as dt
import collections
import pathlib
import typing
import warnings

Expand Down Expand Up @@ -2483,7 +2484,13 @@ def _on_set(self, attribute, old, new):
self.update()

def update(self, default=True):
self.objects = sorted(glob.glob(self.path))
if self.path == "":
self.objects = []
else:
# Convert using os.fspath and pathlib.Path to handle ensure
# the path separators are consistent (on Windows in particular)
pathpattern = os.fspath(pathlib.Path(self.path))
self.objects = sorted(glob.glob(pathpattern))
if self.default in self.objects:
return
if default:
Expand Down
24 changes: 24 additions & 0 deletions tests/testfileselector.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,27 @@ def test_update_file_removed(self):
p.param.b.update()
assert p.param.b.objects == [self.fb]
assert p.param.b.default == self.fb


def test_fileselector_glob_parent(tmpdir):
# https://github.com/holoviz/param/issues/139

ncwd = tmpdir / 'folder'
data = tmpdir / 'data'
data.mkdir()
(data / 'foo.txt').write_text('foo', encoding='utf-8')
ncwd.mkdir()
cwd = os.getcwd()
os.chdir(ncwd)
try:
if os.name == 'nt':
default = r'..\data\foo.txt'
else:
default = '../data/foo.txt'
class P(param.Parameterized):
fs = param.FileSelector(default, path='../data/*.txt')

assert len(P.param.fs.objects) == 1
assert P.fs == default
finally:
os.chdir(cwd)

0 comments on commit 978af55

Please sign in to comment.