Skip to content

Commit

Permalink
Remove query modifiers in favor of all()/hidden()
Browse files Browse the repository at this point in the history
  • Loading branch information
frthjf committed May 25, 2024
1 parent a30841c commit 26b89d2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/machinable/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ def singleton(
context = instance.compute_context()

for candidate in self:
if candidate.matches(context):
if candidate.matches(context) and not candidate.hidden():
return candidate

return instance
Expand Down
5 changes: 4 additions & 1 deletion src/machinable/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ def cached(
self.save_file("cached", str(reason))
return True
elif cached is False:
os.remove(self.local_directory("cached"), ignore_errors=True)
try:
os.remove(self.local_directory("cached"))
except OSError:
pass

return cached

Expand Down
18 changes: 17 additions & 1 deletion src/machinable/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def singleton(
module,
version,
**kwargs,
)
).filter(lambda x: not x.hidden())

if candidates:
return candidates[-1]
Expand Down Expand Up @@ -780,6 +780,22 @@ def future(self) -> Optional[Self]:

return self

def hidden(
self, hidden: Optional[bool] = None, reason: str = "user"
) -> bool:
if hidden is None:
return self.load_file("hidden", None) is not None
elif hidden is True:
self.save_file("hidden", str(reason))
return True
elif hidden is False:
try:
os.remove(self.local_directory("hidden"))
except OSError:
pass

return hidden

# a posteriori modifiers

def all(self) -> "InterfaceCollection":
Expand Down
63 changes: 0 additions & 63 deletions src/machinable/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,66 +39,3 @@ def new(
) -> Interface:
module, version = extend(module, version)
return Interface.make(module, version, **kwargs)

def or_none(
self,
module: Union[None, str, Interface] = None,
version: VersionType = None,
**kwargs,
) -> Optional[Interface]:
module, version = extend(module, version)
existing = Interface.find(module, version, **kwargs)
if existing:
return existing[-1]

return None

def or_fail(
self,
module: Union[None, str, Interface] = None,
version: VersionType = None,
**kwargs,
) -> Interface:
module, version = extend(module, version)
existing = Interface.find(module, version, **kwargs)
if existing:
return existing[-1]

raise ValueError(
f"Could not find {module}{normversion(version)} ({kwargs})"
)

def cached_or_fail(
self,
module: Union[None, str, Interface] = None,
version: VersionType = None,
**kwargs,
) -> Interface:
module, version = extend(module, version)
existing = Interface.find(module, version, **kwargs)
if existing:
for candidate in existing:
if not hasattr(candidate, "cached"):
continue
if candidate.cached():
return candidate

raise ValueError(
f"Could not find {module}{normversion(version)} ({kwargs})"
)

def prefer_cached(
self,
module: Union[None, str, Interface] = None,
version: VersionType = None,
**kwargs,
) -> Optional[Interface]:
module, version = extend(module, version)
existing = Interface.find(module, version, **kwargs)
if existing:
for i in range(len(existing) - 1):
if existing[i].cached():
return existing[i]
return existing[-1]

return Interface.make(module, version, **kwargs)
26 changes: 8 additions & 18 deletions tests/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,24 +255,14 @@ def test_interface_modifiers(tmp_storage):
# new
assert get.new("interface.dummy").is_committed() is False

# or
assert get.or_none("interface.dummy") is not None
assert get.or_none("interface.dummy", {"a": 2}) is None
assert get.or_fail("interface.dummy").is_committed()
with pytest.raises(ValueError):
get.or_fail("interface.dummy", {"a": 2})
with pytest.raises(ValueError):
get.cached_or_fail("dummy")
d = get("hello").launch()
assert get.cached_or_fail("hello") == d

# preferring cached
d1 = get.new("dummy").commit()
d2 = get.new("dummy").launch()
d3 = get.new("dummy").commit()
assert get("dummy") == d3
assert get.prefer_cached("dummy") == d2
assert not get.prefer_cached("dummy", {"a": -100}).is_committed()
# hide
c = get("interface.dummy")
uid = c.uuid
assert not c.hidden()
c.hidden(True)
assert get("interface.dummy").uuid != uid
c.hidden(False)
assert get("interface.dummy").uuid == uid

project.__exit__()

Expand Down

0 comments on commit 26b89d2

Please sign in to comment.