Skip to content

Commit

Permalink
FileSelector defaults to the first path (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximlt authored Jul 28, 2023
1 parent 89718fe commit 754dcfd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
13 changes: 8 additions & 5 deletions param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2471,20 +2471,23 @@ def __init__(
def __init__(self, default=Undefined, *, path="", **kwargs):
self.default = default
self.path = path
self.update()
super().__init__(default=default, objects=self.objects,
empty_default=True, **kwargs)
if default is Undefined:
self.update()
else:
self.update(default=False)
super().__init__(default=self.default, objects=self.objects, **kwargs)

def _on_set(self, attribute, old, new):
super()._on_set(attribute, new, old)
if attribute == 'path':
self.update()

def update(self):
def update(self, default=True):
self.objects = sorted(glob.glob(self.path))
if self.default in self.objects:
return
self.default = self.objects[0] if self.objects else None
if default:
self.default = self.objects[0] if self.objects else None

def get_range(self):
return _abbreviate_paths(self.path,super().get_range())
Expand Down
5 changes: 2 additions & 3 deletions tests/testfileselector.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ def test_defaults_unbound(self):
check_defaults(s, label=None)
self._check_defaults(s)

def test_default_is_None(self):
def test_default_to_first(self):
p = self.P()
assert p.a is None
assert p.param.a.default is None
assert p.a == p.param.a.objects[0]

def test_default_is_honored(self):
p = self.P()
Expand Down

0 comments on commit 754dcfd

Please sign in to comment.