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: simplify _check_file_exists and get_profile_version #562

Merged
merged 1 commit into from
Oct 20, 2021
Merged
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
26 changes: 8 additions & 18 deletions powersimdata/data_access/data_access.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import operator
import posixpath
import time
from subprocess import Popen
Expand Down Expand Up @@ -84,11 +83,11 @@ def _check_file_exists(self, path, should_exist=True):
:param bool should_exist: whether the file is expected to exist
:raises OSError: if the expected condition is not met
"""
result = self.fs.exists(path)
compare = operator.ne if should_exist else operator.eq
if compare(result, True):
msg = "not found" if should_exist else "already exists"
raise OSError(f"{path} {msg} on {self.description}")
exists = self.fs.exists(path)
if should_exist and not exists:
raise OSError(f"{path} not found on {self.description}")
if not should_exist and exists:
raise OSError(f"{path} already exists on {self.description}")

def makedir(self, path):
"""Create path in current environment
Expand Down Expand Up @@ -128,7 +127,9 @@ def get_profile_version(self, grid_model, kind):
:param str kind: *'demand'*, *'hydro'*, *'solar'* or *'wind'*.
:return: (*list*) -- available profile version.
"""
return ProfileHelper.get_profile_version_cloud(grid_model, kind)
blob_version = ProfileHelper.get_profile_version_cloud(grid_model, kind)
local_version = ProfileHelper.get_profile_version_local(grid_model, kind)
return list(set(blob_version + local_version))


class LocalDataAccess(DataAccess):
Expand Down Expand Up @@ -170,17 +171,6 @@ def move_to(self, file_name, to_dir, change_name_to=None):
self.makedir(to_dir)
self.fs.move(file_name, dest)

def get_profile_version(self, grid_model, kind):
"""Returns available raw profile from blob storage or local disk

:param str grid_model: grid model.
:param str kind: *'demand'*, *'hydro'*, *'solar'* or *'wind'*.
:return: (*list*) -- available profile version.
"""
blob_version = super().get_profile_version(grid_model, kind)
local_version = ProfileHelper.get_profile_version_local(grid_model, kind)
return list(set(blob_version + local_version))


class SSHDataAccess(DataAccess):
"""Interface to a remote data store, accessed via SSH."""
Expand Down