From 21fd871e05ac4a6d3a423ea965945f956b24d68a Mon Sep 17 00:00:00 2001 From: Jon Hagg Date: Mon, 12 Jul 2021 17:01:10 -0700 Subject: [PATCH] refactor: define MemoryDataAccess for reusability --- powersimdata/data_access/data_access.py | 33 +++++++++++-------- .../data_access/tests/test_data_access.py | 14 ++------ 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/powersimdata/data_access/data_access.py b/powersimdata/data_access/data_access.py index 89df25f96..9ed094d8c 100644 --- a/powersimdata/data_access/data_access.py +++ b/powersimdata/data_access/data_access.py @@ -47,7 +47,7 @@ def move_to(self, file_name, to_dir, change_name_to=None): raise NotImplementedError def tmp_folder(self, scenario_id): - """Get path matching the given kind of scenario data + """Get path to temporary scenario folder :param int/str scenario_id: the scenario id :return: (*str*) -- the specified path @@ -110,9 +110,9 @@ def checksum(self, relative_path): """Return the checksum of the file path :param str relative_path: path relative to root - :return: (*int/str*) -- the checksum of the file + :return: (*str*) -- the checksum of the file """ - raise NotImplementedError + return "dummy_value" def push(self, file_name, checksum, change_name_to=None): """Push the file from local to remote root folder, ensuring integrity @@ -173,14 +173,6 @@ def move_to(self, file_name, to_dir, change_name_to=None): self.makedir(to_dir) self.fs.copy(file_name, dest) - def checksum(self, relative_path): - """Return the checksum of the file path - - :param str relative_path: path relative to root - :return: (*int/str*) -- the checksum of the file - """ - return "dummy_value" - def get_profile_version(self, grid_model, kind): """Returns available raw profile from blob storage or local disk @@ -214,9 +206,10 @@ def fs(self): :raises IOError: if connection failed or still within retry window :return: (*powersimdata.data_access.ssh_fs.WrapSSHFS) -- filesystem instance """ - should_attempt = time.time() - SSHDataAccess._last_attempt > self._retry_after - if self._fs is None: + should_attempt = ( + time.time() - SSHDataAccess._last_attempt > self._retry_after + ) if should_attempt: try: self._fs = get_ssh_fs(self.root) @@ -284,7 +277,7 @@ def checksum(self, relative_path): """Return the checksum of the file path :param str relative_path: path relative to root - :return: (*int/str*) -- the checksum of the file + :return: (*str*) -- the checksum of the file """ full_path = self.join(self.root, relative_path) self._check_file_exists(relative_path) @@ -325,3 +318,15 @@ def push(self, file_name, checksum, change_name_to=None): for e in errors: print(e) raise IOError("Failed to push file - most likely a conflict was detected.") + + +class MemoryDataAccess(SSHDataAccess): + def __init__(self): + self.local_fs = fs2.open_fs("mem://") + self._fs = fs2.open_fs("mem://") + self.description = "in-memory" + self.local_root = self.root = "dummy" + self.join = fs2.path.join + + def push(self, file_name, checksum, change_name_to=None): + self.move_to(file_name, change_name_to=change_name_to) diff --git a/powersimdata/data_access/tests/test_data_access.py b/powersimdata/data_access/tests/test_data_access.py index d9649eb3f..ad46b3466 100644 --- a/powersimdata/data_access/tests/test_data_access.py +++ b/powersimdata/data_access/tests/test_data_access.py @@ -1,7 +1,7 @@ import fs as fs2 import pytest -from powersimdata.data_access.data_access import SSHDataAccess +from powersimdata.data_access.data_access import MemoryDataAccess, SSHDataAccess from powersimdata.utility import server_setup FILE_NAME = "test.txt" @@ -13,19 +13,9 @@ def ssh_data_access(): return SSHDataAccess() -def mem_fs(): - return fs2.open_fs("mem://") - - @pytest.fixture def data_access(): - with mem_fs() as fs1, mem_fs() as fs2: - data_access = SSHDataAccess() - data_access._fs = fs1 - data_access.local_fs = fs2 - data_access.root = "/" - data_access.local_root = "/" - yield data_access + return MemoryDataAccess() def make_temp(fs, path):