Skip to content
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

feat: optionally exclude descriptions from tag search results #118

Merged
merged 2 commits into from
Feb 29, 2024
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
9 changes: 6 additions & 3 deletions tagreader/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def search_tag(
tag: Optional[str] = None,
desc: Optional[str] = None,
timeout: Optional[int] = None,
) -> List[Tuple[str, str]]:
) -> Union[List[Tuple[str, str]], List[str]]:
logger.warning("This function is deprecated. Please call 'search()' instead")
return self.search(tag=tag, desc=desc, timeout=timeout)

Expand All @@ -309,8 +309,11 @@ def search(
tag: Optional[str] = None,
desc: Optional[str] = None,
timeout: Optional[int] = None,
) -> List[Tuple[str, str]]:
return self.handler.search(tag=tag, desc=desc, timeout=timeout)
return_desc: bool = True,
) -> Union[List[Tuple[str, str]], List[str]]:
return self.handler.search(
tag=tag, desc=desc, timeout=timeout, return_desc=return_desc
)

def _get_metadata(self, tag: str):
return self.handler._get_tag_metadata(
Expand Down
49 changes: 37 additions & 12 deletions tagreader/web_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ def connect(self):
) from None

@abstractmethod
def verify_connection(self, datasource: str):
...
def verify_connection(self, datasource: str): ...


class AspenHandlerWeb(BaseHandlerWeb):
Expand Down Expand Up @@ -379,8 +378,24 @@ def _get_default_mapname(self, tagname: str):
return k

def search(
self, tag: Optional[str], desc: Optional[str], timeout: Optional[int] = None
) -> List[Tuple[str, str]]:
self,
tag: Optional[str],
desc: Optional[str],
timeout: Optional[int] = None,
return_desc: bool = True,
) -> Union[List[Tuple[str, str]], List[str]]:
ret = []

if tag is None and desc is None:
raise ValueError("Tag is a required argument")
# return ret
elif tag is None and len(desc) == 0:
raise ValueError("Tag is a required argument")
# return ret
elif desc is None and len(tag) == 0:
raise ValueError("Tag is a required argument")
# return ret

if tag is None:
raise ValueError("Tag is a required argument")

Expand All @@ -403,19 +418,24 @@ def search(
data = self.fetch(url, timeout=timeout)

if "tags" not in data["data"]:
return []
return ret

ret = []
for item in data["data"]["tags"]:
tagname = item["t"]
description = self._get_tag_description(tagname)
ret.append((tagname, description))
if not desc and not return_desc:
ret.append(tagname)
else:
description = self._get_tag_description(tagname)
ret.append((tagname, description))

if not desc:
return ret
pass
else:
r = re.compile(desc)
ret = [x for x in ret if r.search(x[1])]
if not return_desc:
ret = [x[0] for x in ret]

r = re.compile(desc)
ret = [x for x in ret if r.search(x[1])]
return ret

def _get_tag_metadata(self, tag: str):
Expand Down Expand Up @@ -791,7 +811,8 @@ def search(
tag: Optional[str] = None,
desc: Optional[str] = None,
timeout: Optional[int] = None,
) -> List[Tuple]:
return_desc: bool = True,
) -> Union[List[Tuple[str, str]], List[str]]:
params = self.generate_search_query(
tag=tag, desc=desc, datasource=self.datasource
)
Expand All @@ -809,6 +830,10 @@ def search(
params["start"] = next_start # noqa
else:
done = True

if not return_desc:
ret = [x[0] for x in ret]

return ret

def _get_tag_metadata(self, tag: str) -> Dict[str, str]:
Expand Down
33 changes: 31 additions & 2 deletions tests/test_AspenHandlerREST_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

SOURCE = "SNA"
TAG = "ATCAI"
START_TIME = datetime(2018, 5, 1, 10, 0, 0)
STOP_TIME = datetime(2018, 5, 1, 11, 0, 0)
START_TIME = datetime(2023, 5, 1, 10, 0, 0)
STOP_TIME = datetime(2023, 5, 1, 11, 0, 0)
SAMPLE_TIME = timedelta(seconds=60)


Expand Down Expand Up @@ -79,22 +79,51 @@ def test_verify_connection(aspen_handler: AspenHandlerWeb) -> None:
def test_search_tag(client: IMSClient) -> None:
res = client.search(tag="so_specific_it_cannot_possibly_exist", desc=None)
assert 0 == len(res)

res = client.search(tag="ATCAI", desc=None)
assert res == [("ATCAI", "Sine Input")]

res = client.search(tag="ATCM*", desc=None)
assert 5 <= len(res)

[taglist, desclist] = zip(*res)
assert "ATCMIXTIME1" in taglist
assert desclist[taglist.index("ATCMIXTIME1")] == "MIX TANK 1 TIMER"

res = client.search(tag="ATCM*", desc=None)
assert 5 <= len(res)
assert isinstance(res, list)
assert isinstance(res[0], tuple)

res = client.search("AspenCalcTrigger1", desc=None)
assert res == [("AspenCalcTrigger1", "")]

res = client.search("ATC*", "Sine*")
assert res == [("ATCAI", "Sine Input")]
with pytest.raises(ValueError):
_ = client.search(desc="Sine Input") # noqa

res = client.search(tag="ATCM*", return_desc=False)
assert 5 <= len(res)
assert isinstance(res, list)
assert isinstance(res[0], str)

res = client.search("AspenCalcTrigger1")
assert res == [("AspenCalcTrigger1", "")]
res = client.search("AspenCalcTrigger1", desc=None)
assert res == [("AspenCalcTrigger1", "")]

res = client.search("ATC*", "Sine*")
assert res == [("ATCAI", "Sine Input")]

with pytest.raises(ValueError):
res = client.search("")

with pytest.raises(ValueError):
_ = client.search(
desc="Sine Input"
) # noqa res = client.search(tag="ATCM*", return_desc=False)´


def test_read_unknown_tag(client: IMSClient) -> None:
df = client.read(
Expand Down
8 changes: 7 additions & 1 deletion tests/test_PIHandlerREST_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,17 @@ def test_search_tag(client: IMSClient) -> None:
res = client.search("SINUSOID")
assert 1 == len(res)
res = client.search("SIN*")
assert isinstance(res, list)
assert 3 <= len(res)
assert isinstance(res[0], tuple)
[taglist, desclist] = zip(*res)
assert "SINUSOIDU" in taglist
assert desclist[taglist.index("SINUSOID")] == "12 Hour Sine Wave"
res = client.search(tag=None, desc="12 Hour Sine Wave")
res = client.search("SIN*", return_desc=False)
assert 3 <= len(res)
assert isinstance(res, list)
assert isinstance(res[0], str)
res = client.search(desc="12 Hour Sine Wave")
assert 1 <= len(res)
res = client.search(tag="SINUSOID", desc="*Sine*")
assert 1 <= len(res)
Expand Down