forked from sandialabs/toyplot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
138 lines (104 loc) · 4.4 KB
/
backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Copyright 2014, Sandia Corporation. Under the terms of Contract
# DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains certain
# rights in this software.
from behave import *
import importlib
import io
import numpy
import os
import xml.etree.ElementTree as xml
import test
import testing
if not os.path.exists(testing.backend_dir):
os.makedirs(testing.backend_dir)
@given(u'that the {} backend is available')
def step_impl(context, name):
try:
context.name = name
context.backend = importlib.import_module(name)
except:
context.scenario.skip(reason="The %s backend is not available." % name)
@then(u'the canvas can be rendered to an html file')
def step_impl(context):
target = os.path.join(testing.backend_dir, "%s.html" % context.name)
context.backend.render(context.canvas, target)
#html = xml.parse(target)
#test.assert_equal(html.getroot().tag, "{http://www.w3.org/2000/svg}svg")
@then(u'the canvas can be rendered to an html buffer')
def step_impl(context):
buffer = io.BytesIO()
context.backend.render(context.canvas, buffer)
#html = xml.parse(buffer.getvalue())
#test.assert_equal(html.getroot().tag, "html")
@then(u'the canvas can be rendered to a returned html dom')
def step_impl(context):
html = context.backend.render(context.canvas)
test.assert_is_instance(html, xml.Element)
test.assert_equal(html.tag, "div")
@then(u'the canvas can be rendered to a pdf file')
def step_impl(context):
target = os.path.join(testing.backend_dir, "%s.pdf" % context.name)
context.backend.render(context.canvas, target)
@then(u'the canvas can be rendered to a pdf buffer')
def step_impl(context):
buffer = io.BytesIO()
context.backend.render(context.canvas, buffer)
@then(u'the canvas can be rendered to a returned pdf document')
def step_impl(context):
pdf = context.backend.render(context.canvas)
@then(u'the canvas can be rendered to a png file')
def step_impl(context):
target = os.path.join(testing.backend_dir, "%s.png" % context.name)
context.backend.render(context.canvas, target)
image = testing.read_png(target)
test.assert_equal(image.shape, (600, 600, 4))
test.assert_equal(image.dtype, "uint8")
@then(u'the canvas can be rendered to a png buffer')
def step_impl(context):
buffer = io.BytesIO()
context.backend.render(context.canvas, buffer)
buffer.seek(0)
image = testing.read_png(buffer)
test.assert_equal(image.shape, (600, 600, 4))
test.assert_equal(image.dtype, "uint8")
@then(u'the canvas can be rendered to a returned png document')
def step_impl(context):
png = context.backend.render(context.canvas)
image = testing.read_png(io.BytesIO(png))
test.assert_equal(image.shape, (600, 600, 4))
test.assert_equal(image.dtype, "uint8")
@then(u'the canvas can be rendered to a 200 pixel wide png document')
def step_impl(context):
png = context.backend.render(context.canvas, width="200px")
image = testing.read_png(io.BytesIO(png))
test.assert_equal(image.shape, (200, 200, 4))
test.assert_equal(image.dtype, "uint8")
@then(u'the canvas can be rendered to a 150 pixel high png document')
def step_impl(context):
png = context.backend.render(context.canvas, height="150px")
image = testing.read_png(io.BytesIO(png))
test.assert_equal(image.shape, (150, 150, 4))
test.assert_equal(image.dtype, "uint8")
@then(u'the canvas can be rendered to a half scale png document')
def step_impl(context):
png = context.backend.render(context.canvas, scale=0.5)
image = testing.read_png(io.BytesIO(png))
test.assert_equal(image.shape, (300, 300, 4))
test.assert_equal(image.dtype, "uint8")
@then(u'the canvas can be rendered to an svg file')
def step_impl(context):
target = os.path.join(testing.backend_dir, "%s.svg" % context.name)
context.backend.render(context.canvas, target)
svg = xml.parse(target)
test.assert_equal(svg.getroot().tag, "{http://www.w3.org/2000/svg}svg")
@then(u'the canvas can be rendered to an svg buffer')
def step_impl(context):
buffer = io.BytesIO()
context.backend.render(context.canvas, buffer)
#svg = xml.parse(buffer.getvalue())
#test.assert_equal(svg.getroot().tag, "svg")
@then(u'the canvas can be rendered to a returned svg dom')
def step_impl(context):
svg = context.backend.render(context.canvas)
test.assert_is_instance(svg, xml.Element)
test.assert_equal(svg.tag, "svg")