Skip to content

Commit

Permalink
device: Minor refactor
Browse files Browse the repository at this point in the history
* And added a new function: is_resource_exist(self, resource_name: str)
  • Loading branch information
hakimifr committed Jan 25, 2024
1 parent 5a98a12 commit 57e6d0e
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions yeet_api/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ class _DeviceJSON:
"""

def __init__(self, devicejson: Path) -> None:
def __init__(self, jsonfile: Path) -> None:
self._json: dict

with open(devicejson, "r") as f:
with open(jsonfile, "r") as f:
self._json = json.load(f)

# JSON sanity checks
# We don't want array json
if not isinstance(self._json, dict):
raise exceptions.InvalidConfigError(
"JSON must contain an object (dict), "
f"not an array (list) [{devicejson.name}]"
f"not an array (list) [{jsonfile.name}]"
)

if (
Expand All @@ -57,7 +57,7 @@ def __init__(self, devicejson: Path) -> None:
raise exceptions.InvalidConfigError(
"Incomplete device config; must "
"consist of: {fullname, codename, "
f"resources}} [{devicejson.name}]"
f"resources}} [{jsonfile.name}]"
)

@property
Expand Down Expand Up @@ -119,7 +119,7 @@ def codename(self) -> str:
"""Get device codename."""
return self.devicejson.json.get("codename", "")

def get_available_resources(self) -> tuple[str]:
def get_available_resources(self) -> tuple[str, ...]:
"""Get available resources provided by the device JSON.
Returns:
Expand All @@ -134,6 +134,10 @@ def get_available_resources(self) -> tuple[str]:
# raised.
return tuple(self.devicejson.json["resources"].keys())

def is_resource_exist(self, resource_name: str) -> bool:
"""Determine whether a resource exist."""
return bool(self.devicejson.json["resources"].get(resource_name))

def get_resource(self, resource_name: str) -> dict | None:
"""Get a resource for a device.
Expand All @@ -156,4 +160,4 @@ def update_database(self, **kwargs) -> None:
set. Pass rebase=False to override.
"""
self.repo.remote().pull(**{"rebase": True} | kwargs)
self.repo.remote().pull(**{"rebase": True} | kwargs) # type: ignore

0 comments on commit 57e6d0e

Please sign in to comment.