Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, filename, mode='w+b', bufsize=-1, **kwds):
self._tmpdir = TemporaryDirectory(**kwds)
path = _os.path.join(self._tmpdir.name, filename)
self.file = open(path, mode, bufsize)
self.file = open(path, mode, bufsize)
def cleanup(self):
self.file.close()
self._tmpdir.cleanup()
__del__ = cleanup
def __enter__(self):
return self.file
def __exit__(self, type, value, traceback):
self.cleanup()
class TemporaryWorkingDirectory(TemporaryDirectory):
"""
Creates a temporary directory and sets the cwd to that directory.
Automatically reverts to previous cwd upon cleanup.
Usage example::
with TemporaryWorkingDirectory() as tmpdir:
...
"""
def __enter__(self):
self.old_wd = _os.getcwd()
_os.chdir(self.name)
return super(TemporaryWorkingDirectory, self).__enter__()
def __exit__(self, exc, value, tb):
_os.chdir(self.old_wd)
def setUp(self):
self.td = TemporaryDirectory()
self.addCleanup(self.td.cleanup)
self.file_path = os.path.join(self.td.name, 'afile')
with open(self.file_path, 'w') as f:
f.write('Blah')
self.dir_path = os.path.join(self.td.name, 'adir')
os.mkdir(self.dir_path)
self.link_path = os.path.join(self.td.name, 'alink')
self.pipe_path = os.path.join(self.td.name, 'apipe')
self.socket_path = os.path.join(self.td.name, 'asocket')
if os.name == 'posix':
# Symlinks are rarely usable on Windows, because a special
# permission is needed to create them.
os.symlink(self.file_path, self.link_path)
def convert_figure(self, data_format, data):
"""
Convert a single SVG figure to PDF. Returns converted data.
"""
#Work in a temporary directory
with TemporaryDirectory() as tmpdir:
#Write fig to temp file
input_filename = os.path.join(tmpdir, 'figure.svg')
# SVG data is unicode text
with io.open(input_filename, 'w', encoding='utf8') as f:
f.write(cast_unicode_py2(data))
#Call conversion application
output_filename = os.path.join(tmpdir, 'figure.pdf')
shell = self.command.format(from_filename=input_filename,
to_filename=output_filename)
subprocess.call(shell, shell=True) #Shell=True okay since input is trusted.
#Read output from drive
# return value expects a filename
if os.path.isfile(output_filename):