本文整理汇总了Python中tempfile.SpooledTemporaryFile.close方法的典型用法代码示例。如果您正苦于以下问题:Python SpooledTemporaryFile.close方法的具体用法?Python SpooledTemporaryFile.close怎么用?Python SpooledTemporaryFile.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tempfile.SpooledTemporaryFile
的用法示例。
在下文中一共展示了SpooledTemporaryFile.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _do_execute_direct
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
def _do_execute_direct(self, code):
shell = builtins.__xonsh_shell__
env = builtins.__xonsh_env__
out = io.StringIO()
err = io.StringIO()
enc = env.get('XONSH_ENCODING')
out = SpooledTemporaryFile(max_size=MAX_SIZE, mode='w+t',
encoding=enc, newline='\n')
err = SpooledTemporaryFile(max_size=MAX_SIZE, mode='w+t',
encoding=enc, newline='\n')
try:
with redirect_stdout(out), redirect_stderr(err), \
swap(builtins, '__xonsh_stdout_uncaptured__', out), \
swap(builtins, '__xonsh_stderr_uncaptured__', err), \
env.swap({'XONSH_STORE_STDOUT': False}):
shell.default(code)
interrupted = False
except KeyboardInterrupt:
interrupted = True
output, error = '', ''
if out.tell() > 0:
out.seek(0)
output = out.read()
if err.tell() > 0:
err.seek(0)
error = err.read()
out.close()
err.close()
return output, error, interrupted
示例2: archive_to_repo
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
def archive_to_repo(archive_path, repo, archive_type="tar"):
"""Downloads a archive from the specified path,
extracts it into the repo's directory, commits
any changes from the previous version and pushes it
to github!!!!"""
# Download the tarball and stick it in a tempfile
r = requests.get(archive_path)
tmp = SpooledTemporaryFile()
tmp.write(r.content)
tmp.seek(0)
# Open the tempfile contents as an actual tarball
if archive_type == "tar":
archive = tarfile.open(fileobj=tmp)
elif archive_type == "zip":
archive = zipfile.open(tmp)
else:
raise ValueError("Unrecognized Archive Type")
# Clear working files
clear_working_dir(repo.working_dir)
# Extract to the repo path
archive.extract(repo.working_dir)
# Add and commit everything!
try:
repo.git.add(".", A=True)
repo.git.commit(m="New archive version")
except:
pass # May be that there was nothing new to commit
# Cleanup, outta here!
archive.close()
tmp.close()
示例3: close
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
def close(self):
"""Send the change to the DFS, and close the file."""
self.flush()
if 'c' not in self.mode:
SpooledTemporaryFile.close(self)
示例4: generate_thumbnail
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
def generate_thumbnail(self, content):
content = file_from_content(content)
uploaded_image = Image.open(content)
if max(uploaded_image.size) >= self.max_size:
uploaded_image.thumbnail((self.max_size, self.max_size), Image.BILINEAR)
content = SpooledTemporaryFile(INMEMORY_FILESIZE)
uploaded_image.save(content, uploaded_image.format)
content.seek(0)
thumbnail = uploaded_image.copy()
thumbnail.thumbnail(self.thumbnail_size, Image.ANTIALIAS)
thumbnail = thumbnail.convert('RGBA')
thumbnail.format = self.thumbnail_format
output = SpooledTemporaryFile(INMEMORY_FILESIZE)
thumbnail.save(output, self.thumbnail_format)
output.seek(0)
thumb_path, thumb_id = self.store_content(output,
'thumb.%s' % self.thumbnail_format.lower())
self['thumb_id'] = thumb_id
self['thumb_path'] = thumb_path
thumbnail_file = self.thumb_file
self['_thumb_public_url'] = thumbnail_file.public_url
content.close()
示例5: GoogleCloudFile
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
class GoogleCloudFile(File):
def __init__(self, name, mode, storage):
self.name = name
self.mime_type = mimetypes.guess_type(name)[0]
self._mode = mode
self._storage = storage
# NOTE(mattrobenolt): This is the same change in behavior as in
# the s3 backend. We're opting now to load the file
# or metadata at this step. This means we won't actually
# know a file doesn't exist until we try to read it.
self.blob = FancyBlob(storage.download_url, self.name, storage.bucket)
self._file = None
self._is_dirty = False
@property
def size(self):
return self.blob.size
def _get_file(self):
if self._file is None:
with metrics.timer('filestore.read', instance='gcs'):
self._file = SpooledTemporaryFile(
max_size=self._storage.max_memory_size,
suffix=".GSStorageFile",
dir=None,
)
if 'r' in self._mode:
self._is_dirty = False
self.blob.download_to_file(self._file)
self._file.seek(0)
return self._file
def _set_file(self, value):
self._file = value
file = property(_get_file, _set_file)
def read(self, num_bytes=None):
if 'r' not in self._mode:
raise AttributeError("File was not opened in read mode.")
if num_bytes is None:
num_bytes = -1
return super(GoogleCloudFile, self).read(num_bytes)
def write(self, content):
if 'w' not in self._mode:
raise AttributeError("File was not opened in write mode.")
self._is_dirty = True
return super(GoogleCloudFile, self).write(force_bytes(content))
def close(self):
if self._file is not None:
if self._is_dirty:
self.file.seek(0)
self.blob.upload_from_file(self.file, content_type=self.mime_type)
self._file.close()
self._file = None
示例6: _shell_command
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
def _shell_command(self, cmd):
"""Shell out a subprocess and return what it writes to stdout as a string"""
in_mem_file = SpooledTemporaryFile(max_size=2048, mode="r+")
check_call(cmd, shell=True, stdout=in_mem_file)
in_mem_file.seek(0)
stdout = in_mem_file.read()
in_mem_file.close()
return stdout
示例7: GoogleCloudFile
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
class GoogleCloudFile(File):
def __init__(self, name, mode, storage):
self.name = name
self.mime_type = mimetypes.guess_type(name)[0]
self._mode = mode
self._storage = storage
self.blob = storage.bucket.get_blob(name)
if not self.blob and 'w' in mode:
self.blob = Blob(self.name, storage.bucket)
self._file = None
self._is_dirty = False
@property
def size(self):
return self.blob.size
def _get_file(self):
if self._file is None:
self._file = SpooledTemporaryFile(
max_size=self._storage.max_memory_size,
suffix=".GSStorageFile",
dir=setting("FILE_UPLOAD_TEMP_DIR", None)
)
if 'r' in self._mode:
self._is_dirty = False
self.blob.download_to_file(self._file)
self._file.seek(0)
return self._file
def _set_file(self, value):
self._file = value
file = property(_get_file, _set_file)
def read(self, num_bytes=None):
if 'r' not in self._mode:
raise AttributeError("File was not opened in read mode.")
if num_bytes is None:
num_bytes = -1
return super(GoogleCloudFile, self).read(num_bytes)
def write(self, content):
if 'w' not in self._mode:
raise AttributeError("File was not opened in write mode.")
self._is_dirty = True
return super(GoogleCloudFile, self).write(force_bytes(content))
def close(self):
if self._file is not None:
if self._is_dirty:
self.file.seek(0)
self.blob.upload_from_file(self.file, content_type=self.mime_type)
self._file.close()
self._file = None
示例8: close
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
def close(self):
"""On close, seek to 0 and write the data via the API, then close()
for realz
"""
logger.debug("close() called on WriteBuffer")
self.seek(0)
logger.debug("Attempting to create file at dir_path %s with name %s" %
(self.path, self.filename))
self.fs.rc.fs.write_file(self, self.fullpath)
SpooledTemporaryFile.close(self) # old-style class!
示例9: __init__
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
def __init__(self, path, filename, fs, max_size=WRITE_BUFFER_SIZE):
"""We need the path so we can write the buffered file to the API"""
SpooledTemporaryFile.__init__(self, max_size=max_size) # old-style!
self.path = path
self.filename = filename
self.fs = fs
self.fullpath = ''
try:
self.fullpath = self.create_file()
except RequestError, e:
SpooledTemporaryFile.close(self)
raise FilesystemError(str(e))
示例10: upload_file
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
def upload_file(self, user, stream, expected_size, filename, force_coll_name=''):
"""Upload WARC archive.
:param User user: user
:param stream: file object
:param int expected_size: expected WARC archive size
:param str filename: WARC archive filename
:param str force_coll_name: name of collection to upload into
:returns: upload information
:rtype: dict
"""
temp_file = None
logger.debug('Upload Begin')
logger.debug('Expected Size: ' + str(expected_size))
#is_anon = False
size_rem = user.get_size_remaining()
logger.debug('User Size Rem: ' + str(size_rem))
if size_rem < expected_size:
return {'error': 'out_of_space'}
if force_coll_name and not user.has_collection(force_coll_name):
#if is_anon:
# user.create_collection(force_coll, 'Temporary Collection')
#else:
#status = 'Collection {0} not found'.format(force_coll_name)
return {'error': 'no_such_collection'}
temp_file = SpooledTemporaryFile(max_size=BLOCK_SIZE)
stream = CacheingLimitReader(stream, expected_size, temp_file)
if filename.endswith('.har'):
stream, expected_size = self.har2warc(filename, stream)
temp_file.close()
temp_file = stream
infos = self.parse_uploaded(stream, expected_size)
total_size = temp_file.tell()
if total_size != expected_size:
return {'error': 'incomplete_upload', 'expected': expected_size, 'actual': total_size}
upload_id, upload_key = self._init_upload_status(user, total_size, 1, filename=filename)
return self.handle_upload(temp_file, upload_id, upload_key, infos, filename,
user, force_coll_name, total_size)
示例11: pipestring_process
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
def pipestring_process(cmd_string, stdin_string=''):
"""Pipe a python string to standard input for cmd_string
>>> pipestring_process('grep 2', '1\\n2\\n3\\n')
(0, '2\\n', '')
"""
f=SpooledTemporaryFile()
f.write(stdin_string)
f.seek(0)
results=process(cmd_string, stdin=f)
f.close()
return results
示例12: image
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
def image(type, spec=' ', ext='png'):
# Parameters for `suml`.
import suml.common
import optparse
options = optparse.Values(({
'scruffy': True,
'png': ext == 'png',
'svg': ext == 'svg' or ext == 'pdf',
'font': os.getenv('SCRUFFY_FONT', suml.common.defaultScruffyFont()),
'shadow': False,
}))
from tempfile import SpooledTemporaryFile
fout = SpooledTemporaryFile()
# Execute Scruffy `suml`.
if type == 'class':
suml.yuml2dot.transform(spec, fout, options)
elif type == 'sequence':
suml.suml2pic.transform(spec, fout, options)
else:
return HTTPError(404, 'Unhandled diagram type.')
# Retrieve the data generated.
fout.seek(0)
data = fout.read()
fout.close()
# Convert SVG to PDF?
if ext == 'pdf':
# Load SVG file.
doc = xml.dom.expatbuilder.parseString(data)
# Convert to a RLG drawing
svg_renderer = svglib.svglib.SvgRenderer()
svg_renderer.render(doc.documentElement)
drawing = svg_renderer.finish()
# Generate PDF.
data = reportlab.graphics.renderPDF.drawToString(drawing)
# Server the generated image.
if ext == 'png':
response.content_type = 'image/png'
elif ext == 'svg':
response.content_type = 'image/svg+xml'
elif ext == 'pdf':
response.content_type = 'application/pdf'
else:
return HTTPError(500, 'Unhandled extension type.')
return data
示例13: do_execute
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
def do_execute(self, code, silent, store_history=True, user_expressions=None,
allow_stdin=False):
"""Execute user code."""
if len(code.strip()) == 0:
return {'status': 'ok', 'execution_count': self.execution_count,
'payload': [], 'user_expressions': {}}
env = builtins.__xonsh_env__
shell = builtins.__xonsh_shell__
hist = builtins.__xonsh_history__
enc = env.get('XONSH_ENCODING')
out = SpooledTemporaryFile(max_size=MAX_SIZE, mode='w+t',
encoding=enc, newline='\n')
err = SpooledTemporaryFile(max_size=MAX_SIZE, mode='w+t',
encoding=enc, newline='\n')
try:
with redirect_stdout(out), redirect_stderr(err), \
swap(builtins, '__xonsh_stdout_uncaptured__', out), \
swap(builtins, '__xonsh_stderr_uncaptured__', err), \
env.swap({'XONSH_STORE_STDOUT': False}):
shell.default(code)
interrupted = False
except KeyboardInterrupt:
interrupted = True
if not silent: # stdout response
if out.tell() > 0:
out.seek(0)
self._respond_in_chunks('stdout', out.read())
if err.tell() > 0:
err.seek(0)
self._respond_in_chunks('stderr', err.read())
if hasattr(builtins, '_') and builtins._ is not None:
# rely on sys.displayhook functionality
self._respond_in_chunks('stdout', pformat(builtins._))
builtins._ = None
if len(hist) > 0 and out.tell() == 0 and err.tell() == 0:
self._respond_in_chunks('stdout', hist.outs[-1])
out.close()
err.close()
if interrupted:
return {'status': 'abort', 'execution_count': self.execution_count}
rtn = 0 if len(hist) == 0 else hist.rtns[-1]
if 0 < rtn:
message = {'status': 'error', 'execution_count': self.execution_count,
'ename': '', 'evalue': str(rtn), 'traceback': []}
else:
message = {'status': 'ok', 'execution_count': self.execution_count,
'payload': [], 'user_expressions': {}}
return message
示例14: do_execute
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
def do_execute(self, code, silent, store_history=True, user_expressions=None, allow_stdin=False):
"""Execute user code."""
if len(code.strip()) == 0:
return {"status": "ok", "execution_count": self.execution_count, "payload": [], "user_expressions": {}}
env = builtins.__xonsh_env__
shell = builtins.__xonsh_shell__
hist = builtins.__xonsh_history__
enc = env.get("XONSH_ENCODING")
out = SpooledTemporaryFile(max_size=MAX_SIZE, mode="w+t", encoding=enc, newline="\n")
err = SpooledTemporaryFile(max_size=MAX_SIZE, mode="w+t", encoding=enc, newline="\n")
try:
with redirect_stdout(out), redirect_stderr(err), swap(builtins, "__xonsh_stdout_uncaptured__", out), swap(
builtins, "__xonsh_stderr_uncaptured__", err
), env.swap({"XONSH_STORE_STDOUT": False}):
shell.default(code)
interrupted = False
except KeyboardInterrupt:
interrupted = True
if not silent: # stdout response
if out.tell() > 0:
out.seek(0)
self._respond_in_chunks("stdout", out.read())
if err.tell() > 0:
err.seek(0)
self._respond_in_chunks("stderr", err.read())
if hasattr(builtins, "_") and builtins._ is not None:
# rely on sys.displayhook functionality
self._respond_in_chunks("stdout", pformat(builtins._))
builtins._ = None
if len(hist) > 0 and out.tell() == 0 and err.tell() == 0:
self._respond_in_chunks("stdout", hist.outs[-1])
out.close()
err.close()
if interrupted:
return {"status": "abort", "execution_count": self.execution_count}
rtn = 0 if len(hist) == 0 else hist.rtns[-1]
if 0 < rtn:
message = {
"status": "error",
"execution_count": self.execution_count,
"ename": "",
"evalue": str(rtn),
"traceback": [],
}
else:
message = {"status": "ok", "execution_count": self.execution_count, "payload": [], "user_expressions": {}}
return message
示例15: TempInput
# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import close [as 别名]
class TempInput(object):
def __init__(self, inputstr):
self.inputstr = inputstr
def __enter__(self):
self.tempfile = SpooledTemporaryFile()
self.tempfile.write(self.inputstr)
self.tempfile.seek(0)
return self.tempfile
def __exit__(self, type_, value, traceback):
self.tempfile.close()
return False