Ejemplo n.º 1
0
def save_project(spj, filename=None, path=None):
    """
    Write the whole project to a file.  Return True on success.
    
    The archive that is created is a gzipped tar file containing
    the XML file with the project info and additionally the data
    files containing the information from the current Dataset
    objects.
    """

    #
    # write project XML file
    #

    tempdir = tempfile.mkdtemp(prefix="spj-export-")
    filename = filename or spj.filename
    if filename is None:
        raise RuntimeError("No valid filename specified.")                                              

    try:
        projectfile = os.path.join( tempdir,'project.xml' )
        e = toElement(spj)

        fd = open(projectfile, 'w')
        fd.write('<?xml version="1.0" encoding="utf-8"?>\n')
        ElementTree(e).write(fd, encoding="utf-8")
        fd.close()

        #
        # now add all extra information to the tempdir
        # (Datasets and other files)
        #

        # add Dataset files to tempdir
        exporter = ExporterRegistry.new_instance(DEFAULT_FF)

        dsdir = os.path.join(tempdir, 'datasets')
        os.mkdir(dsdir)
        for ds in spj.datasets:
            try:
                dspath = os.path.join(dsdir, dataset_filename(ds.key))
                exporter.write_to_file(dspath, ds.data)
            except AttributeError:
                logger.error("Error while writing Dataset '%s'" % ds.key)
                raise
            except NoData:
                logger.error("Warning, empty Dataset -- no data file written.")


        #
        # create tar archive from tempdir
        #
        try:
            archive = None
            try:
                if path is not None:
                    filename = os.path.join(path, os.path.basename(filename))
                logger.info("Writing archive '%s'" % filename)
                archive = tarfile.open(filename, mode="w:gz")
                archive.add( tempdir, '' )
            except IOError,(nr, msg):
                logger.error('Error while creating archive "%s": %s' % (filename, msg))
                return False

        finally:
            if archive is not None:
                archive.close()
    finally:
        logger.debug("Removing directory %s" % tempdir)
        shutil.rmtree(tempdir)
        
    logger.debug("Finished writing '%s'" % filename)
    return True
Ejemplo n.º 2
0
    def redraw(self, rebuild_cache=True):

        # All commands for gnuplot are appended to the cmd_list,
        # so that they can be executed at the very end.
        cmd_list = []
        cmd_list.append('cd "%s"' % self.tmpdir)
	cmd_list.append( "set encoding %s" % self.encoding )

        cmd_list += self.terminal.build(self)     

        # multiplot ?
        if len(self.plot.layers) > 1:
            cmd_list.append( "set multiplot" )
            for layer in self.plot.layers:
                group_info = {}
                x, y = uwrap.get(layer, 'x'), uwrap.get(layer, 'y')
                width, height = uwrap.get(layer, 'width'), uwrap.get(layer, 'height')
                cmd_list.append("set origin %.2f,%.2f" % (x,y))
                cmd_list.append("set size %.2f,%.2f" % (width, height))
                cmd_list += self.build_layer(layer, group_info)
            cmd_list.append( "unset multiplot" )
        else:
            # Single plot!
            # create plotting commands from the Layer information
            group_info = {}
            cmd_list += self.build_layer(self.plot.layers[0], group_info)


        # Export Datasets to temporary directory, so that
        # gnuplot can access them.
        exporter = ExporterRegistry.new_instance('ASCII')
        
        destdir = self.tmpdir
        for (source, value) in self.exports.iteritems():
            (filename, change_counter, ds) = value
            if ds is None:
                logger.warn("One of the Datasets to export is None.")
                continue
            if ds.is_empty():
                logger.warn("One of the Datasets to export is empty")
                continue
            logging.debug("Change counter %d, old %d" % (ds.change_counter, change_counter))
            if ds.has_changes(change_counter):                              
                filename = os.path.join(destdir, filename)
                logger.debug('exporting "%s" to dir "%s"' % (ds, destdir))            
                exporter.write_to_file(filename, ds.data)
                self.exports[source][1] = ds.change_counter
            else:
                logger.info("Dataset has not changed and is not exported!")
                
       
        # Now execute all collected commands.
        print "cmd list is: "
        for cmd in cmd_list:
            print "   ", cmd
        print
        
        Signals.emit(self, 'gnuplot-start-plotting')
        logger.info("Gnuplot command list:\n\n%s" % "\n".join(cmd_list))
        for cmd in cmd_list:
            self(cmd)

        Signals.emit(self,'gnuplot-after-plot', window_title=self.window_title)