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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
# Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 of the
# License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
import cairo
import cairo_utils
from cairo_utils import Context, ImageSurface, Pattern
from cairo_utils import VBoxNode, TextRectangle, intersect_lines, draw_arrow_head
class TreeNode(VBoxNode):
def __init__(self, parent, name, title, data):
VBoxNode.__init__(self)
self.padding = (0, 0, 0, 0)
self.expand_to_fill = False
self.name = name
self.parent = parent
self.data = data
self.children = []
self.is_step_list = False
if title is not None:
self.content = TextRectangle(title)
self.content.set_fill_color(0.83137254901960789, 0.92941176470588238, 0.99215686274509807, 1)
self.items.append(self.content)
self.extra_bottom_space = 0
def __repr__(self):
return "<node %s>"%self.content.text
def process(self):
for ch in self.children:
ch.process()
self.calc()
def render(self, c):
VBoxNode.render(self, c)
self.render_shadow(c)
def minimal_space_required_space_for_ref_to(self, c, ch):
return 0
def layout(self, c, x, y):
self.calc(c)
self.pos = (x, y)
class TreeStepNode(TreeNode):
def __init__(self, parent, name, title, data):
TreeNode.__init__(self, parent, name, title, data)
self.set_fill_color(0.5, 0.7, 0.5, 1)
self.set_color(1, 1, 1, 1)
class StepListNode(TreeNode):
def __init__(self, parent, steps):
TreeNode.__init__(self, parent, "steps", None, steps)
def layout(self, c, x, y):
self.calc(c)
for ch in self.children:
ch.layout(c, x, y)
y += ch.size[1] + 40
def render(self, c):
for ch in self.children:
ch.render(c)
def process(self):
for step in self.data:
name, value = step.items()[0]
print "#"*20,name, value
node = TreeStepNode(self, name, name, value)
self.children.append(node)
class TreeRootNode(StepListNode):
def __init__(self, steps):
StepListNode.__init__(self, None, steps["steps"])
class TableDependency(TreeNode):
def __init__(self, parent, data):
title = "%s.%s" % (data["database"], data["table"])
TreeNode.__init__(self, parent, "TableDependency", title, data)
# Layouter
class TreeLayouter:
def __init__(self, root):
self.root = root
self.yspacing = 30
self.xspacing = 50
def layout(self, ctx, node, x = 0, y = 0):
t, l, b, r = node.padding
w, h = node.size
twidth = 0
theight = 0
# calc total height occupied by children
if node.is_step_list:
for step in node.children:
step.pos = x, y
if node.name == "join_optimization":
print "put ", step.name, x + self.xspacing
if step.is_step_list:
sw, sh = self.layout(ctx, step, x, y)
else:
sw, sh = self.layout_tree(ctx, step, x + step.size[0] + self.xspacing, y)
step.gsize = (sw, sh)
y += max(step.size[1], sh) + self.yspacing
theight += sh + self.yspacing
twidth = max(sw, twidth)
twidth = max(w, twidth)
else:
twidth, theight = self.layout_tree(ctx, node, x, y)
theight += h
return twidth, theight
def layout_tree(self, ctx, node, x, y):
w, h = node.size
twidth = 0
theight = h
xx = x
for ch in node.children:
sw, sh = self.layout_tree(ctx, ch, xx, y + h + self.yspacing)
xx += sw + self.xspacing
twidth += sw
theight = max(sh, theight)
node.pos = x, y
return max(twidth, w), theight + self.yspacing
def adjust_child_layout(self, node, offset):
for ch in node.children:
cx, cy = ch.pos
ch.pos = int(cx + offset), int(cy)
self.adjust_child_layout(ch, offset)
def get_total_size(self, ctx):
def calc_all(c, node):
node.calc(c)
for ch in node.children:
calc_all(c, ch)
calc_all(ctx, self.root)
return self.layout(ctx, self.root, 0, 0)
def render(self, c, x, y):
def calc_all(c, node):
node.calc(c)
for ch in node.children:
calc_all(c, ch)
calc_all(c, self.root)
self.layout(c, self.root, x, y)
self.do_render(c, self.root)
self.do_render_lines(c, self.root)
def do_render_lines(self, c, node):
if node.is_step_list:
if node.children:
prev = node.children[0]
node.stroke_line_from_node(c, prev)
for ch in node.children[1:]:
prev.stroke_line_from_node(c, ch)
prev = ch
else:
for ch in node.children:
ch.stroke_line_from_node(c, node)
for ch in node.children:
self.do_render_lines(c, ch)
def do_render(self, c, node):
node.render(c)
for ch in node.children:
self.do_render(c, ch)
def decode_json(text):
return eval(text, {"false":False, "true":True})
def process_node(parent, name, data):
if type(data) is not dict or name in ["depends_on_map_bits"]:
return None
oper = name
node = TreeNode(parent, name, oper, data)
return node
def tree_from_json(parent, name, json):
node = process_node(parent, name, json)
if not node:
return None
for key, value in json.items():
if type(value) is dict:
ch = tree_from_json(node, key, value)
if ch:
node.children.append(ch)
elif type(value) is list:
if key == "steps":
previous = None
node.is_step_list = True
for item in value:
ch = tree_from_json(node, "step", item)
if ch.children: # remove useless intermediate step node
ch = ch.children[0]
node.children.append(ch)
ch.set_fill_color(0.8, 0.5, 0.5, 1)
ch.previous = previous
if previous:
previous.next = ch
previous = ch
if key == "table_dependencies":
interm = TreeNode(node, key, key, value)
node.children.append(interm)
for item in value:
ch = TableDependency(interm, item)
interm.children.append(ch)
else:
interm = TreeNode(node, key, key, {})
node.children.append(interm)
for item in value:
ch = tree_from_json(interm, key+" item", item)
if ch:
interm.children.append(ch)
return node
def render_json_data(json_text, background_image, png_file):
padding = 50
json = decode_json(json_text)
tree = TreeRootNode(json)
tree.process()
layout = TreeLayouter(tree)
img = ImageSurface(cairo.CAIRO_FORMAT_ARGB32, 10, 100)
ctx = Context(img)
w, h = 500, 500
img = ImageSurface(cairo.CAIRO_FORMAT_ARGB32, w+padding*2, h+padding*2)
ctx = Context(img)
ctx.set_font_size(12)
if background_image:
bgimage = ImageSurface.from_png(background_image)
else:
bgimage = None
if bgimage and bgimage.status() == cairo.CAIRO_STATUS_SUCCESS:
ctx.save()
pat = Pattern(bgimage)
pat.set_extend(cairo.CAIRO_EXTEND_REPEAT)
ctx.set_source(pat)
ctx.paint()
ctx.restore()
else:
ctx.set_source_rgb(1,1,1)
ctx.paint()
#layout.render(ctx, 0, 0)
tree.layout(ctx, 0, 0)
tree.render(ctx)
img.write_to_png(png_file)
return w+padding*2, h+padding*2
if __name__ == "__main__":
import sys
f = sys.argv[1]
render_json_data(open(f).read(), None, f+".png")
|