Skip to content

Fix ResourceName delimiters #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/viam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ def _log_exceptions(exctype, value, traceback):
##################
# MONKEY PATCHES #
##################
_ResourceName.__hash__ = lambda self: hash(f"{self.namespace}/{self.type}/{self.subtype}/{self.name}")
_ResourceName.__hash__ = lambda self: hash(f"{self.namespace}:{self.type}:{self.subtype}/{self.name}")
2 changes: 1 addition & 1 deletion src/viam/module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async def reconfigure_resource(self, request: ReconfigureResourceRequest):
await self.add_resource(add_request)

async def remove_resource(self, request: RemoveResourceRequest):
rn = resource_name_from_string(request.name.replace("/", ":"))
rn = resource_name_from_string(request.name)
resource = self.server.get_component(ComponentBase, rn)
if isinstance(resource, Stoppable):
if iscoroutinefunction(resource.stop):
Expand Down
16 changes: 12 additions & 4 deletions src/viam/resource/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def from_string(cls, model: str, *, ignore_errors=False) -> Self:


def resource_name_from_string(string: str) -> ResourceName:
"""Create a ResourceName from its string representation (namespace:resource_type:resource_subtype:name)
"""Create a ResourceName from its string representation (namespace:resource_type:resource_subtype/<optional_remote:>name)

Args:
string (str): The ResourceName as a string
Expand All @@ -185,10 +185,18 @@ def resource_name_from_string(string: str) -> ResourceName:
Returns:
ResourceName: The new ResourceName
"""
parts = string.split(":")
if len(parts) < 4:
regex = re.compile(r"^([\w-]+:[\w-]+:(?:[\w-]+))\/?([\w-]+:(?:[\w-]+:)*)?(.+)?$")
match = regex.match(string)
if not match:
raise ValueError(f"{string} is not a valid ResourceName")
return ResourceName(namespace=parts[0], type=parts[1], subtype=parts[2], name=":".join(parts[3:]))
parts = match[1].split(":")
if len(parts) != 3:
raise ValueError(f"{string} is not a valid ResourceName")
if match[2]:
name = f"{match[2]}{match[3]}"
else:
name = match[3]
return ResourceName(namespace=parts[0], type=parts[1], subtype=parts[2], name=name)


class ResourceBase(Protocol):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ def test_model():

def test_resource_name_from_string():
# normal
rn = resource_name_from_string("namespace:type:subtype:name")
rn = resource_name_from_string("namespace:type:subtype/name")
assert rn.namespace == "namespace"
assert rn.type == "type"
assert rn.subtype == "subtype"
assert rn.name == "name"

# remote
rn = resource_name_from_string("ns:tp:st:remote:nm")
rn = resource_name_from_string("ns:tp:st/remote:nm")
assert rn.namespace == "ns"
assert rn.type == "tp"
assert rn.subtype == "st"
Expand Down