~jeremysanders/veusz/trunk

« back to all changes in this revision

Viewing changes to document/commandinterface.py

  • Committer: Jeremy Sanders
  • Date: 2009-06-20 17:57:45 UTC
  • Revision ID: git-v1:9eb967c7f5b151d0cc62e5de7df14fa3ac8cca0d
Save filename links in documents as relative links. Documents will try to load data from the same directory as the document and then the original save path (specified with AddImportPath in the saved file).

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
external programs.
27
27
"""
28
28
 
 
29
import os.path
 
30
 
29
31
import numpy as N
30
32
 
31
33
import veusz.qtall as qt4
40
42
    safe_commands = (
41
43
        'Action',
42
44
        'Add',
 
45
        'AddImportPath',
43
46
        'Get',
44
47
        'GetChildren',
45
48
        'GetData',
78
81
        self.document = document
79
82
        self.currentwidget = self.document.basewidget
80
83
        self.verbose = False
 
84
        self.importpath = []
81
85
 
82
86
        self.connect( self.document, qt4.SIGNAL("sigWiped"),
83
87
                      self.slotWipedDoc )
86
90
        """When the document is wiped, we change to the root widget."""
87
91
        self.To('/')
88
92
 
 
93
    def findFileOnImportPath(self, filename):
 
94
        """Find file on path, returning filename, or original if not found."""
 
95
        for path in self.importpath:
 
96
            fname = os.path.join(path, filename)
 
97
            try:
 
98
                # try to open file to make sure we have access to it and
 
99
                # it exists
 
100
                opened = open(fname)
 
101
                opened.close()
 
102
                return fname
 
103
            except IOError:
 
104
                pass
 
105
        return filename
 
106
 
89
107
    def SetVerbose(self, v=True):
90
108
        """Specify whether we want verbose output after operations."""
91
109
        self.verbose = v
101
119
 
102
120
        return w.name
103
121
 
 
122
    def AddImportPath(self, directory):
 
123
        """Add directory to import file path."""
 
124
        assert isinstance(directory, basestring)
 
125
        self.importpath.append(directory)
 
126
 
104
127
    def Remove(self, name):
105
128
        """Remove a graph from the dataset."""
106
129
        w = self.document.resolve(self.currentwidget, name)
323
346
        if type(datasetnames) in (str, unicode):
324
347
            datasetnames = [datasetnames]
325
348
 
326
 
        op = operations.OperationDataImport2D(datasetnames, datastr=string, xrange=xrange,
327
 
                                              yrange=yrange, invertrows=invertrows,
328
 
                                              invertcols=invertcols, transpose=transpose)
 
349
        op = operations.OperationDataImport2D(
 
350
            datasetnames, datastr=string, xrange=xrange,
 
351
            yrange=yrange, invertrows=invertrows,
 
352
            invertcols=invertcols, transpose=transpose)
329
353
        self.document.applyOperation(op)
330
354
        if self.verbose:
331
355
            print "Imported datasets %s" % (', '.join(datasetnames))
350
374
        if linked=True then the dataset is linked to the file
351
375
        """
352
376
 
 
377
        # look up filename on path
 
378
        realfilename = self.findFileOnImportPath(filename)
 
379
 
353
380
        if type(datasetnames) in (str, unicode):
354
381
            datasetnames = [datasetnames]
355
382
 
356
 
        op = operations.OperationDataImport2D(datasetnames, filename=filename, xrange=xrange,
357
 
                                              yrange=yrange, invertrows=invertrows,
358
 
                                              invertcols=invertcols, transpose=transpose,
359
 
                                              prefix=prefix, suffix=suffix,
360
 
                                              linked=linked)
 
383
        op = operations.OperationDataImport2D(
 
384
            datasetnames, filename=realfilename, xrange=xrange,
 
385
            yrange=yrange, invertrows=invertrows,
 
386
            invertcols=invertcols, transpose=transpose,
 
387
            prefix=prefix, suffix=suffix,
 
388
            linked=linked)
361
389
        self.document.applyOperation(op)
362
390
        if self.verbose:
363
391
            print "Imported datasets %s" % (', '.join(datasetnames))
382
410
         converting the data
383
411
        """
384
412
 
385
 
        op = operations.OperationDataImport(descriptor, filename=filename,
386
 
                                            useblocks=useblocks, linked=linked,
387
 
                                            prefix=prefix, suffix=suffix,
388
 
                                            ignoretext=ignoretext)
 
413
        realfilename = self.findFileOnImportPath(filename)
 
414
 
 
415
        op = operations.OperationDataImport(
 
416
            descriptor, filename=realfilename,
 
417
            useblocks=useblocks, linked=linked,
 
418
            prefix=prefix, suffix=suffix,
 
419
            ignoretext=ignoretext)
389
420
        dsnames = self.document.applyOperation(op)
390
421
        errors = op.simpleread.getInvalidConversions()
391
422
            
411
442
 
412
443
        If linked is True the data are linked with the file."""
413
444
 
 
445
        # backward compatibility
414
446
        if prefix:
415
447
            dsprefix = prefix + '_'
416
448
 
417
 
        op = operations.OperationDataImportCSV(filename, readrows=readrows,
418
 
                                               prefix=dsprefix, suffix=dssuffix,
419
 
                                               linked=linked)
 
449
        # lookup filename
 
450
        realfilename = self.findFileOnImportPath(filename)
 
451
 
 
452
        op = operations.OperationDataImportCSV(
 
453
            realfilename, readrows=readrows,
 
454
            prefix=dsprefix, suffix=dssuffix,
 
455
            linked=linked)
420
456
        dsnames = self.document.applyOperation(op)
421
457
            
422
458
        if self.verbose:
441
477
        linked specfies that the dataset is linked to the file
442
478
        """
443
479
 
444
 
        op = operations.OperationDataImportFITS(dsname, filename, hdu,
445
 
                                                datacol=datacol, symerrcol=symerrcol,
446
 
                                                poserrcol=poserrcol, negerrcol=negerrcol,
447
 
                                                linked=linked)
 
480
        # lookup filename
 
481
        realfilename = self.findFileOnImportPath(filename)
 
482
 
 
483
        op = operations.OperationDataImportFITS(
 
484
            dsname, realfilename, hdu,
 
485
            datacol=datacol, symerrcol=symerrcol,
 
486
            poserrcol=poserrcol, negerrcol=negerrcol,
 
487
            linked=linked)
448
488
        self.document.applyOperation(op)
449
489
 
450
490
    def ReloadData(self):