-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First stab at temp_dir context manager.
- Loading branch information
1 parent
b31f8fb
commit 5a7bdd8
Showing
6 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
contextlib2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from temporary.directory import temp_dir | ||
|
||
# silence PyFlakes | ||
assert temp_dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from contextlib2 import contextmanager | ||
from os import chdir, getcwd | ||
from shutil import rmtree | ||
from tempfile import mkdtemp | ||
|
||
|
||
@contextmanager | ||
def temp_dir(suffix='', prefix='tmp', parent_dir=None, make_cwd=False): | ||
prev_cwd = getcwd() | ||
abs_path = mkdtemp(suffix, prefix, parent_dir) | ||
try: | ||
if make_cwd: | ||
chdir(abs_path) | ||
yield abs_path | ||
finally: | ||
if make_cwd: | ||
chdir(prev_cwd) | ||
rmtree(abs_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from os.path import exists, isdir | ||
from nose.tools import eq_ | ||
from temporary import temp_dir | ||
|
||
|
||
# | ||
# Tests | ||
# | ||
|
||
def test_temp_dir(): | ||
for x in __ensure_usage(): | ||
yield x | ||
|
||
|
||
# | ||
# Test Helpers | ||
# | ||
|
||
def __ensure_usage(**kwargs): | ||
def ensure_context_manager(): | ||
with temp_dir(**kwargs) as d: | ||
eq_(isdir(d), True) | ||
eq_(exists(d), False) | ||
|
||
def ensure_decorator(): | ||
@temp_dir(**kwargs) | ||
def inner(): | ||
pass | ||
|
||
inner() | ||
|
||
yield ensure_context_manager | ||
yield ensure_decorator |