Example #1
0
    def save(self, fname=None):
        """Saves the plot.

        @param fname: the filename to save to. It is ignored if the surface
          of the plot is not an C{ImageSurface}.
        """
        if self._is_dirty:
            self.redraw()
        if isinstance(self._surface, cairo.ImageSurface):
            if fname is None and self._need_tmpfile:
                with named_temporary_file(prefix="igraph", suffix=".png") as fname:
                    self._surface.write_to_png(fname)
                    return None

            fname = fname or self._filename
            if fname is None:
                raise ValueError(
                    "no file name is known for the surface " + "and none given"
                )
            return self._surface.write_to_png(fname)

        if fname is not None:
            warn("filename is ignored for surfaces other than ImageSurface")

        self._ctx.show_page()
        self._surface.finish()
Example #2
0
    def show(self):
        """Saves the plot to a temporary file and shows it."""
        if not isinstance(self._surface, cairo.ImageSurface):
            sur = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(self.bbox.width),
                                     int(self.bbox.height))
            ctx = cairo.Context(sur)
            self.redraw(ctx)
        else:
            sur = self._surface
            ctx = self._ctx
            if self._is_dirty:
                self.redraw(ctx)

        with named_temporary_file(prefix="igraph", suffix=".png") as tmpfile:
            sur.write_to_png(tmpfile)
            config = Configuration.instance()
            imgviewer = config["apps.image_viewer"]
            if not imgviewer:
                # No image viewer was given and none was detected. This
                # should only happen on unknown platforms.
                plat = platform.system()
                raise NotImplementedError("showing plots is not implemented " + \
                                          "on this platform: %s" % plat)
            else:
                os.system("%s %s" % (imgviewer, tmpfile))
                if platform.system() == "Darwin" or self._windows_hacks:
                    # On Mac OS X and Windows, launched applications are likely to
                    # fork and give control back to Python immediately.
                    # Chances are that the temporary image file gets removed
                    # before the image viewer has a chance to open it, so
                    # we wait here a little bit. Yes, this is quite hackish :(
                    time.sleep(5)
Example #3
0
    def save(self, fname=None):
        """Saves the plot.

        @param fname: the filename to save to. It is ignored if the surface
          of the plot is not an C{ImageSurface}.
        """
        if self._is_dirty:
            self.redraw()
        if isinstance(self._surface, cairo.ImageSurface):
            if fname is None and self._need_tmpfile:
                with named_temporary_file(prefix="igraph", suffix=".png") as fname:
                    self._surface.write_to_png(fname)
                    return None

            fname  = fname or self._filename
            if fname is None:
                raise ValueError("no file name is known for the surface " + \
                                 "and none given")
            return self._surface.write_to_png(fname)

        if fname is not None:
            warn("filename is ignored for surfaces other than ImageSurface")

        self._ctx.show_page()
        self._surface.finish()
Example #4
0
    def show(self):
        """Saves the plot to a temporary file and shows it."""
        if not isinstance(self._surface, cairo.ImageSurface):
            sur = cairo.ImageSurface(cairo.FORMAT_ARGB32,
                    int(self.bbox.width), int(self.bbox.height))
            ctx = cairo.Context(sur)
            self.redraw(ctx)
        else:
            sur = self._surface
            ctx = self._ctx
            if self._is_dirty:
                self.redraw(ctx)

        with named_temporary_file(prefix="igraph", suffix=".png") as tmpfile:
            sur.write_to_png(tmpfile)
            config = Configuration.instance()
            imgviewer = config["apps.image_viewer"]
            if not imgviewer:
                # No image viewer was given and none was detected. This
                # should only happen on unknown platforms.
                plat = platform.system()
                raise NotImplementedError("showing plots is not implemented " + \
                                          "on this platform: %s" % plat)
            else:
                os.system("%s %s" % (imgviewer, tmpfile))
                if platform.system() == "Darwin" or self._windows_hacks:
                    # On Mac OS X and Windows, launched applications are likely to
                    # fork and give control back to Python immediately.
                    # Chances are that the temporary image file gets removed
                    # before the image viewer has a chance to open it, so
                    # we wait here a little bit. Yes, this is quite hackish :(
                    time.sleep(5)
Example #5
0
def pad2igraph(gid, url, format, delete=False, store="/pads/", debug=True):

    print("pad2igraph", gid, url, format)

    if format == 'csv':

        try:
            description = "imported from %s" % url
            if url[0:4] != 'http':
                url = "%s/%s.%s" % (store, url, format)
            bot = BotaIgraph(directed=True)
            botapad = Botapad(bot,
                              gid,
                              description,
                              delete=delete,
                              verbose=True,
                              debug=False)
            botapad.parse(url, separator='auto', debug=debug)
            graph = bot.get_igraph(weight_prop="weight")

            if graph.vcount() == 0:
                raise BotapadParseError(
                    url, "Botapad can't create a graph without nodes.", None)

            return prepare_graph(gid, graph)

        except BotapadParseError as e:
            log = botapad.get_log()
            e.log = log
            raise e

        except OSError as e:
            raise BotapadURLError("No such File or Directory : %s " % url, url)

    elif format in AVAILABLE_FORMATS:
        content = None
        if url[0:4] == 'http':
            try:
                url = convert_url(path)
                if format in ('pickle', 'picklez'):
                    raise ValueError('no pickle from HTTP : %s ' % url)
                print(" === Downloading %s %s\n" % (url, separator))
                content = requests.get(url).text
            except:
                raise BotapadURLError("Can't download %s" % url, url)

        else:
            try:
                print(" === reading  %s/%s.%s" % (store, url, format))
                content = open("%s/%s.%s" % (store, url, format), 'r').read()
            except Exception as err:
                raise BotapadURLError("Can't open file %s: %s" % (url, err),
                                      url)

        try:
            with named_temporary_file(text=False) as tmpf:
                outf = open(tmpf, "wt")
                outf.write(content)
                outf.close()
                graph = igraph.read(tmpf, format=format)

            return prepare_graph(gid, graph)

        except Exception as err:
            raise
            raise BotapadError('%s : cannot read %s file at %s : %s' %
                               (gid, format, url, err))

    else:
        raise BotapadError('%s : Unsupported format %s file at %s ' %
                           (gid, format, url))