Ejemplo n.º 1
0
    def save(self, filename=None, fileformat=None,
             rankdir='LR', prog=None,
             wrap=10, tikz=False):
        """Save image to file.

        Recommended file formats:

            - tikz (via dot2tex)
            - pdf
            - svg
            - dot
            - png

        Any other format supported by C{pydot.write} is available.

        Experimental:

            - html (uses d3.js)
            - 'scxml'

        Requires
        ========
          - graphviz dot: http://www.graphviz.org/
          - pydot: https://pypi.python.org/pypi/pydot

        and for tikz:

          - dot2tex: https://pypi.python.org/pypi/dot2tex
          - dot2texi: http://www.ctan.org/pkg/dot2texi
            (to automate inclusion)

        See Also
        ========
        L{plot}, C{pydot.Dot.write}

        @param filename: file path to save image to
            Default is C{self.name}, unless C{name} is empty,
            then use 'out.pdf'.

            If extension is missing '.pdf' is used.
        @type filename: str

        @param fileformat: replace the extension of C{filename}
            with this. For example::

                filename = 'fig.pdf'
                fileformat = 'svg'

            result in saving 'fig.svg'

        @param rankdir: direction for dot layout
        @type rankdir: str = 'TB' | 'LR'
            (i.e., Top->Bottom | Left->Right)

        @param prog: executable to call
        @type prog: dot | circo | ... see pydot.Dot.write

        @param wrap: max width of node strings
        @type wrap: int

        @param tikz: use tikz automata library in dot

        @rtype: bool
        @return: True if saving completed successfully, False otherwise.
        """
        if filename is None:
            if not self.name:
                filename = 'out'
            else:
                filename = self.name
        fname, fextension = os.path.splitext(filename)
        # default extension
        if not fextension or fextension is '.':
            fextension = '.pdf'
        if fileformat:
            fextension = '.' + fileformat
        filename = fname + fextension
        # drop '.'
        fileformat = fextension[1:]
        # check for html
        if fileformat is 'html':
            from tulip.transys.export import save_d3
            return save_d3.labeled_digraph2d3(self, filename)
        # subclass has extra export formats ?
        if hasattr(self, '_save'):
            if self._save(filename, fileformat):
                return True
        if prog is None:
            prog = self.default_layout
        from tulip.transys.export import graph2dot
        graph2dot.save_dot(self, filename, fileformat, rankdir,
                           prog, wrap, tikz=tikz)
        return True
Ejemplo n.º 2
0
    def save(self, filename=None, fileformat=None,
             rankdir='LR', prog=None,
             wrap=10, tikz=False):
        """Save image to file.

        Recommended file formats:

            - tikz (via dot2tex)
            - pdf
            - svg
            - dot
            - png

        Any other format supported by C{pydot.write} is available.

        Experimental:

            - html (uses d3.js)
            - 'scxml'

        Requires
        ========
          - graphviz dot: http://www.graphviz.org/
          - pydot: https://pypi.python.org/pypi/pydot

        and for tikz:

          - dot2tex: https://pypi.python.org/pypi/dot2tex
          - dot2texi: http://www.ctan.org/pkg/dot2texi
            (to automate inclusion)

        See Also
        ========
        L{plot}, C{pydot.Dot.write}

        @param filename: file path to save image to
            Default is C{self.name}, unless C{name} is empty,
            then use 'out.pdf'.

            If extension is missing '.pdf' is used.
        @type filename: str

        @param fileformat: replace the extension of C{filename}
            with this. For example::

                filename = 'fig.pdf'
                fileformat = 'svg'

            result in saving 'fig.svg'

        @param rankdir: direction for dot layout
        @type rankdir: str = 'TB' | 'LR'
            (i.e., Top->Bottom | Left->Right)

        @param prog: executable to call
        @type prog: dot | circo | ... see pydot.Dot.write

        @param wrap: max width of node strings
        @type wrap: int

        @param tikz: use tikz automata library in dot

        @rtype: bool
        @return: True if saving completed successfully, False otherwise.
        """
        if filename is None:
            if not self.name:
                filename = 'out'
            else:
                filename = self.name
        fname, fextension = os.path.splitext(filename)
        # default extension
        if not fextension or fextension is '.':
            fextension = '.pdf'
        if fileformat:
            fextension = '.' + fileformat
        filename = fname + fextension
        # drop '.'
        fileformat = fextension[1:]
        # check for html
        if fileformat is 'html':
            from tulip.transys.export import save_d3
            return save_d3.labeled_digraph2d3(self, filename)
        # subclass has extra export formats ?
        if hasattr(self, '_save'):
            if self._save(filename, fileformat):
                return True
        if prog is None:
            prog = self.default_layout
        from tulip.transys.export import graph2dot
        graph2dot.save_dot(self, filename, fileformat, rankdir,
                           prog, wrap, tikz=tikz)
        return True