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

Refactor channel search functionality in tdm_loader #28

Merged
merged 10 commits into from
Mar 28, 2024
46 changes: 21 additions & 25 deletions tdm_loader/tdm_loader.py
Original file line number Diff line number Diff line change
@@ -228,31 +228,27 @@ def channel_search(self, search_term):
"""
search_term = str(search_term).upper().replace(" ", "")

ind_chg_ch = []
for j in range(len(self._xml_chgs)):
chs = self._channels_xml(j)

if search_term == "":
found_terms = [
ch.findtext("name") for ch in chs if ch.findtext("name") is None
]
else:
found_terms = [
ch.findtext("name")
for ch in chs
if ch.findtext("name") is not None
and ch.findtext("name")
.upper()
.replace(" ", "")
.find(str(search_term))
>= 0
]

for name in found_terms:
i = [ch.findtext("name") for ch in chs].index(name)
ind_chg_ch.append((name, j, i))

return ind_chg_ch
matched_channels = []
channel_groups_cache = {}
EspenEnes marked this conversation as resolved.
Show resolved Hide resolved
channel_group_ids = {v: i for i, v in enumerate(x.get("id") for x in self._xml_chgs)}

for channel in self._root.findall(".//tdm_channel"):
if channel_name := channel.find("name").text:
EspenEnes marked this conversation as resolved.
Show resolved Hide resolved
channel_id = channel.get("id")
EspenEnes marked this conversation as resolved.
Show resolved Hide resolved
group_uri = re.findall(r'id\("(.+?)"\)', channel.find("group").text)
group_id = channel_group_ids.get(group_uri[0])
channels = channel_groups_cache.get(group_id)
EspenEnes marked this conversation as resolved.
Show resolved Hide resolved

if not channels:
group = self._xml_chgs[group_id]
channels = {v: i for i, v in enumerate(re.findall(r'id\("(.+?)"\)', group.find("channels").text))}
channel_groups_cache[group_id] = channels
EspenEnes marked this conversation as resolved.
Show resolved Hide resolved
channel_id = channels.get(channel_id)
EspenEnes marked this conversation as resolved.
Show resolved Hide resolved

if channel_name.upper().replace(" ", "").find(search_term) >= 0:
matched_channels.append((channel_name, group_id, channel_id))

return matched_channels

def channel(self, channel_group, channel, occurrence=0, ch_occurrence=0):
"""Returns a data channel by its channel group and channel index.
4 changes: 3 additions & 1 deletion tdm_loader/tests/test_non_zip_tdm.py
Original file line number Diff line number Diff line change
@@ -204,7 +204,9 @@ def test_channel_search(tdm_file):
("Float as Float", 0, 1),
]

assert tdm_file.channel_search("") == []
assert tdm_file.channel_search("") == [('Float_4_Integers', 0, 0),
('Float as Float', 0, 1),
('Integer32_with_max_min', 0, 2)]


# pylint: disable=redefined-outer-name
Loading