Skip to content

Commit

Permalink
refactor: define MemoryDataAccess for reusability
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Hagg committed Jul 13, 2021
1 parent a949aea commit 21fd871
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
33 changes: 19 additions & 14 deletions powersimdata/data_access/data_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
14 changes: 2 additions & 12 deletions powersimdata/data_access/tests/test_data_access.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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):
Expand Down

0 comments on commit 21fd871

Please sign in to comment.