text
stringlengths
0
828
return {
'string': data,
'mime_type': mimetypes.guess_type(url)[0],
}
else:
return default_url_fetcher(url)"
631,"def render_pdf(
template,
file_,
url_fetcher=staticfiles_url_fetcher,
context=None,
):
""""""
Writes the PDF data into ``file_``. Note that ``file_`` can actually be a
Django Response object as well.
This function may be used as a helper that can be used to save a PDF file
to a file (or anything else outside of a request/response cycle), eg::
:param str html: A rendered HTML.
:param file file_: A file like object (or a Response) where to output
the rendered PDF.
""""""
context = context or {}
html = get_template(template).render(context)
HTML(
string=html,
base_url='not-used://',
url_fetcher=url_fetcher,
).write_pdf(
target=file_,
)"
632,"def encode_bytes(src_buf, dst_file):
""""""Encode a buffer length followed by the bytes of the buffer
itself.
Parameters
----------
src_buf: bytes
Source bytes to be encoded. Function asserts that
0 <= len(src_buf) <= 2**16-1.
dst_file: file
File-like object with write method.
Returns
-------
int
Number of bytes written to `dst_file`.
""""""
if not isinstance(src_buf, bytes):
raise TypeError('src_buf must by bytes.')
len_src_buf = len(src_buf)
assert 0 <= len_src_buf <= 2**16-1
num_written_bytes = len_src_buf + 2
len_buf = FIELD_U16.pack(len_src_buf)
dst_file.write(len_buf)
dst_file.write(src_buf)
return num_written_bytes"
633,"def decode_bytes(f):
""""""Decode a buffer length from a 2-byte unsigned int then read the
subsequent bytes.
Parameters
----------
f: file
File-like object with read method.
Raises
------
UnderflowDecodeError
When the end of stream is encountered before the end of the
encoded bytes.
Returns
-------
int
Number of bytes read from `f`.
bytes
Value bytes decoded from `f`.
""""""
buf = f.read(FIELD_U16.size)
if len(buf) < FIELD_U16.size:
raise UnderflowDecodeError()
(num_bytes,) = FIELD_U16.unpack_from(buf)
num_bytes_consumed = FIELD_U16.size + num_bytes
buf = f.read(num_bytes)
if len(buf) < num_bytes:
raise UnderflowDecodeError()
return num_bytes_consumed, buf"
634,"def encode_utf8(s, f):
""""""UTF-8 encodes string `s` to file-like object `f` according to
the MQTT Version 3.1.1 specification in section 1.5.3.