Esempio n. 1
0
    def import_datasets(self, filenames, importer, progresslist=None, undolist=None):
        if undolist is None:
            undolist = self.journal

        if isinstance(importer, basestring):
            importer = ImporterRegistry.new_instance(importer)
        elif not isinstance(importer, Importer):
            raise TypeError("'importer' needs to be a key or a valid Importer instance.")

        # To ensure a proper undo, the Datasets are imported one by one
        # to a temporary dict.  When finished, they are added as a whole.
        new_datasets = list()

        progresslist = progresslist or self.app.progresslist
        pl = progresslist(filenames)
        for filename in pl:
            try:
                tbl = importer.read_table_from_file(filename)
            except ImportError, msg:
                pl.fail(msg)
                continue

            root, ext = os.path.splitext(basename(filename))
            filename = utils.encode_as_key(root)
            ds = Dataset(key=filename, data=tbl)
            ds.metadata['Import-Source'] = unicode(filename)
            ds.metadata['Import-Filter'] = unicode(importer.blurb)

            new_datasets.append(ds)
            pl.succeed()
        n = 0.0
        N = len(filenames)
        for filename in filenames:
            yield ("Importing %s" % filename, n/N)

            try:
                tbl = importer.read_table_from_file(filename)
            except ImportError, msg:
                self.error_message(msg)
                continue
            except error.UserCancel:
                self.error_message("Import aborted by user")
                continue

            root, ext = os.path.splitext(os.path.basename(filename))
            filename = utils.encode_as_key(root)
            ds = Dataset(key=filename, data=tbl)
            ds.metadata['Import-Source'] = unicode(filename)
            ds.metadata['Import-Filter'] = unicode(importer.blurb)

            new_datasets.append(ds)

            n+=1
            yield (None,n/N)

        yield (-1,None)

        if len(new_datasets) > 0:
            ul = UndoList().describe("Import Dataset(s)")
            project.add_datasets(new_datasets, undolist=ul)
            undolist.append(ul)
    N = len(filenames)
    app.progress(0.0)        
    for filename in filenames:
        app.status_msg("Importing %s" % filename)                       
        try:
            importer = template.new_instance()                
            ds = importer.read_dataset_from_file(filename)
        except dataio.ImportError, msg:
            app.error_msg(msg)
            continue
        except error.UserCancel:
            app.error_msg("Import aborted by user")
            continue

        root, ext = os.path.splitext(os.path.basename(filename))
        ds.key = utils.encode_as_key(root)

        new_datasets.append(ds)
        n+=1
        app.progress(n/N)

    app.progress(1.0)

    if len(new_datasets) > 0:
        ul = UndoList()
        if len(new_datasets) == 1:
            ul.describe("Import Dataset")
        else:
            ul.describe("Import %d Datasets" % len(new_datasets) )

        project.add_datasets(new_datasets, undolist=ul)