Skip to content

Commit 709aeb5

Browse files
authored
Fix ResourceName delimiters (#235)
1 parent fbc6a60 commit 709aeb5

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

src/viam/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ def _log_exceptions(exctype, value, traceback):
4141
##################
4242
# MONKEY PATCHES #
4343
##################
44-
_ResourceName.__hash__ = lambda self: hash(f"{self.namespace}/{self.type}/{self.subtype}/{self.name}")
44+
_ResourceName.__hash__ = lambda self: hash(f"{self.namespace}:{self.type}:{self.subtype}/{self.name}")

src/viam/module/module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ async def reconfigure_resource(self, request: ReconfigureResourceRequest):
134134
await self.add_resource(add_request)
135135

136136
async def remove_resource(self, request: RemoveResourceRequest):
137-
rn = resource_name_from_string(request.name.replace("/", ":"))
137+
rn = resource_name_from_string(request.name)
138138
resource = self.server.get_component(ComponentBase, rn)
139139
if isinstance(resource, Stoppable):
140140
if iscoroutinefunction(resource.stop):

src/viam/resource/types.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def from_string(cls, model: str, *, ignore_errors=False) -> Self:
174174

175175

176176
def resource_name_from_string(string: str) -> ResourceName:
177-
"""Create a ResourceName from its string representation (namespace:resource_type:resource_subtype:name)
177+
"""Create a ResourceName from its string representation (namespace:resource_type:resource_subtype/<optional_remote:>name)
178178
179179
Args:
180180
string (str): The ResourceName as a string
@@ -185,10 +185,18 @@ def resource_name_from_string(string: str) -> ResourceName:
185185
Returns:
186186
ResourceName: The new ResourceName
187187
"""
188-
parts = string.split(":")
189-
if len(parts) < 4:
188+
regex = re.compile(r"^([\w-]+:[\w-]+:(?:[\w-]+))\/?([\w-]+:(?:[\w-]+:)*)?(.+)?$")
189+
match = regex.match(string)
190+
if not match:
190191
raise ValueError(f"{string} is not a valid ResourceName")
191-
return ResourceName(namespace=parts[0], type=parts[1], subtype=parts[2], name=":".join(parts[3:]))
192+
parts = match[1].split(":")
193+
if len(parts) != 3:
194+
raise ValueError(f"{string} is not a valid ResourceName")
195+
if match[2]:
196+
name = f"{match[2]}{match[3]}"
197+
else:
198+
name = match[3]
199+
return ResourceName(namespace=parts[0], type=parts[1], subtype=parts[2], name=name)
192200

193201

194202
class ResourceBase(Protocol):

tests/test_registry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ def test_model():
125125

126126
def test_resource_name_from_string():
127127
# normal
128-
rn = resource_name_from_string("namespace:type:subtype:name")
128+
rn = resource_name_from_string("namespace:type:subtype/name")
129129
assert rn.namespace == "namespace"
130130
assert rn.type == "type"
131131
assert rn.subtype == "subtype"
132132
assert rn.name == "name"
133133

134134
# remote
135-
rn = resource_name_from_string("ns:tp:st:remote:nm")
135+
rn = resource_name_from_string("ns:tp:st/remote:nm")
136136
assert rn.namespace == "ns"
137137
assert rn.type == "tp"
138138
assert rn.subtype == "st"

0 commit comments

Comments
 (0)