Skip to content

Commit

Permalink
Add env var expansion to schemeless ButlerURI
Browse files Browse the repository at this point in the history
  • Loading branch information
timj committed Nov 24, 2020
1 parent 3f99bad commit a9e9e0a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion python/lsst/daf/butler/core/_butlerUri/schemeless.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def _fixupPathUri(parsed: urllib.parse.ParseResult, root: Optional[Union[str, Bu
to be turned into absolute paths before they can be used. This is
always done regardless of the ``forceAbsolute`` parameter.
Scheme-less paths are normalized.
Scheme-less paths are normalized and environment variables are
expanded.
"""
# assume we are not dealing with a directory URI
dirLike = False
Expand All @@ -148,6 +149,10 @@ def _fixupPathUri(parsed: urllib.parse.ParseResult, root: Optional[Union[str, Bu
# we quoted it in the constructor so unquote here
expandedPath = os.path.expanduser(urllib.parse.unquote(parsed.path))

# We might also be receiving a path containing environment variables
# so expand those here
expandedPath = os.path.expandvars(expandedPath)

# Ensure that this becomes a file URI if it is already absolute
if os.path.isabs(expandedPath):
replacements["scheme"] = "file"
Expand Down
13 changes: 13 additions & 0 deletions tests/test_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ def testRelative(self):
child = ButlerURI("../c/e/f/g.txt", forceAbsolute=False)
self.assertEqual(child.relative_to(parent), "e/f/g.txt")

def testEnvVar(self):
"""Test that environment variables are expanded."""

with unittest.mock.patch.dict(os.environ, {"MY_TEST_DIR": "/a/b/c"}):
uri = ButlerURI("${MY_TEST_DIR}/d.txt")
self.assertEqual(uri.path, "/a/b/c/d.txt")
self.assertEqual(uri.scheme, "file")

# This will not expand
uri = ButlerURI("${MY_TEST_DIR}/d.txt", forceAbsolute=False)
self.assertEqual(uri.path, "${MY_TEST_DIR}/d.txt")
self.assertFalse(uri.scheme)

def testMkdir(self):
tmpdir = ButlerURI(self.tmpdir)
newdir = tmpdir.join("newdir/seconddir")
Expand Down

0 comments on commit a9e9e0a

Please sign in to comment.