Skip to content

Commit

Permalink
ENH: remove redundant ssh in cases where we only need to check the fi…
Browse files Browse the repository at this point in the history
…le list
  • Loading branch information
ZLLentz committed Mar 20, 2024
1 parent 3bdf6eb commit 16dfa21
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions pmpsdb_client/plc_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,19 @@
class DataMethod(enum.Enum):
ssh = enum.auto()
ftp = enum.auto()
unk = enum.auto()


def get_data_method(hostname: str, directory: str | None = None) -> DataMethod:
"""
For functions other than list_file_info: pick the data method from the cache,
or by calling list_file_info.
"""
try:
return plc_mapping[hostname]
except KeyError:
...
try:
ssh_data.list_file_info(hostname=hostname, directory=directory)
except Exception:
try:
ftp_data.list_file_info(hostname=hostname, directory=directory)
except Exception:
raise RuntimeError(f"Cannot get data method for {hostname}")
else:
plc_mapping[hostname] = DataMethod.ftp
return DataMethod.ftp
else:
plc_mapping[hostname] = DataMethod.ssh
return DataMethod.ssh
list_file_info(hostname=hostname, directory=directory)
return plc_mapping[hostname]


def list_file_info(
Expand All @@ -64,11 +57,29 @@ def list_file_info(
filenames : list of FileInfo or PLCFile
Information about all the files in the PLC's pmps folder.
"""
data_method = get_data_method(hostname=hostname, directory=directory)
try:
data_method = plc_mapping[hostname]
except KeyError:
data_method = DataMethod.unk

if data_method == DataMethod.ssh:
return ssh_data.list_file_info(hostname=hostname, directory=directory)
elif data_method == DataMethod.ftp:
return ftp_data.list_file_info(hostname=hostname, directory=directory)
elif data_method == DataMethod.unk:
try:
file_info = ssh_data.list_file_info(hostname=hostname, directory=directory)
except Exception:
try:
file_info = ftp_data.list_file_info(hostname=hostname, directory=directory)
except Exception:
raise RuntimeError(f"Cannot get data method for {hostname}")
else:
plc_mapping[hostname] = DataMethod.ftp
return file_info
else:
plc_mapping[hostname] = DataMethod.ssh
return file_info
else:
raise RuntimeError(f"Unhandled data method {data_method}")

Expand Down

0 comments on commit 16dfa21

Please sign in to comment.