Commit 983f4205 authored by Igor Dejanovic's avatar Igor Dejanovic

Fixing PEP8 violation in export.py

parent 297eea71
......@@ -10,52 +10,55 @@
import StringIO
from arpeggio import Terminal
class Export(object):
'''
Base class for all Exporters.
'''
def __init__(self):
super(Export, self).__init__()
# Export initialization
self._render_set = set() # Used in rendering to prevent rendering
self._render_set = set() # Used in rendering to prevent
# rendering
# of the same node multiple times
self._adapter_map = {} # Used as a registry of adapters to ensure
self._adapter_map = {} # Used as a registry of adapters to
# ensure
# ensure that the same adapter is
# returned for the same adaptee object
def export(self, obj):
'''Export of obj to a string.'''
self._outf = StringIO()
self._export(obj)
return self._outf.getvalue()
def exportFile(self, obj, file_name):
'''Export of obj to a file.'''
self._outf = open(file_name, "w")
self._export(obj)
self._outf.close()
def _export(self, obj):
self._outf.write(self._start())
self._render_node(obj)
self._outf.write(self._end())
def _start(self):
'''
Overide this to specify the begining of the graph representation.
Override this to specify the beginning of the graph representation.
'''
return ""
def _end(self):
'''
Overide this to specify the end of the graph representation.
Override this to specify the end of the graph representation.
'''
return ""
class ExportAdapter(object):
'''
Base adapter class for the export support.
......@@ -68,25 +71,26 @@ class ExportAdapter(object):
'''
self.adaptee = node # adaptee is adapted graph node
self.export = export
# -------------------------------------------------------------------------
# Support for DOT language
class DOTExportAdapter(ExportAdapter):
'''
Base adapter class for the DOT export support.
'''
'''
@property
def id(self):
'''Graph node unique identification.'''
raise NotImplementedError()
@property
def desc(self):
'''Graph node textual description.'''
raise NotImplementedError()
@property
def children(self):
'''Children of the graph node.'''
......@@ -96,35 +100,42 @@ class DOTExportAdapter(ExportAdapter):
class PMDOTExportAdapter(DOTExportAdapter):
'''
Adapter for ParsingExpression graph types (parser model).
'''
'''
@property
def id(self):
return id(self.adaptee)
@property
def desc(self):
return self.adaptee.desc
@property
def children(self):
if not hasattr(self, "_children"):
self._children = []
adapter_map = self.export._adapter_map # Registry of adapters used in this export
for c,n in enumerate(self.adaptee.nodes):
if isinstance(n, PMDOTExportAdapter): # if child node is already adapted use that adapter
self._children.append((str(c+1), n))
elif adapter_map.has_key(id(n)): # current node is adaptee -> there is registered adapter
self._children.append((str(c+1), adapter_map[id(n)]))
# Registry of adapters used in this export
adapter_map = self.export._adapter_map
for c, n in enumerate(self.adaptee.nodes):
if isinstance(n, PMDOTExportAdapter):
# if child node is already adapted use that adapter
self._children.append((str(c + 1), n))
elif id(n) in adapter_map:
# current node is adaptee -> there is registered adapter
self._children.append((str(c + 1), adapter_map[id(n)]))
else:
adapter = PMDOTExportAdapter(n, self.export)
self._children.append((str(c+1), adapter))
self._children.append((str(c + 1), adapter))
adapter_map[adapter.id] = adapter
return self._children
class PTDOTExportAdapter(PMDOTExportAdapter):
'''
Adapter for ParseTreeNode graph types.
'''
'''
@property
def children(self):
if isinstance(self.adaptee, Terminal):
......@@ -132,11 +143,12 @@ class PTDOTExportAdapter(PMDOTExportAdapter):
else:
if not hasattr(self, "_children"):
self._children = []
for c,n in enumerate(self.adaptee.nodes):
for c, n in enumerate(self.adaptee.nodes):
adapter = PTDOTExportAdapter(n, self.export)
self._children.append((str(c+1), adapter))
self._children.append((str(c + 1), adapter))
return self._children
class DOTExport(Export):
'''
Export to DOT language (part of GraphViz, see http://www.graphviz.org/)
......@@ -144,22 +156,25 @@ class DOTExport(Export):
def _render_node(self, node):
if not node in self._render_set:
self._render_set.add(node)
self._outf.write('\n%s [label="%s"];' % (node.id, self._dot_label_esc(node.desc)))
self._outf.write('\n%s [label="%s"];' %
(node.id, self._dot_label_esc(node.desc)))
#TODO Comment handling
# if hasattr(node, "comments") and root.comments:
# retval += self.node(root.comments)
# retval += '\n%s->%s [label="comment"]' % (id(root), id(root.comments))
# retval += '\n%s->%s [label="comment"]' % \
#(id(root), id(root.comments))
for name, n in node.children:
self._outf.write('\n%s->%s [label="%s"]' % (node.id, n.id, name))
self._outf.write('\n%s->%s [label="%s"]' %
(node.id, n.id, name))
self._outf.write('\n')
self._render_node(n)
self._render_node(n)
def _start(self):
return "digraph arpeggio_graph {"
def _end(self):
return "\n}"
def _dot_label_esc(self, to_esc):
to_esc = to_esc.replace("\\", "\\\\")
to_esc = to_esc.replace('\"', '\\"')
......@@ -170,7 +185,7 @@ class DOTExport(Export):
class PMDOTExport(DOTExport):
'''
Convenience DOTExport extension that uses ParserExpressionDOTExportAdapter
'''
'''
def export(self, obj):
return super(PMDOTExport, self).\
export(PMDOTExportAdapter(obj, self))
......@@ -183,7 +198,7 @@ class PMDOTExport(DOTExport):
class PTDOTExport(DOTExport):
'''
Convenience DOTExport extension that uses PTDOTExportAdapter
'''
'''
def export(self, obj):
return super(PTDOTExport, self).\
export(PTDOTExportAdapter(obj, self))
......@@ -191,4 +206,3 @@ class PTDOTExport(DOTExport):
def exportFile(self, obj, file_name):
return super(PTDOTExport, self).\
exportFile(PTDOTExportAdapter(obj, self), file_name)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment