Skip to content

Commit

Permalink
Sort root_dir checks for when using Sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmcalpine committed Nov 21, 2023
1 parent 5348cf8 commit dcc9f63
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
38 changes: 25 additions & 13 deletions src/dataregistry/DataRegistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,28 @@ def _get_root_dir(self, root_dir, site):
with open(_SITE_CONFIG_PATH) as f:
data = yaml.safe_load(f)

if root_dir is not None:
return root_dir
elif (site is not None) and (self.db_connection._dialect != "sqlite"):
assert site.lower() in data.keys(), "Bad site selected"
return data[site.lower()]
elif os.getenv("DATAREG_SITE"):
return os.getenv("DATAREG_SITE")
else:
if self.db_connection._dialect == "sqlite":
raise ValueError(
"Must pass a root_dir, or set $DATAREG_SITE for Sqlite"
)
return data["nersc"]
# Find out what the root_dir should be
if root_dir is None:
if site is not None:
if site.lower() not in data.keys():
raise ValueError(f"{site} is not a valid site")
root_dir = data[site.lower()]
elif os.getenv("DATAREG_SITE"):
root_dir = os.getenv("DATAREG_SITE")
else:
if self.db_connection._dialect == "sqlite":
raise ValueError(
"For Sqlite, must pass root_dir or set $DATAREG_SITE"
)
else:
root_dir = data["nersc"]

if self.db_connection._dialect == "sqlite":
# root_dir cannot equal a site path when using Sqlite
for a, v in data.items():
if root_dir == v:
raise ValueError(
"Cannot have `root_dir` equal to a pre-defined site when using Sqlite"
)

return root_dir
2 changes: 1 addition & 1 deletion tests/unit_tests/test_root_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
(None, "nersc", _NERSC_SITE),
(None, None, _ENV_SITE),
(None, None, _NERSC_SITE),
(_TEST_ROOT_DIR, _NERSC_SITE, _TEST_ROOT_DIR),
(_TEST_ROOT_DIR, "nersc", _TEST_ROOT_DIR),
],
)
def test_root_dir_manual(root_dir, site, ans):
Expand Down

0 comments on commit dcc9f63

Please sign in to comment.