Add docstrings for TempFileManager.
This commit is contained in:
parent
15a0a17af0
commit
b11d5496a7
17
test.py
17
test.py
@ -144,13 +144,24 @@ def wait_until_file_exists(file_path, times=None, interval=0.01):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
class TempFileManager(object):
|
class TempFileManager(object):
|
||||||
|
"""A TempFileManager keeps a unique prefix path for temp
|
||||||
|
files. A temp file, or a name for a temp file generate by a
|
||||||
|
TempFileManager always belongs to the same directory.
|
||||||
|
"""
|
||||||
def __init__(self, name=""):
|
def __init__(self, name=""):
|
||||||
|
"""The unique prefix path is UltiSnipsTest_{name}XXXXXX.
|
||||||
|
"""
|
||||||
self._temp_dir = tempfile.mkdtemp(prefix="UltiSnipsTest_" + name)
|
self._temp_dir = tempfile.mkdtemp(prefix="UltiSnipsTest_" + name)
|
||||||
|
|
||||||
def name_temp(self, file_path):
|
def name_temp(self, file_path):
|
||||||
|
"""Get the absolute path of a temp file by given file path.
|
||||||
|
"""
|
||||||
return os.path.join(self._temp_dir, file_path)
|
return os.path.join(self._temp_dir, file_path)
|
||||||
|
|
||||||
def write_temp(self, file_path, content):
|
def write_temp(self, file_path, content):
|
||||||
|
"""Write the content to a temp file by given file path inside
|
||||||
|
the _temp_dir, and return the absolute path of that file.
|
||||||
|
"""
|
||||||
abs_path = self.name_temp(file_path)
|
abs_path = self.name_temp(file_path)
|
||||||
create_directory(os.path.dirname(abs_path))
|
create_directory(os.path.dirname(abs_path))
|
||||||
if PYTHON3:
|
if PYTHON3:
|
||||||
@ -162,12 +173,18 @@ class TempFileManager(object):
|
|||||||
return abs_path
|
return abs_path
|
||||||
|
|
||||||
def unique_name_temp(self, suffix="", prefix=""):
|
def unique_name_temp(self, suffix="", prefix=""):
|
||||||
|
"""Generate a unique name for a temp file with given suffix and
|
||||||
|
prefix, and return full absolute path.
|
||||||
|
"""
|
||||||
file_handler, abspath = tempfile.mkstemp(suffix, prefix, self._temp_dir)
|
file_handler, abspath = tempfile.mkstemp(suffix, prefix, self._temp_dir)
|
||||||
os.close(file_handler)
|
os.close(file_handler)
|
||||||
os.remove(abspath)
|
os.remove(abspath)
|
||||||
return abspath
|
return abspath
|
||||||
|
|
||||||
def clear_temp(self):
|
def clear_temp(self):
|
||||||
|
"""Clear the temp file directory, but the directory itself is
|
||||||
|
not removed.
|
||||||
|
"""
|
||||||
shutil.rmtree(self._temp_dir)
|
shutil.rmtree(self._temp_dir)
|
||||||
create_directory(self._temp_dir)
|
create_directory(self._temp_dir)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user