-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
64 lines (50 loc) · 1.77 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os, sys, tempfile, shutil
class TempDir(object):
"""
class for temporary directories
creates a (named) directory which is deleted after use.
All files created within the directory are destroyed
Might not work on windows when the files are still opened
"""
def __init__(self, suffix="", prefix="tmp", basedir=None):
self.name = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=basedir)
def __del__(self):
try:
if self.name:
self.dissolve()
except AttributeError:
pass
def __enter__(self):
return self.name
def __exit__(self, *errstuff):
self.dissolve()
def dissolve(self):
"""remove all files and directories created within the tempdir"""
if self.name:
shutil.rmtree(self.name)
self.name = ""
def __str__(self):
if self.name:
return "temporary directory at: %s" % (self.name,)
else:
return "dissolved temporary directory"
class in_tempdir(object):
"""Create a temporary directory and change to it. """
def __init__(self, delete_temp=True, *args, **kwargs):
self.delete_temp = delete_temp
self.tmpdir = TempDir(*args, **kwargs)
def __enter__(self):
self.old_path = os.getcwd()
os.chdir(self.tmpdir.name)
return self.tmpdir.name
def __exit__(self, *errstuff):
os.chdir(self.old_path)
if self.delete_temp:
self.tmpdir.dissolve()
class IterableAdapter:
"""https://stackoverflow.com/a/39564774"""
def __init__(self, iterable_factory, length=None):
self.iterable_factory = iterable_factory
self.length = length
def __iter__(self):
return iter(self.iterable_factory())