Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test(format_name=format_name):
"""Test the generation of ``format_name`` images."""
content = cairosvg.SURFACES[format_name].convert(url=svg_filename)
assert content.startswith(MAGIC_NUMBERS[format_name])
test.description = 'Test that the output from svg2%s looks like %s' % (
def test_formats(format_name):
"""Convert to a given format and test that output looks right."""
content = SURFACES[format_name].convert(SVG_SAMPLE)
assert content.startswith(MAGIC_NUMBERS[format_name])
def convert(self, format="png", **kwargs):
"""
png, ps, pdf, gif, jpg, svg
returns image in format as bytes
"""
if format.upper() in cairosvg.SURFACES:
surface = cairosvg.SURFACES[format.upper()]
else:
raise Exception("'%s' image format unavailable: use one of %s" %
(format.upper(), list(cairosvg.SURFACES.keys())))
return surface.convert(bytestring=str(self), **kwargs)
def main(argv=None, stdout=None, stdin=None):
"""Entry-point of the executable."""
# Get command-line options
parser = argparse.ArgumentParser(
description='Convert SVG files to other formats')
parser.add_argument('input', default='-', help='input filename or URL')
parser.add_argument(
'-v', '--version', action='version', version=VERSION)
parser.add_argument(
'-f', '--format', help='output format',
choices=sorted([surface.lower() for surface in SURFACES]))
parser.add_argument(
'-d', '--dpi', default=96, type=float,
help='ratio between 1 inch and 1 pixel')
parser.add_argument(
'-W', '--width', default=None, type=float,
help='width of the parent container in pixels')
parser.add_argument(
'-H', '--height', default=None, type=float,
help='height of the parent container in pixels')
parser.add_argument(
'-s', '--scale', default=1, type=float, help='output scaling factor')
parser.add_argument(
'-b', '--background', metavar='COLOR', help='output background color')
parser.add_argument(
'-n', '--negate-colors', action='store_true',
help='replace every vector color with its complement')
def convert(self, format="png", **kwargs):
"""
png, ps, pdf, gif, jpg, svg
returns image in format as bytes
"""
if format.upper() in cairosvg.SURFACES:
surface = cairosvg.SURFACES[format.upper()]
else:
raise Exception("'%s' image format unavailable: use one of %s" %
(format.upper(), list(cairosvg.SURFACES.keys())))
return surface.convert(bytestring=str(self), **kwargs)
'output_width': options.output_width,
'output_height': options.output_height}
stdin = stdin or sys.stdin
stdout = stdout or sys.stdout
kwargs['write_to'] = (
stdout.buffer if options.output == '-' else options.output)
if options.input == '-':
kwargs['file_obj'] = stdin.buffer
else:
kwargs['url'] = options.input
output_format = (
options.format or
os.path.splitext(options.output)[1].lstrip('.') or
'pdf').upper()
SURFACES[output_format.upper()].convert(**kwargs)