/usr/lib/python3/dist-packages/bimdp/hinet/bihtmlvisitor.py is in python3-mdp 3.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """
BiNet version of the htmlvisitor hinet module to convert a flow into HTML.
"""
from builtins import str
import tempfile
import os
import webbrowser
import mdp
from bimdp import BiNode
from bimdp.nodes import SenderBiNode
from bimdp.hinet import CloneBiLayer
class BiHiNetHTMLVisitor(mdp.hinet.HiNetHTMLVisitor):
"""Special version of HiNetHTMLVisitor with BiNode support.
All bimdp attributes are highligthed via the span.bicolor css tag.
"""
_BIHINET_STYLE = """
span.bicolor {
color: #6633FC;
}
"""
@classmethod
def hinet_css(cls):
"""Return the standard CSS string.
The CSS should be embedded in the final HTML file.
"""
css = super(BiHiNetHTMLVisitor, cls).hinet_css()
return css + cls._BIHINET_STYLE
def _translate_clonelayer(self, clonelayer):
"""This specialized version checks for CloneBiLayer."""
f = self._file
self._open_node_env(clonelayer, "layer")
f.write('<tr><td class="nodename">')
f.write(str(clonelayer) + '<br><br>')
f.write('%d repetitions' % len(clonelayer))
if isinstance(clonelayer, CloneBiLayer):
f.write('<br><br>')
f.write('<span class="bicolor" style="font-size: x-small">')
f.write('use copies: %s</span>' % str(clonelayer.use_copies))
f.write('</td>')
f.write('<td>')
self._visit_node(clonelayer.nodes[0])
f.write('</td></tr>')
self._close_node_env(clonelayer)
def _write_node_header(self, node, type_id="node"):
"""Write the header content for the node into the HTML file."""
f = self._file
if type_id == "flow":
pass
elif type_id == "flownode":
if isinstance(node, BiNode):
f.write('<tr><td class="dim">')
f.write('<span class="bicolor">id: %s</span>' % node._node_id)
f.write('</td></tr>')
else:
f.write('<tr><td class="dim">in-dim: %s' % str(node.input_dim))
if isinstance(node, BiNode):
f.write(' <span class="bicolor">id: %s</span>'
% node._node_id)
f.write('</td></tr>')
f.write('<tr><td>')
f.write('<table class="nodestruct">')
@mdp.extension_method("html", SenderBiNode)
def _html_representation(self):
return ('<span class="bicolor">recipient id: %s </span><br>' %
str(self._recipient_id))
#@mdp.extension_method("html_representation", BiNode)
#def _html_representation(self):
# html_repr = super(BiNode, self).html_representation()
# if self._stop_result:
# html_repr = [html_repr,
# '<span class="bicolor">stop_msg: %s </span><br>'
# % str(self._stop_result)]
# return html_repr
## Helper functions ##
def show_biflow(flow, filename=None, title="MDP flow display",
show_size=False, browser_open=True):
"""Write a flow with BiMDP nodes into a HTML file, open it in the browser
and return the file name. Compared the the non-bi function this provides
special decoration for BiNode attributes.
flow -- The flow to be shown.
filename -- Filename for the HTML file to be created. If None
a temporary file is created.
title -- Title for the HTML file.
show_size -- Show the approximate memory footprint of all nodes.
browser_open -- If True (default value) then the slideshow file is
automatically opened in a webbrowser.
"""
if filename is None:
(fd, filename) = tempfile.mkstemp(suffix=".html", prefix="MDP_")
html_file = os.fdopen(fd, 'w')
else:
html_file = open(filename, 'w')
html_file.write('<html>\n<head>\n<title>%s</title>\n' % title)
html_file.write('<style type="text/css" media="screen">')
html_file.write(mdp.utils.basic_css())
html_file.write(BiHiNetHTMLVisitor.hinet_css())
html_file.write('</style>\n</head>\n<body>\n')
html_file.write('<h3>%s</h3>\n' % title)
explanation = '(data flows from top to bottom)'
html_file.write('<par class="explanation">%s</par>\n' % explanation)
html_file.write('<br><br><br>\n')
converter = BiHiNetHTMLVisitor(html_file, show_size=show_size)
converter.convert_flow(flow=flow)
html_file.write('</body>\n</html>')
html_file.close()
if browser_open:
webbrowser.open(os.path.abspath(filename))
return filename
|