Skip to content

Commit 353849a

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
sambacc: move rados pesudo-uri parsing to top level function
Parsing the URI(ish) string is not dependent on rados libs, move the parsing function to the module scope so that it can be reused elsewhere. Signed-off-by: John Mulligan <[email protected]>
1 parent 0dea0eb commit 353849a

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

sambacc/rados_opener.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def rados_open(self, req: urllib.request.Request) -> typing.IO:
6767
"""Open a rados-style url. Called from urllib."""
6868
if self._interface is None:
6969
raise RADOSUnsupported()
70-
rinfo = self._parse_req(req)
70+
rinfo = parse_rados_uri(req)
7171
if rinfo.get("subtype") == "mon-config-key":
7272
return _get_mon_config_key(self._interface, rinfo["path"])
7373
return RADOSObjectRef(
@@ -83,7 +83,7 @@ def get_object(
8383
"""
8484
if self._interface is None:
8585
raise RADOSUnsupported()
86-
rinfo = self._parse_req(urllib.request.Request(uri))
86+
rinfo = parse_rados_uri(urllib.request.Request(uri))
8787
if rinfo.get("type") != "rados":
8888
raise ValueError("only rados URI values supported")
8989
if rinfo.get("subtype") == "mon-config-key":
@@ -96,29 +96,6 @@ def get_object(
9696
must_exist=must_exist,
9797
)
9898

99-
def _parse_req(self, req: urllib.request.Request) -> dict[str, str]:
100-
"""Parse a urlib request into useful components."""
101-
subtype = "mon-config-key"
102-
if req.selector.startswith(subtype + ":"):
103-
return {
104-
"type": req.type,
105-
"subtype": subtype,
106-
"path": req.selector.split(":", 1)[1],
107-
}
108-
sel = req.selector.lstrip("/")
109-
if req.host:
110-
pool = req.host
111-
ns, key = sel.split("/", 1)
112-
else:
113-
pool, ns, key = sel.split("/", 2)
114-
return {
115-
"type": req.type,
116-
"subtype": "object",
117-
"pool": pool,
118-
"ns": ns,
119-
"key": key,
120-
}
121-
12299

123100
# it's quite annoying to have a read-only typing.IO we're forced to
124101
# have so many stub methods. Go's much more granular io interfaces for
@@ -388,6 +365,35 @@ def is_rados_uri(uri: str) -> bool:
388365
return uri.startswith("rados:")
389366

390367

368+
def parse_rados_uri(
369+
uri: typing.Union[str, urllib.request.Request]
370+
) -> dict[str, str]:
371+
"""Given a rados uri-like value return a dict containing a breakdown of the
372+
components of the uri.
373+
"""
374+
req = uri if not isinstance(uri, str) else urllib.request.Request(uri)
375+
subtype = "mon-config-key"
376+
if req.selector.startswith(subtype + ":"):
377+
return {
378+
"type": req.type,
379+
"subtype": subtype,
380+
"path": req.selector.split(":", 1)[1],
381+
}
382+
sel = req.selector.lstrip("/")
383+
if req.host:
384+
pool = req.host
385+
ns, key = sel.split("/", 1)
386+
else:
387+
pool, ns, key = sel.split("/", 2)
388+
return {
389+
"type": req.type,
390+
"subtype": "object",
391+
"pool": pool,
392+
"ns": ns,
393+
"key": key,
394+
}
395+
396+
391397
def enable_rados(
392398
cls: typing.Type[url_opener.URLOpener],
393399
*,

0 commit comments

Comments
 (0)