Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
A
arpeggio-gm
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
backend
arpeggio-gm
Commits
983f4205
Commit
983f4205
authored
Dec 17, 2013
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixing PEP8 violation in export.py
parent
297eea71
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
42 deletions
+55
-42
export.py
arpeggio/export.py
+55
-42
No files found.
arpeggio/export.py
View file @
983f4205
...
@@ -10,52 +10,55 @@
...
@@ -10,52 +10,55 @@
import
StringIO
import
StringIO
from
arpeggio
import
Terminal
from
arpeggio
import
Terminal
class
Export
(
object
):
class
Export
(
object
):
'''
'''
Base class for all Exporters.
Base class for all Exporters.
'''
'''
def
__init__
(
self
):
def
__init__
(
self
):
super
(
Export
,
self
)
.
__init__
()
super
(
Export
,
self
)
.
__init__
()
# Export initialization
# 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
# 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
# ensure that the same adapter is
# returned for the same adaptee object
# returned for the same adaptee object
def
export
(
self
,
obj
):
def
export
(
self
,
obj
):
'''Export of obj to a string.'''
'''Export of obj to a string.'''
self
.
_outf
=
StringIO
()
self
.
_outf
=
StringIO
()
self
.
_export
(
obj
)
self
.
_export
(
obj
)
return
self
.
_outf
.
getvalue
()
return
self
.
_outf
.
getvalue
()
def
exportFile
(
self
,
obj
,
file_name
):
def
exportFile
(
self
,
obj
,
file_name
):
'''Export of obj to a file.'''
'''Export of obj to a file.'''
self
.
_outf
=
open
(
file_name
,
"w"
)
self
.
_outf
=
open
(
file_name
,
"w"
)
self
.
_export
(
obj
)
self
.
_export
(
obj
)
self
.
_outf
.
close
()
self
.
_outf
.
close
()
def
_export
(
self
,
obj
):
def
_export
(
self
,
obj
):
self
.
_outf
.
write
(
self
.
_start
())
self
.
_outf
.
write
(
self
.
_start
())
self
.
_render_node
(
obj
)
self
.
_render_node
(
obj
)
self
.
_outf
.
write
(
self
.
_end
())
self
.
_outf
.
write
(
self
.
_end
())
def
_start
(
self
):
def
_start
(
self
):
'''
'''
Over
ide this to specify the begi
ning of the graph representation.
Over
ride this to specify the begin
ning of the graph representation.
'''
'''
return
""
return
""
def
_end
(
self
):
def
_end
(
self
):
'''
'''
Overide this to specify the end of the graph representation.
Over
r
ide this to specify the end of the graph representation.
'''
'''
return
""
return
""
class
ExportAdapter
(
object
):
class
ExportAdapter
(
object
):
'''
'''
Base adapter class for the export support.
Base adapter class for the export support.
...
@@ -68,25 +71,26 @@ class ExportAdapter(object):
...
@@ -68,25 +71,26 @@ class ExportAdapter(object):
'''
'''
self
.
adaptee
=
node
# adaptee is adapted graph node
self
.
adaptee
=
node
# adaptee is adapted graph node
self
.
export
=
export
self
.
export
=
export
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# Support for DOT language
# Support for DOT language
class
DOTExportAdapter
(
ExportAdapter
):
class
DOTExportAdapter
(
ExportAdapter
):
'''
'''
Base adapter class for the DOT export support.
Base adapter class for the DOT export support.
'''
'''
@property
@property
def
id
(
self
):
def
id
(
self
):
'''Graph node unique identification.'''
'''Graph node unique identification.'''
raise
NotImplementedError
()
raise
NotImplementedError
()
@property
@property
def
desc
(
self
):
def
desc
(
self
):
'''Graph node textual description.'''
'''Graph node textual description.'''
raise
NotImplementedError
()
raise
NotImplementedError
()
@property
@property
def
children
(
self
):
def
children
(
self
):
'''Children of the graph node.'''
'''Children of the graph node.'''
...
@@ -96,35 +100,42 @@ class DOTExportAdapter(ExportAdapter):
...
@@ -96,35 +100,42 @@ class DOTExportAdapter(ExportAdapter):
class
PMDOTExportAdapter
(
DOTExportAdapter
):
class
PMDOTExportAdapter
(
DOTExportAdapter
):
'''
'''
Adapter for ParsingExpression graph types (parser model).
Adapter for ParsingExpression graph types (parser model).
'''
'''
@property
@property
def
id
(
self
):
def
id
(
self
):
return
id
(
self
.
adaptee
)
return
id
(
self
.
adaptee
)
@property
@property
def
desc
(
self
):
def
desc
(
self
):
return
self
.
adaptee
.
desc
return
self
.
adaptee
.
desc
@property
@property
def
children
(
self
):
def
children
(
self
):
if
not
hasattr
(
self
,
"_children"
):
if
not
hasattr
(
self
,
"_children"
):
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
):
# Registry of adapters used in this export
if
isinstance
(
n
,
PMDOTExportAdapter
):
# if child node is already adapted use that adapter
adapter_map
=
self
.
export
.
_adapter_map
self
.
_children
.
append
((
str
(
c
+
1
),
n
))
elif
adapter_map
.
has_key
(
id
(
n
)):
# current node is adaptee -> there is registered adapter
for
c
,
n
in
enumerate
(
self
.
adaptee
.
nodes
):
self
.
_children
.
append
((
str
(
c
+
1
),
adapter_map
[
id
(
n
)]))
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
:
else
:
adapter
=
PMDOTExportAdapter
(
n
,
self
.
export
)
adapter
=
PMDOTExportAdapter
(
n
,
self
.
export
)
self
.
_children
.
append
((
str
(
c
+
1
),
adapter
))
self
.
_children
.
append
((
str
(
c
+
1
),
adapter
))
adapter_map
[
adapter
.
id
]
=
adapter
adapter_map
[
adapter
.
id
]
=
adapter
return
self
.
_children
return
self
.
_children
class
PTDOTExportAdapter
(
PMDOTExportAdapter
):
class
PTDOTExportAdapter
(
PMDOTExportAdapter
):
'''
'''
Adapter for ParseTreeNode graph types.
Adapter for ParseTreeNode graph types.
'''
'''
@property
@property
def
children
(
self
):
def
children
(
self
):
if
isinstance
(
self
.
adaptee
,
Terminal
):
if
isinstance
(
self
.
adaptee
,
Terminal
):
...
@@ -132,11 +143,12 @@ class PTDOTExportAdapter(PMDOTExportAdapter):
...
@@ -132,11 +143,12 @@ class PTDOTExportAdapter(PMDOTExportAdapter):
else
:
else
:
if
not
hasattr
(
self
,
"_children"
):
if
not
hasattr
(
self
,
"_children"
):
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
)
adapter
=
PTDOTExportAdapter
(
n
,
self
.
export
)
self
.
_children
.
append
((
str
(
c
+
1
),
adapter
))
self
.
_children
.
append
((
str
(
c
+
1
),
adapter
))
return
self
.
_children
return
self
.
_children
class
DOTExport
(
Export
):
class
DOTExport
(
Export
):
'''
'''
Export to DOT language (part of GraphViz, see http://www.graphviz.org/)
Export to DOT language (part of GraphViz, see http://www.graphviz.org/)
...
@@ -144,22 +156,25 @@ class DOTExport(Export):
...
@@ -144,22 +156,25 @@ class DOTExport(Export):
def
_render_node
(
self
,
node
):
def
_render_node
(
self
,
node
):
if
not
node
in
self
.
_render_set
:
if
not
node
in
self
.
_render_set
:
self
.
_render_set
.
add
(
node
)
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
#TODO Comment handling
# if hasattr(node, "comments") and root.comments:
# if hasattr(node, "comments") and root.comments:
# retval += self.node(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
:
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
.
_outf
.
write
(
'
\n
'
)
self
.
_render_node
(
n
)
self
.
_render_node
(
n
)
def
_start
(
self
):
def
_start
(
self
):
return
"digraph arpeggio_graph {"
return
"digraph arpeggio_graph {"
def
_end
(
self
):
def
_end
(
self
):
return
"
\n
}"
return
"
\n
}"
def
_dot_label_esc
(
self
,
to_esc
):
def
_dot_label_esc
(
self
,
to_esc
):
to_esc
=
to_esc
.
replace
(
"
\\
"
,
"
\\\\
"
)
to_esc
=
to_esc
.
replace
(
"
\\
"
,
"
\\\\
"
)
to_esc
=
to_esc
.
replace
(
'
\"
'
,
'
\\
"'
)
to_esc
=
to_esc
.
replace
(
'
\"
'
,
'
\\
"'
)
...
@@ -170,7 +185,7 @@ class DOTExport(Export):
...
@@ -170,7 +185,7 @@ class DOTExport(Export):
class
PMDOTExport
(
DOTExport
):
class
PMDOTExport
(
DOTExport
):
'''
'''
Convenience DOTExport extension that uses ParserExpressionDOTExportAdapter
Convenience DOTExport extension that uses ParserExpressionDOTExportAdapter
'''
'''
def
export
(
self
,
obj
):
def
export
(
self
,
obj
):
return
super
(
PMDOTExport
,
self
)
.
\
return
super
(
PMDOTExport
,
self
)
.
\
export
(
PMDOTExportAdapter
(
obj
,
self
))
export
(
PMDOTExportAdapter
(
obj
,
self
))
...
@@ -183,7 +198,7 @@ class PMDOTExport(DOTExport):
...
@@ -183,7 +198,7 @@ class PMDOTExport(DOTExport):
class
PTDOTExport
(
DOTExport
):
class
PTDOTExport
(
DOTExport
):
'''
'''
Convenience DOTExport extension that uses PTDOTExportAdapter
Convenience DOTExport extension that uses PTDOTExportAdapter
'''
'''
def
export
(
self
,
obj
):
def
export
(
self
,
obj
):
return
super
(
PTDOTExport
,
self
)
.
\
return
super
(
PTDOTExport
,
self
)
.
\
export
(
PTDOTExportAdapter
(
obj
,
self
))
export
(
PTDOTExportAdapter
(
obj
,
self
))
...
@@ -191,4 +206,3 @@ class PTDOTExport(DOTExport):
...
@@ -191,4 +206,3 @@ class PTDOTExport(DOTExport):
def
exportFile
(
self
,
obj
,
file_name
):
def
exportFile
(
self
,
obj
,
file_name
):
return
super
(
PTDOTExport
,
self
)
.
\
return
super
(
PTDOTExport
,
self
)
.
\
exportFile
(
PTDOTExportAdapter
(
obj
,
self
),
file_name
)
exportFile
(
PTDOTExportAdapter
(
obj
,
self
),
file_name
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment