Mercurial > hg > Lemuriformes
annotate lemuriformes/path.py @ 16:9b1bb9eee962
misc functions related to filesystem paths
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 10 Dec 2017 17:19:30 -0800 |
parents | |
children |
rev | line source |
---|---|
16
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
1 """ |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
2 functions related to paths |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
3 """ |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
4 |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
5 import shutil |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
6 import tempfile |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
7 |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
8 |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
9 def ensure_dir(path): |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
10 """ensures `path` is a directory""" |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
11 |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
12 if os.path.exists(path): |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
13 assert os.path.isdir(path) |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
14 else: |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
15 os.makedirs(path) |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
16 return path |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
17 |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
18 |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
19 class TempDir(object): |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
20 """temporary directory context manager""" |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
21 # For inspiration see |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
22 # https://stackoverflow.com/questions/19296146/tempfile-temporarydirectory-context-manager-in-python-2-7 |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
23 |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
24 def __init__(self): |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
25 self.tmpdir = None |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
26 |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
27 def __enter__(self): |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
28 self.tmpdir = tempfile.mkdtemp() |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
29 return self.tmpdir |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
30 |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
31 def __exit__(self, *args): |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
32 if not self.tmpdir: |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
33 return |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
34 shutil.rmtree(self.tmpdir, ignore_errors=True) |
9b1bb9eee962
misc functions related to filesystem paths
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
35 self.tmpdir = None |