Skip to content

Commit

Permalink
Implemented mode parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
titusjan committed Aug 10, 2013
1 parent f9b27fe commit 7a7df76
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* The AstViewer class constructor accepts a source_code
parameter so source can be compared from a string.

* The AstViewer class constructor accepts a mode parameter

* Bug fix: highlighting of the last element now works.


13 changes: 10 additions & 3 deletions astviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def class_name(obj):
class AstViewer(QtGui.QMainWindow):
""" The main application.
"""
def __init__(self, source_code = '', file_name = ''):
def __init__(self, source_code = '', file_name = '', mode='exec'):
""" Constructor
AST browser windows that displays the Abstract Syntax Tree
Expand All @@ -87,9 +87,16 @@ def __init__(self, source_code = '', file_name = ''):
"""
super(AstViewer, self).__init__()

# Check values for mode
# (see http://docs.python.org/2/library/functions.html#compile)
valid_modes = ['exec', 'eval', 'single']
if mode not in valid_modes:
raise ValueError("Mode must be one of: {}".format(valid_modes))

# Models
self._file_name = '<source>'
self._source_code = source_code
self._mode = mode

# Views
self._setup_actions()
Expand Down Expand Up @@ -337,7 +344,7 @@ def add_node(ast_node, parent_item, field_label):

# End of helper function

syntax_tree = ast.parse(self._source_code, filename=self._file_name, mode='exec')
syntax_tree = ast.parse(self._source_code, filename=self._file_name, mode=self._mode)
#logger.debug(ast.dump(syntax_tree))
add_node(syntax_tree, self.ast_tree, '"{}"'.format(self._file_name))
self.ast_tree.expandToDepth(1)
Expand Down Expand Up @@ -438,5 +445,5 @@ def quit_application(self):
# pylint: enable=R0901, R0902, R0904, W0201

if __name__ == '__main__':
sys.exit(view(width=800, height=600, source_code = "a + 5 + 6 / 3.7"))
sys.exit(view(width=800, height=600, source_code = "a + 5 + 6 / 3.7", mode='eval'))

0 comments on commit 7a7df76

Please sign in to comment.