-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c4e0e1
commit 7abbcfb
Showing
4 changed files
with
218 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from dataclasses import dataclass | ||
from typing import Dict, List | ||
import urllib | ||
|
||
|
||
@dataclass | ||
class ParsedDIDInfo: | ||
# The did to pass into the library | ||
did: str | ||
|
||
# Mode to get the files (default 'all') | ||
get_mode: str | ||
|
||
# Number of files to fetch (default '-1') | ||
file_count: int | ||
|
||
|
||
def parse_did_uri(uri: str) -> ParsedDIDInfo: | ||
'''Parse the uri that is given to us from ServiceX, pulling out | ||
the components we care about, and keeping the DID that needs to | ||
be passed down. | ||
Args: | ||
uri (str): DID from ServiceX | ||
Returns: | ||
ParsedDIDInfo: The URI parsed into parts | ||
''' | ||
info = urllib.parse.urlparse(uri) # type: ignore | ||
|
||
params = urllib.parse.parse_qs(info.query) # type: ignore | ||
get_string = 'all' if 'get' not in params else params['get'][-1] | ||
file_count = -1 if 'files' not in params else int(params['files'][0]) | ||
|
||
if get_string not in ['all', 'available']: | ||
raise ValueError('Bad value for "get" string in DID - must be "all" or "available", not ' | ||
f'"{get_string}"') | ||
|
||
for k in ['get', 'files']: | ||
if k in params: | ||
del params[k] | ||
|
||
def unwind_params(ps: Dict[str, List[str]]): | ||
for k, values in ps.items(): | ||
for v in values: | ||
yield k, v | ||
|
||
new_query = "&".join(f'{k}={v}' for k, v in unwind_params(params)) | ||
if len(new_query) > 0: | ||
new_query = "?" + new_query | ||
|
||
return ParsedDIDInfo(info.path + new_query, get_string, file_count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import pytest | ||
from servicex_did_finder_lib.util_uri import parse_did_uri | ||
|
||
|
||
def test_plain_uri(): | ||
r = parse_did_uri('forkit') | ||
|
||
assert r.did == "forkit" | ||
assert r.get_mode == "all" | ||
assert r.file_count == -1 | ||
|
||
|
||
def test_uri_with_mode_avail(): | ||
r = parse_did_uri('forkit?get=available') | ||
|
||
assert r.did == "forkit" | ||
assert r.get_mode == "available" | ||
assert r.file_count == -1 | ||
|
||
|
||
def test_uri_with_mode_all(): | ||
r = parse_did_uri('forkit?get=all') | ||
|
||
assert r.did == "forkit" | ||
assert r.get_mode == "all" | ||
assert r.file_count == -1 | ||
|
||
|
||
def test_uri_with_mode_bad(): | ||
with pytest.raises(ValueError) as e: | ||
parse_did_uri('forkit?get=all_available') | ||
|
||
assert "all_available" in str(e.value) | ||
|
||
|
||
def test_uri_with_file_count(): | ||
r = parse_did_uri('forkit?files=10') | ||
|
||
assert r.did == "forkit" | ||
assert r.get_mode == "all" | ||
assert r.file_count == 10 | ||
|
||
|
||
def test_uri_with_file_count_neg(): | ||
r = parse_did_uri('forkit?files=-1') | ||
|
||
assert r.did == "forkit" | ||
assert r.get_mode == "all" | ||
assert r.file_count == -1 | ||
|
||
|
||
def test_uri_with_file_and_get(): | ||
r = parse_did_uri('forkit?files=10&get=available') | ||
|
||
assert r.did == "forkit" | ||
assert r.get_mode == "available" | ||
assert r.file_count == 10 | ||
|
||
|
||
def test_uri_with_other_params(): | ||
r = parse_did_uri('forkit?get=available&stuff=hi&files=10') | ||
|
||
assert r.did == "forkit?stuff=hi" | ||
assert r.get_mode == "available" | ||
assert r.file_count == 10 |