Example #1
0
File: svg.py Project: PP-TSD/sdgen
def to_png(data, path=None, conf=None):
    '''
    Generate a svg image(s). This function can generate multiple images.
    @param data: input data
    @param path: path were to save files;
                             if the value is None no file is saved, overwriting is forbidden (raise exception)
    @param conf: configuration to substitute the default one
    @return: a list of tuples, each tuple contains two fields: a name and an image in svg format
    '''
    views_to_render = [data]
    views_to_render.extend(find_non_terminals(data))
    result = []
    for view in views_to_render:
        result.append((view["name"], create_diagram(view, Configuration(conf))))

    if path != None:
        for image in result:
            # replace spaces to underscores
            fstr = image[0].replace(" ", "_")

            file_name = os.path.join(path, fstr + ".svg")
            #if os.path.exists(file_name):
            #    raise Exception, "File already exists!"
            with open(file_name, 'w') as f:
                f.write(image[1].encode('utf-8'))

            #command = 'python CairoSVG-0.3.1//cairosvg.py file_name -f png -o ' + os.path.join(path, image[0] + ".png"
            #os.system(command);
            cairosvg.main(file_name, 'png', 300, os.path.join(path, fstr + ".png"))

    return result
Example #2
0
    def test_main(args, exit_=False, input_=None):
        """Test main called with given ``args``.

        If ``exit_`` is ``True``, check that ``SystemExit`` is raised. We then
        assume that the program output is an unicode string.

        If ``input_`` is given, use this stream as input stream.

        """
        sys.argv = ['cairosvg.py'] + args
        old_stdin, old_stdout = sys.stdin, sys.stdout

        output_buffer = io.BytesIO()
        if sys.version_info[0] >= 3:
            sys.stdout = io.TextIOWrapper(output_buffer)
        else:
            sys.stdout = output_buffer

        if input_:
            kwargs = {'stdin': open(input_, 'rb')}
            sys.stdin = open(input_, 'rb')
        else:
            kwargs = {}

        if exit_:
            try:
                main()
            except SystemExit:
                pass
            else:
                raise Exception('CairoSVG did not exit')
        else:
            main()

        sys.stdout.flush()
        output = output_buffer.getvalue()
        sys.stdin, sys.stdout = old_stdin, old_stdout
        eq_(output, run(*args, **kwargs))

        return output
Example #3
0
    def test_main(args, exit_=False, input_=None):
        """Test main called with given ``args``.

        If ``exit_`` is ``True``, check that ``SystemExit`` is raised. We then
        assume that the program output is an unicode string.

        If ``input_`` is given, use this stream as input stream.

        """
        sys.argv = ['cairosvg.py'] + args
        old_stdin, old_stdout = sys.stdin, sys.stdout

        output_buffer = io.BytesIO()
        if sys.version_info[0] >= 3:
            sys.stdout = io.TextIOWrapper(output_buffer)
        else:
            sys.stdout = output_buffer

        if input_:
            kwargs = {'stdin': open(input_, 'rb')}
            sys.stdin = open(input_, 'rb')
        else:
            kwargs = {}

        if exit_:
            try:
                main()
            except SystemExit:
                pass
            else:
                raise Exception('CairoSVG did not exit')
        else:
            main()

        sys.stdout.flush()
        output = output_buffer.getvalue()
        sys.stdin, sys.stdout = old_stdin, old_stdout
        eq_(output, run(*args, **kwargs))

        return output
Example #4
0
#!/usr/bin/env python
#
# This file is part of CairoSVG
# Copyright © 2010-2015 Kozea
#
# This library is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with CairoSVG.  If not, see <http://www.gnu.org/licenses/>.
"""
CairoSVG entry point.

"""

import cairosvg
cairosvg.main()
Example #5
0
#http://cairosvg.org/user_documentation/
import cairosvg
cairosvg.main()
Example #6
0
def svg2png(svgsrc,pngfile):
    cairosvg.main()