[Mumps2Py:] [136] implemented the concept of the 'current module', so that I can do stuff like extract the labels from a routine, extract the entryrefs from a routine.

[ Thread Index | Date Index | More lists.mumps2py.org/discuss Archives ]


Revision: 136
Author:   pgallot
Date:     2008-02-27 17:30:41 +0000 (Wed, 27 Feb 2008)

Log Message:
-----------
implemented the concept of the 'current module', so that I can do stuff like extract the labels from a routine, extract the entryrefs from a routine.
Also added a way to list the routines in the current file.

Modified Paths:
--------------
    trunk/mumps2py_ui.pyw


Modified: trunk/mumps2py_ui.pyw
===================================================================
--- trunk/mumps2py_ui.pyw	2008-02-27 17:22:59 UTC (rev 135)
+++ trunk/mumps2py_ui.pyw	2008-02-27 17:30:41 UTC (rev 136)
@@ -35,15 +35,16 @@
         self.root = my_root
         self.entry_val = None
         
-    def message(self, msg):
+    def message(self, msg, msg_title = "info"):
         """pop up a dialog showing a message"""
         popup = Toplevel(self.root)
+        popup.title(msg_title)
         popup.transient(self.root)
-        Label(popup, text = msg).pack(side = TOP, fill = BOTH, expand = YES)
+        Message(popup, text = msg).pack(side = TOP, fill = BOTH, expand = YES)
         btn = Button(popup, text = "OK", takefocus = YES)
         btn.pack(side = BOTTOM, padx = "0.5c", padx = "0.5c")
-        btn.bind('<Button-1>', lambda event, p = popup: (p.destroy()))
-        btn.bind('<KeyPress-Return>', lambda event, p = popup: (p.destroy())) 
+        btn.bind('<Button-1>', lambda event, p = popup: p.destroy())
+        btn.bind('<KeyPress-Return>', lambda event, p = popup: p.destroy()) 
         btn.focus_set()
         
     def popup_entry(self, msg, entry_width, callback):
@@ -67,9 +68,10 @@
                      lambda event, c = callback, p = popup: (c(p))) 
         entry_ctl.focus_set()
 
-    def info_window(self):
+    def info_window(self, msg_title = "Mumps2py"):
         """ generic popup for displaying a bunch of Text."""
         popup = Toplevel(self.root)
+        popup.title(msg_title)
         popup.transient(self.root)
         f_info = Frame(popup, borderwidth = 2)
         info_window = Text(f_info, width = 60, font = ("Consolas", 10),
@@ -88,7 +90,7 @@
         btn.bind('<Button-1>', lambda event, p = popup: (p.destroy()))
         btn.bind('<KeyPress-Return>', lambda event, p = popup: (p.destroy())) 
         return info_window
-    
+
 class Mumps2pyUI(GenericUI):
     """The class which provides the User Interface to Mumps2Py"""
     def __init__(self, my_root):
@@ -97,6 +99,7 @@
         self.modules = None
         self.before_window = None
         self.after_window = None
+        self.current_module = None
 
         self.query = { "from": \
                        ("Enter the approximate line # to parse from:", 7, \
@@ -132,12 +135,17 @@
 You should have received a copy of the GNU General Public License
 along with Mumps2Py.  If not, see <http://www.gnu.org/licenses/>.
 """
-        self.message(msg)
+        self.message(msg, "About Mumps2py")
         
-    def __display_before(self, filename, line_start = 1, line_end = -1):
+    def __display_before(self, a_module = None):
         """populate the before window with the Mumps module's raw source code"""
+        if a_module:
+            the_module = a_module
+        else:
+            the_module = self.current_module
+        line_start, line_end = the_module.start, the_module.end
         self.before_window.delete(0.0, END)
-        for line in fileinput.input(filename):
+        for line in fileinput.input(the_module.input_file):
             line_no = fileinput.lineno()
             if line_no < line_start:
                 continue
@@ -152,12 +160,65 @@
         for line in translation:
             self.after_window.insert(END, line)
 
-    def __display_decomposed(self, a_module):
+    def list_mods(self):
         """displays an intermediate decomposition of the Mumps code"""
-        decomposed_window = self.info_window()
-        for token in a_module.tokenized():
+        if not self.modules:
+            return
+
+        routinelist_window = self.info_window("routines in this file")
+        for routine in self.modules:
+            routinelist_window.insert(END, str(routine) + '\n')
+
+    def display_decomposed(self, a_module = None):
+        """displays an intermediate decomposition of the Mumps code"""
+        if a_module:
+            the_module = a_module
+        else:
+            the_module = self.current_module
+
+        title = the_module.mod_name + " decomposed"
+        decomposed_window = self.info_window(title)
+        for token in the_module.tokenized():
             decomposed_window.insert(END, str(token) + '\n')
 
+    def label_rpt(self, a_module = None):
+        """ extracts any labels in the given or current module"""
+        if a_module:
+            the_module = a_module
+        elif self.current_module:
+            the_module = self.current_module
+        else:
+            return
+
+        from mumps2py.tokens import LABEL
+        
+        title = the_module.mod_name + " labels"
+        rpt_window = self.info_window(title)
+        for token in the_module.tokenized():
+            if token.toktype == LABEL:
+                rpt_window.insert(END, str(token) + '\n')
+        
+    def entryref_rpt(self, a_module = None):
+        """ extracts any entryrefs in the given or current module"""
+        if a_module:
+            the_module = a_module
+        elif self.current_module:
+            the_module = self.current_module
+        else:
+            return
+
+        from mumps2py.tokens import ENTRYREF
+        
+        title = the_module.mod_name + " entryrefs"
+        rpt_window = self.info_window(title)
+        entryrefs = {}
+        for token in the_module.tokenized():
+            token.extract_subtokens(entryrefs, (ENTRYREF, ))
+
+        if entryrefs.get(ENTRYREF, None):
+            for ref in entryrefs[ENTRYREF]:
+                rpt_window.insert(END, str(ref) + '\n')
+
     def parse_fromlinemodule(self, popup):
         """parse from the module containing the line specified by popup"""
         start_line = int(self.entry_val.get())
@@ -169,7 +230,7 @@
                 try:
                     mumps2py.parseMumps(a_module)
                 except mumps2py.ParseError, err:
-                    self.message(err.error_msg())
+                    self.message(err.error_msg(), "Parse Error")
 
     def parsegivenmodule(self, popup):
         """parse the module specified by popup"""
@@ -178,16 +239,16 @@
         if self.modules:
             for the_module in self.modules:
                 if the_module.mod_name == mod_name:
-                    self.__display_before(the_module.input_file, \
-                                          the_module.start, the_module.end)
+                    self.current_module = the_module
+                    self.__display_before(the_module)
                     try:
                         mumps2py.parseMumps(the_module)
                         translation = mumps2py.translate(the_module)
                         self.__display_after(translation)
                     except mumps2py.ParseError, err:
-                        self.message(err.error_msg())
+                        self.message(err.error_msg(), "Parse Error")
                     except mumps2py.TranslationError, err:
-                        self.message(err.error_msg())
+                        self.message(err.error_msg(), "Translation Error")
 
     def parse_all(self):
         """parse all the modules in a Mumps file"""
@@ -196,24 +257,25 @@
                 try:
                     mumps2py.parseMumps(the_module)
                 except mumps2py.ParseError, err:
-                    self.message(err.error_msg())
+                    self.message(err.error_msg(), "Parse Error")
         elif len(self.modules) == 1:
-            the_module = self.modules[0]
+            the_module = self.current_module
             try:
                 mumps2py.parseMumps(the_module)
             except mumps2py.ParseError, err:
-                self.message(err.error_msg())
-            self.__display_decomposed(the_module)
+                self.message(err.error_msg(), "Parse Error")
+            self.display_decomposed(the_module)
             try:
                 translation = mumps2py.translate(the_module)
                 self.__display_after(translation)
             except mumps2py.TranslationError, err:
-                self.message(err.error_msg())
+                self.message(err.error_msg(), "Translation Error")
 
     def parseuntilmodule(self, popup):
         """analyze the tokens up until the module specified by popup"""
         mod_name = self.entry_val.get()
         popup.destroy()
+        self.current_module = None
         mod_list = []
         if self.modules:
             for the_module in self.modules:
@@ -228,6 +290,8 @@
         modcnt = 0
         if mod_list:
             modules_list = mod_list
+        elif self.current_module:
+            modules_list = [self.current_module, ]
         else:
             modules_list = self.modules
         if modules_list:
@@ -260,6 +324,7 @@
         self.before_window.delete(0.0, END)
         self.after_window.delete(0.0, END)
         self.modules = None
+        self.current_module = None
 
     def file_open(self):
         """ open a file of Mumps code"""
@@ -281,12 +346,15 @@
         print time.time() - tstart,"seconds to load %d routines" \
               % len(self.modules)
         if len(self.modules) == 1:
-            self.__display_before(mumps_file)
+            self.current_module = self.modules[0]
+            self.__display_before(self.current_module)
+        else:
+            self.current_module = None
                 
     def file_save(self):
         """ saves what is in the After window to a file."""
-        if self.modules and len(self.modules) == 1:
-            suggest_name = self.modules[0].output_file
+        if self.current_module:
+            suggest_name = self.current_module.output_file
 
             name = tkFileDialog.asksaveasfilename(parent = self.root,
                                                   initialfile = suggest_name)
@@ -298,7 +366,7 @@
         return name
 
     def file_m2p(self):
-        """ saves what is in the After window to a file."""
+        """ creates an m2p from the current list of modules."""
         if self.modules:
             base_fname = self.modules[0].input_file
             dlg_title = "confirm input file"
@@ -386,6 +454,10 @@
         parse_btn.pack(side = LEFT, padx = "2m")
         parse_btn.menu = Menu(parse_btn)
 
+        parse_btn.menu.add_command( label = "List routines", \
+                                    underline = 0, command = \
+                                    (lambda s = self: s.list_mods()))
+        parse_btn.menu.add('separator')
         parse_btn.menu.add_command( label = "Parse All", \
                                     underline = 6, command = \
                                     (lambda s = self: s.parse_all()))
@@ -408,9 +480,19 @@
                              takefocus = TRUE)
         run_btn.pack(side = LEFT, padx = "2m")
         run_btn.menu = Menu(run_btn)
+        run_btn.menu.add_command( label="routine decomposition", \
+                                  underline = 1, command = \
+                                  (lambda s = self: s.display_decomposed()))
+        run_btn.menu.add_command( label="label report", \
+                                  underline = 1, \
+                                  command = lambda s = self: s.label_rpt())
+        run_btn.menu.add_command( label="entryref report", \
+                                  underline = 1, command = \
+                                  lambda s = self: s.entryref_rpt())
+        run_btn.menu.add('separator')
         run_btn.menu.add_command( label="Save and run", \
                                   underline = 1, \
-                                  command = (lambda s = self: s.save_and_run()))
+                                  command = lambda s = self: s.save_and_run())
         run_btn.menu.add('separator')
         run_btn.menu.add_command( label="Regression test run", \
                                   underline = 1, command = \
@@ -422,11 +504,10 @@
         run_btn['menu'] = run_btn.menu
 
         help_btn = Menubutton(menu_bar, text = 'Help', underline = 0,
-                             takefocus = TRUE)
+                              takefocus = TRUE)
         help_btn.pack(side = LEFT, padx = "2m")
-        help_btn.menu = Menu(help_btn)
-        help_btn.menu.add_command( label="About", \
-                                  underline = 0, \
+        help_btn.menu = Menu(help_btn, tearoff = 0)
+        help_btn.menu.add_command( label="About", underline = 0, \
                                   command = (lambda s = self: s.help_about()))
         help_btn['menu'] = help_btn.menu
         


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/