Skip to content

Commit

Permalink
add normalize_filename()
Browse files Browse the repository at this point in the history
  • Loading branch information
artgoldberg committed Mar 6, 2019
1 parent a4d6d7d commit 479318d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/util/test_files.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Tests of the file utilities
:Author: Jonathan Karr <jonrkarr@gmail.com>
:Author: Arthur Goldberg <Arthur.Goldberg@mssm.edu>
:Date: 2018-05-11
:Copyright: 2018, Karr Lab
:License: MIT
Expand All @@ -11,9 +12,11 @@
import shutil
import tempfile
import unittest
import getpass


class FileUtilsTestCase(unittest.TestCase):

def setUp(self):
self.dirname = tempfile.mkdtemp()

Expand Down Expand Up @@ -60,3 +63,18 @@ def test_copytree_to_existing_destination(self):
files.copytree_to_existing_destination(os.path.join(base, 'a', 'b', 'c'), os.path.join(base, 'D'))
with open(os.path.join(base, 'D'), 'r') as file:
self.assertEqual(file.read(), '3')


def test_normalize_filename(self):
normalize_filename = files.normalize_filename

self.assertEqual(normalize_filename('~'), normalize_filename('~' + getpass.getuser()))
self.assertEqual(normalize_filename('~'), normalize_filename('$HOME'))
cur_dir = os.path.dirname(__file__)
self.assertEqual(cur_dir,
normalize_filename(os.path.join(cur_dir, '..', os.path.basename(cur_dir))))
test_filename = os.path.join(cur_dir, 'test_filename')
self.assertEqual(test_filename, normalize_filename('test_filename', dir=os.path.dirname(test_filename)))
self.assertEqual(os.path.join(os.getcwd(), 'test_filename'), normalize_filename('test_filename'))
with self.assertRaisesRegex(ValueError, r"directory '.+' isn't absolute"):
normalize_filename('test_filename', dir='~')
31 changes: 31 additions & 0 deletions wc_utils/util/files.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" File utils
:Author: Jonathan Karr <karr@mssm.edu>
:Author: Arthur Goldberg <Arthur.Goldberg@mssm.edu>
:Date: 2018-05-11
:Copyright: 2018, Karr Lab
:License: MIT
Expand Down Expand Up @@ -35,3 +36,33 @@ def copytree_to_existing_destination(src, dst):
shutil.copy2(s, d)
else:
shutil.copy2(src, dst)


def normalize_filename(filename, dir=None):
""" Normalize a filename to its fully expanded, real, absolute path
Expand `filename` by interpreting a user’s home directory, environment variables, and
normalizing its path. If `filename` is not an absolute path and `dir` is provided then
return a full path of `filename` in `dir`.
Args:
filename (:obj:`str`): a filename
dir (:obj:`str`, optional): a directory that contains `filename`
Returns:
:obj:`str`: `filename`'s fully expanded, absolute path
Raises:
:obj:`ValueError`: if neither `filename` after expansion nor `dir` are absolute
"""
filename = os.path.expanduser(filename)
filename = os.path.expandvars(filename)
if os.path.isabs(filename):
return os.path.normpath(filename)
elif dir:
# raise exception if dir isn't absolute
if not os.path.isabs(dir):
raise ValueError("directory '{}' isn't absolute".format(dir))
return os.path.normpath(os.path.join(dir, filename))
else:
return os.path.abspath(filename)

0 comments on commit 479318d

Please sign in to comment.