ENH: Clean up temp file from test suite
This commit is contained in:
parent
a6d5e28642
commit
0e4fd90ca2
0
pyqtgraph/exporters/tests/__init__.py
Normal file
0
pyqtgraph/exporters/tests/__init__.py
Normal file
@ -1,16 +1,22 @@
|
|||||||
"""
|
"""
|
||||||
SVG export test
|
SVG export test
|
||||||
"""
|
"""
|
||||||
|
from __future__ import (division, print_function, absolute_import)
|
||||||
import pyqtgraph as pg
|
import pyqtgraph as pg
|
||||||
import pyqtgraph.exporters
|
import pyqtgraph.exporters
|
||||||
import csv
|
import csv
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
from . import utils
|
||||||
|
|
||||||
app = pg.mkQApp()
|
app = pg.mkQApp()
|
||||||
|
|
||||||
def approxeq(a, b):
|
def approxeq(a, b):
|
||||||
return (a-b) <= ((a + b) * 1e-6)
|
return (a-b) <= ((a + b) * 1e-6)
|
||||||
|
|
||||||
|
|
||||||
def test_CSVExporter():
|
def test_CSVExporter():
|
||||||
|
tempfile = utils.gentempfilename(suffix='.csv')
|
||||||
plt = pg.plot()
|
plt = pg.plot()
|
||||||
y1 = [1,3,2,3,1,6,9,8,4,2]
|
y1 = [1,3,2,3,1,6,9,8,4,2]
|
||||||
plt.plot(y=y1, name='myPlot')
|
plt.plot(y=y1, name='myPlot')
|
||||||
@ -24,9 +30,9 @@ def test_CSVExporter():
|
|||||||
plt.plot(x=x3, y=y3, stepMode=True)
|
plt.plot(x=x3, y=y3, stepMode=True)
|
||||||
|
|
||||||
ex = pg.exporters.CSVExporter(plt.plotItem)
|
ex = pg.exporters.CSVExporter(plt.plotItem)
|
||||||
ex.export(fileName='test.csv')
|
ex.export(fileName=tempfile)
|
||||||
|
|
||||||
r = csv.reader(open('test.csv', 'r'))
|
r = csv.reader(open(tempfile, 'r'))
|
||||||
lines = [line for line in r]
|
lines = [line for line in r]
|
||||||
header = lines.pop(0)
|
header = lines.pop(0)
|
||||||
assert header == ['myPlot_x', 'myPlot_y', 'x0001', 'y0001', 'x0002', 'y0002']
|
assert header == ['myPlot_x', 'myPlot_y', 'x0001', 'y0001', 'x0002', 'y0002']
|
||||||
@ -44,6 +50,7 @@ def test_CSVExporter():
|
|||||||
assert (i >= len(y3) and vals[5] == '') or approxeq(float(vals[5]), y3[i])
|
assert (i >= len(y3) and vals[5] == '') or approxeq(float(vals[5]), y3[i])
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
|
os.unlink(tempfile)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
test_CSVExporter()
|
test_CSVExporter()
|
||||||
|
|
@ -1,11 +1,19 @@
|
|||||||
"""
|
"""
|
||||||
SVG export test
|
SVG export test
|
||||||
"""
|
"""
|
||||||
|
from __future__ import (division, print_function, absolute_import)
|
||||||
import pyqtgraph as pg
|
import pyqtgraph as pg
|
||||||
import pyqtgraph.exporters
|
import pyqtgraph.exporters
|
||||||
|
import tempfile
|
||||||
|
from . import utils
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
app = pg.mkQApp()
|
app = pg.mkQApp()
|
||||||
|
|
||||||
|
|
||||||
def test_plotscene():
|
def test_plotscene():
|
||||||
|
tempfile = utils.gentempfilename(suffix='.svg')
|
||||||
pg.setConfigOption('foreground', (0,0,0))
|
pg.setConfigOption('foreground', (0,0,0))
|
||||||
w = pg.GraphicsWindow()
|
w = pg.GraphicsWindow()
|
||||||
w.show()
|
w.show()
|
||||||
@ -18,10 +26,12 @@ def test_plotscene():
|
|||||||
app.processEvents()
|
app.processEvents()
|
||||||
|
|
||||||
ex = pg.exporters.SVGExporter(w.scene())
|
ex = pg.exporters.SVGExporter(w.scene())
|
||||||
ex.export(fileName='test.svg')
|
ex.export(fileName=tempfile)
|
||||||
|
# clean up after the test is done
|
||||||
|
os.unlink(tempfile)
|
||||||
|
|
||||||
def test_simple():
|
def test_simple():
|
||||||
|
tempfile = utils.gentempfilename(suffix='.svg')
|
||||||
scene = pg.QtGui.QGraphicsScene()
|
scene = pg.QtGui.QGraphicsScene()
|
||||||
#rect = pg.QtGui.QGraphicsRectItem(0, 0, 100, 100)
|
#rect = pg.QtGui.QGraphicsRectItem(0, 0, 100, 100)
|
||||||
#scene.addItem(rect)
|
#scene.addItem(rect)
|
||||||
@ -51,6 +61,7 @@ def test_simple():
|
|||||||
#el = pg.QtGui.QGraphicsEllipseItem(0, 0, 100, 50)
|
#el = pg.QtGui.QGraphicsEllipseItem(0, 0, 100, 50)
|
||||||
#el.translate(10,-5)
|
#el.translate(10,-5)
|
||||||
#el.scale(0.5,2)
|
#el.scale(0.5,2)
|
||||||
|
|
||||||
#el.setParentItem(rect2)
|
#el.setParentItem(rect2)
|
||||||
|
|
||||||
grp2 = pg.ItemGroup()
|
grp2 = pg.ItemGroup()
|
||||||
@ -62,6 +73,5 @@ def test_simple():
|
|||||||
grp2.addItem(rect3)
|
grp2.addItem(rect3)
|
||||||
|
|
||||||
ex = pg.exporters.SVGExporter(scene)
|
ex = pg.exporters.SVGExporter(scene)
|
||||||
ex.export(fileName='test.svg')
|
ex.export(fileName=tempfile)
|
||||||
|
os.unlink(tempfile)
|
||||||
|
|
||||||
|
47
pyqtgraph/exporters/tests/utils.py
Normal file
47
pyqtgraph/exporters/tests/utils.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import tempfile
|
||||||
|
import uuid
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def gentempfilename(dir=None, suffix=None):
|
||||||
|
"""Generate a temporary file with a random name
|
||||||
|
|
||||||
|
Defaults to whatever the system thinks is a temporary location
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
suffix : str, optional
|
||||||
|
The suffix of the file name (The thing after the last dot).
|
||||||
|
If 'suffix' does not begin with a dot then one will be prepended
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
str
|
||||||
|
The filename of a unique file in the temporary directory
|
||||||
|
"""
|
||||||
|
if dir is None:
|
||||||
|
dir = tempfile.gettempdir()
|
||||||
|
if suffix is None:
|
||||||
|
suffix = ''
|
||||||
|
elif not suffix.startswith('.'):
|
||||||
|
suffix = '.' + suffix
|
||||||
|
print('tempfile.tempdir = %s' % tempfile.tempdir)
|
||||||
|
print('suffix = %s' % suffix)
|
||||||
|
return os.path.join(dir, str(uuid.uuid4()) + suffix)
|
||||||
|
|
||||||
|
|
||||||
|
def gentempdir(dir=None):
|
||||||
|
"""Generate a temporary directory
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
dir : str, optional
|
||||||
|
The directory to create a temporary directory im. If None, defaults
|
||||||
|
to the place on disk that the system thinks is a temporary location
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
str
|
||||||
|
The path to the temporary directory
|
||||||
|
"""
|
||||||
|
return tempfile.mkdtemp(dir=dir)
|
Loading…
x
Reference in New Issue
Block a user