Exemple #1
0
    def __init__(self, file_name=None, mode='exec', packages=None, width=None, height=None):
        """ Constructor
            
            AST browser windows that displays the Abstract Syntax Tree
            of source_text code.
            
            The source_text can be given as text in the source_text parameter, or
            can be read from a file. The file_name parameter overrides
            the source_text parameter.
            
            The mode argument specifies what kind of code must be compiled; 
            it can be 'exec' if source_text consists of a sequence of statements,
            'eval' if it consists of a single expression, or 'single' if it 
            consists of a single interactive statement (in the latter case, 
            expression statements that evaluate to something other than None 
            will be printed).
            (see http://docs.python.org/2/library/functions.html#compile)
        """
        super(AstToolBox, self).__init__()

        valid_modes = ['exec', 'eval', 'single']
        if mode not in valid_modes:
            raise ValueError("Mode must be one of: {}".format(valid_modes))
        self._mode = 'exec'

        self.auto_expand_ast = True

        self.start_packages = packages if packages else []
        self.start_packages += [
            'ctree.transformations',
            'ctree.c.codegen',
            'ctree.ocl.codegen',
            'ctree.omp.codegen',
        ]

        self.settings = None

        # Set up the UI
        self.tree_transform_controller = TreeTransformController()

        # there is a little trick here so both presenters know about their partner
        self.code_presenter = CodePresenter(
            tree_transform_controller=self.tree_transform_controller
        )
        self.transform_presenter = TransformPresenter(
            tree_transform_controller=self.tree_transform_controller,
            start_packages=self.start_packages,
        )
        self.code_presenter.set_transform_presenter(self.transform_presenter)
        self.transform_presenter.set_code_presenter(self.code_presenter)

        self.code_presenter.new_item_from_file(file_name)
        self.transform_presenter.load_files(self.start_packages)

        central_splitter = QtGui.QSplitter(self, orientation=QtCore.Qt.Horizontal)
        self.setCentralWidget(central_splitter)
        central_layout = QtGui.QHBoxLayout()
        central_splitter.setLayout(central_layout)

        # Create base tree widget
        central_layout.addWidget(self.code_presenter.code_pane)

        central_layout.addWidget(self.transform_presenter.transform_pane)

        # Splitter parameters
        central_splitter.setCollapsible(0, True)
        central_splitter.setCollapsible(1, True)
        central_splitter.setSizes([AstToolBox.default_left_frame_size, 250])
        central_splitter.setStretchFactor(0, 0.5)
        central_splitter.setStretchFactor(1, 0.5)

        self._setup_menu()
        self.setWindowTitle('{}'.format(PROGRAM_NAME))

        self.read_settings()
Exemple #2
0
class AstToolBox(QtGui.QMainWindow):
    """
    The main application.
    """

    default_left_frame_size = 900

    def __init__(self, file_name=None, mode='exec', packages=None, width=None, height=None):
        """ Constructor
            
            AST browser windows that displays the Abstract Syntax Tree
            of source_text code.
            
            The source_text can be given as text in the source_text parameter, or
            can be read from a file. The file_name parameter overrides
            the source_text parameter.
            
            The mode argument specifies what kind of code must be compiled; 
            it can be 'exec' if source_text consists of a sequence of statements,
            'eval' if it consists of a single expression, or 'single' if it 
            consists of a single interactive statement (in the latter case, 
            expression statements that evaluate to something other than None 
            will be printed).
            (see http://docs.python.org/2/library/functions.html#compile)
        """
        super(AstToolBox, self).__init__()

        valid_modes = ['exec', 'eval', 'single']
        if mode not in valid_modes:
            raise ValueError("Mode must be one of: {}".format(valid_modes))
        self._mode = 'exec'

        self.auto_expand_ast = True

        self.start_packages = packages if packages else []
        self.start_packages += [
            'ctree.transformations',
            'ctree.c.codegen',
            'ctree.ocl.codegen',
            'ctree.omp.codegen',
        ]

        self.settings = None

        # Set up the UI
        self.tree_transform_controller = TreeTransformController()

        # there is a little trick here so both presenters know about their partner
        self.code_presenter = CodePresenter(
            tree_transform_controller=self.tree_transform_controller
        )
        self.transform_presenter = TransformPresenter(
            tree_transform_controller=self.tree_transform_controller,
            start_packages=self.start_packages,
        )
        self.code_presenter.set_transform_presenter(self.transform_presenter)
        self.transform_presenter.set_code_presenter(self.code_presenter)

        self.code_presenter.new_item_from_file(file_name)
        self.transform_presenter.load_files(self.start_packages)

        central_splitter = QtGui.QSplitter(self, orientation=QtCore.Qt.Horizontal)
        self.setCentralWidget(central_splitter)
        central_layout = QtGui.QHBoxLayout()
        central_splitter.setLayout(central_layout)

        # Create base tree widget
        central_layout.addWidget(self.code_presenter.code_pane)

        central_layout.addWidget(self.transform_presenter.transform_pane)

        # Splitter parameters
        central_splitter.setCollapsible(0, True)
        central_splitter.setCollapsible(1, True)
        central_splitter.setSizes([AstToolBox.default_left_frame_size, 250])
        central_splitter.setStretchFactor(0, 0.5)
        central_splitter.setStretchFactor(1, 0.5)

        self._setup_menu()
        self.setWindowTitle('{}'.format(PROGRAM_NAME))

        self.read_settings()

        #
        # TODO: figure out where and how to set the focus of this thing as it starts up

    def write_settings(self):
        self.settings = QtCore.QSettings()
        self.settings.beginGroup("MainWindow")
        self.settings.setValue("size", self.size())
        self.settings.setValue("pos", self.pos())
        self.settings.endGroup()

    def read_settings(self):
        self.settings = QtCore.QSettings()
        self.settings.beginGroup("MainWindow")
        self.resize(self.settings.value("size", QtCore.QSize(1100, 900)))
        self.move(self.settings.value("pos", QtCore.QPoint(200, 200)))
        self.settings.endGroup()

    def _setup_menu(self):
        """ Sets up the main menu.
        """
        file_menu = self.menuBar().addMenu("&File")
        file_menu.addAction("&Open...", self.open_file, "Ctrl+O")
        #file_menu.addAction("C&lose", self.close_window, "Ctrl+W")
        file_menu.addAction("E&xit", AstToolBox.quit_application, "Ctrl+Q")

        view_menu = self.menuBar().addMenu("&View")

        self.auto_expand_ast = QtGui.QAction("Expand AST trees on create", self, checkable=True, checked=True)
        assert self.auto_expand_ast.toggled.connect(self.set_auto_expand)

        self.menuBar().addSeparator()
        help_menu = self.menuBar().addMenu("&Help")
        help_menu.addAction('&About', self.about)

    def set_auto_expand(self):
        self.tree_transform_controller.ast_tree_manager.set_auto_expand(self.auto_expand_ast.isChecked())

    def new_file(self):
        """ Clears the widgets """
        self.ast_tree.make_tree_from()

    def open_file(self, file_name=None):
        """ Opens a new file. Show the open file dialog if file_name is None.
        """
        if not file_name:
            file_name, _ = QtGui.QFileDialog.getOpenFileName(self, "Open Python Source", "", "Python Files (*.py)")
            print (file_name)
        self.code_presenter.new_file(file_name)

    def about(self):
        """ Shows the about message window. """
        QtGui.QMessageBox.about(self, "About %s" % PROGRAM_NAME, ABOUT_MESSAGE)

    def close_window(self):
        """ Closes the window """
        self.close()

    def closeEvent(self, event):
        self.write_settings()
        event.accept()

    @staticmethod
    def quit_application():
        """ Closes all windows """
        app = QtGui.QApplication.instance()
        app.closeAllWindows()