-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcodec_helpers.py
More file actions
43 lines (38 loc) · 1.24 KB
/
codec_helpers.py
File metadata and controls
43 lines (38 loc) · 1.24 KB
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
from contextlib import contextmanager, redirect_stderr, redirect_stdout
from os import devnull
import tempfile
import shutil
import logging
@contextmanager
def suppress_stdout_stderr():
"""A context manager that redirects stdout and stderr to devnull"""
logging.info("STDOUT and STDERR are send to devnull.")
with open(devnull, "w") as fnull:
with redirect_stderr(fnull) as err, redirect_stdout(fnull) as out:
yield (err, out)
class temp_dir():
def __init__(self):
"""
A function to create temp codec files inside temporary directories, and
enable multiple codec processing, preventing file writing racing conditions.
Returns
-------
str, Temporary directory path.
"""
self.path = tempfile.mkdtemp()
logging.debug(f"temp_dir: {self.path}")
def dlt(self):
"""
Safe temp_dir deletion and path invalidation.
Returns
-------
None.
"""
if self.path:
shutil.rmtree(self.path)
self.path = None
logging.debug("temp_dir.path: deleted")
return True
else:
logging.error("temp_dir.path: does not exist.")
return False