Commit 62f9addc authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

RF: Roll our own expandvars, because op.expandvars doesn't replace unset

variables with the empty string
parent ed96be43
......@@ -10,6 +10,8 @@ program.
import os.path as op
import os
import re
import math
import zlib
import shutil
......@@ -26,11 +28,30 @@ except ImportError: progressbar = None
import fsl.add_module.admin as admin
def expand(path : Union[str, pathlib.Path]):
"""Applies ``os.path.abspath``, ``os.path.expandvars`` and
def expandvars(path: Union[str, pathlib.Path]) -> str:
"""Alternative to ``os.path.expandvars`` which replaces unset variables
with an empty string.
path = str(path)
patterns = [
for pat in patterns:
matches = re.findall(pat, path)
for fullmatch, name in matches:
path = path.replace(fullmatch, os.environ.get(name, ''))
return path
def expand(path : Union[str, pathlib.Path]) -> str:
"""Applies ``os.path.abspath``, :func:`expandvars` and
``os.path.expanduser`` to ``path``.
return op.abspath(op.expandvars(op.expanduser(path)))
return op.abspath(expandvars(op.expanduser(path)))
class DownloadFailed(Exception):
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment