Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
try:
# Attachments from document links like or <a> can only be URLs.
# They're passed in as tuples
if isinstance(attachment, tuple):
url, description = attachment
attachment = Attachment(
url=url, url_fetcher=url_fetcher, description=description)
elif not isinstance(attachment, Attachment):
attachment = Attachment(guess=attachment, url_fetcher=url_fetcher)
with attachment.source as (source_type, source, url, _):
if isinstance(source, bytes):
source = io.BytesIO(source)
file_stream_id = _write_compressed_file_object(pdf, source)
except URLFetchingError as exc:
LOGGER.error('Failed to load attachment: %s', exc)
return None
# TODO: Use the result object from a URL fetch operation to provide more
# details on the possible filename
filename = _get_filename_from_result(url, None)
return pdf.write_new_object(pdf_format(
'<< /Type /Filespec /F () /UF {0!P} /EF << /F {1} 0 R >> '
'/Desc {2!P}\n>>',
filename,
file_stream_id,
attachment.description or ''))
</a>
base_url=base_url, url_fetcher=url_fetcher,
check_css_mime_type=check_css_mime_type,
**{type_: guess})
with result as result:
yield result
elif filename is not None:
if isinstance(filename, Path):
filename = str(filename)
if base_url is None:
base_url = path2url(filename)
with open(filename, 'rb') as file_obj:
yield 'file_obj', file_obj, base_url, None
elif url is not None:
with fetch(url_fetcher, url) as result:
if check_css_mime_type and result['mime_type'] != 'text/css':
LOGGER.error(
'Unsupported stylesheet type %s for %s',
result['mime_type'], result['redirected_url'])
yield 'string', '', base_url, None
else:
proto_encoding = result.get('encoding')
if base_url is None:
base_url = result.get('redirected_url', url)
if 'string' in result:
yield 'string', result['string'], base_url, proto_encoding
else:
yield (
'file_obj', result['file_obj'], base_url,
proto_encoding)
elif file_obj is not None:
if base_url is None:
# filesystem file-like objects have a 'name' attribute.
style.font_variant_position, style.font_variant_caps,
style.font_variant_numeric, style.font_variant_alternates,
style.font_variant_east_asian, style.font_feature_settings)
if features:
features = ','.join(
('%s %i' % (key, value)) for key, value in features.items())
# TODO: attributes should be freed.
# In the meantime, keep a cache to avoid leaking too many of them.
attr = PANGO_ATTR_FONT_FEATURES_CACHE.get(features)
if attr is None:
try:
attr = pango.pango_attr_font_features_new(
features.encode('ascii'))
except AttributeError:
LOGGER.error(
'OpenType features are not available with Pango < 1.38')
else:
PANGO_ATTR_FONT_FEATURES_CACHE[features] = attr
if attr is not None:
attr_list = pango.pango_attr_list_new()
pango.pango_attr_list_insert(attr_list, attr)
pango.pango_layout_set_attributes(layout.layout, attr_list)
# Tabs width
if style.tab_size != 8: # Default Pango value is 8
layout.set_tabs(style)
return layout
raise ImageLoadingError(
'Could not load GDK-Pixbuf. PNG and SVG are '
'the only image formats available.')
try:
image = SVGImage(string, url, url_fetcher)
except BaseException:
try:
surface, format_name = (
pixbuf.decode_to_image_surface(string))
except pixbuf.ImageLoadingError as exception:
raise ImageLoadingError(str(exception))
if format_name == 'jpeg':
surface.set_mime_data('image/jpeg', string)
image = RasterImage(surface)
except (URLFetchingError, ImageLoadingError) as exc:
LOGGER.error('Failed to load image at "%s" (%s)', url, exc)
image = None
cache[url] = image
return image
def url_join(base_url, url, allow_relative, context, context_args):
"""Like urllib.urljoin, but warn if base_url is required but missing."""
if url_is_absolute(url):
return iri_to_uri(url)
elif base_url:
return iri_to_uri(urljoin(base_url, url))
elif allow_relative:
return iri_to_uri(url)
else:
LOGGER.error('Relative URI reference without a base URI: ' + context,
*context_args)
return None
url_fetcher=url_fetcher, media_type=device_media_type,
font_config=font_config, page_rules=page_rules)
yield css
elif element.tag == 'link' and element.get('href'):
if not element_has_link_type(element, 'stylesheet') or \
element_has_link_type(element, 'alternate'):
continue
href = get_url_attribute(element, 'href', base_url)
if href is not None:
try:
yield CSS(
url=href, url_fetcher=url_fetcher,
_check_mime_type=True, media_type=device_media_type,
font_config=font_config, page_rules=page_rules)
except URLFetchingError as exc:
LOGGER.error(
'Failed to load stylesheet at %s : %s', href, exc)