Changeset 850

Show
Ignore:
Timestamp:
09/16/05 18:01:14 (3 years ago)
Author:
tzanko
Message:

Rewrote conversion from xml to list of blocks. Fixed ticket #34

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • nbshell/trunk/ChangeLog

    r838 r850  
     12005-09-17  Tzanko Matev  <tsanko@gmail.com> 
     2 
     3    * Sheet.py (Sheet._update_celllist): 
     4    * PythonPlugin.py (PythonPluginFactory.get_matchers): 
     5    * FigurePlugin.py (FigurePluginFactory.get_matchers): 
     6    * textplugin.py (TextPluginFactory.get_matchers): 
     7    * PlainTextPlugin.py (PlainTextPluginFactory.get_matchers): 
     8    Rewrote the algorithm for converting a ElementTree structure to a 
     9    list of nbshell blocks. Now each plugin must give a method which 
     10    decides if a given element must be handled by that plugin. As a 
     11    result this part of the sheet is now independant of the specific 
     12    plugins (except for the raw xml plugin). Also adding complex tests 
     13    for parts of the xml tree is much easier than before. The text 
     14    plugin now matches only <para> elements with no subelements which 
     15    fixes ticket #34 
     16 
     17    * ipnNotebookWidget.py (CellCtrlBase.OnKeyDown): Fixed an 
     18    indentation bug where the given method was considered a function 
     19    in another method 
     20     
     21 
    1222005-09-13  Tzanko Matev  <tsanko@gmail.com> 
    223 
  • nbshell/trunk/nbshell/FigurePlugin.py

    r828 r850  
    4949            return None #Well here I should throw an exception, however I am  
    5050                        #not supposed to get to this line for a long long time 
    51                          
     51     
     52    def get_matchers(self): 
     53        """A matcher for the <ipython-figure> element""" 
     54        return lambda element:element.tag == 'ipython-figure' 
     55         
    5256 
    5357#end GenericPluginFactory 
  • nbshell/trunk/nbshell/Main.py

    r828 r850  
    5353     
    5454    plugin_dir = "." #this should be configured somewhere 
    55     plugin_dict = {0:0
     55    plugin_dict = {
    5656     
    5757    def __init__(self, *args, **kwds): 
  • nbshell/trunk/nbshell/PlainTextPlugin.py

    r829 r850  
    7070            return None #Well here I should throw an exception, however I am  
    7171                        #not supposed to get to this line for a long long time 
     72     
     73    def get_matchers(self): 
     74        """No matchers""" 
     75        return None 
    7276#end GenericPluginFactory 
    7377 
  • nbshell/trunk/nbshell/PythonPlugin.py

    r838 r850  
    5656    #    more info""" 
    5757    #    return "raw" #Probably only the python code plugin should be raw 
    58          
     58     
    5959    def CreateDocumentPlugin(self,document, element): 
    6060        """Creates the document part of the plugin. The returned object is  
     
    8080            return None #Well here I should throw an exception, however I am  
    8181                        #not supposed to get to this line for a long long time 
     82                         
     83    def get_matchers(self): 
     84        """Matches the <ipython-block> element""" 
     85        return lambda element:element.tag == 'ipython-block' 
    8286#end GenericPluginFactory 
    8387 
  • nbshell/trunk/nbshell/Sheet.py

    r835 r850  
    2222from nbshell import PythonPlugin 
    2323from nbshell.utils import * 
     24from nbshell import SimpleXMLWriter 
    2425 
    2526 
     
    155156 
    156157            self.__clear_celllist(update = False) 
    157             self.__update_celllist(update = False) 
     158            self._update_celllist(update = False) 
    158159        if dicts: 
    159160            self.__update_dicts() 
     
    226227 
    227228 
    228     def __update_celllist(self, update = False): 
     229    def _update_celllist(self, update = False): 
     230         
     231        #Get the matchers from the plugin factories 
     232        matchers = {} 
     233        for plugin_string in self.factory.keys(): 
     234            result = self.factory[plugin_string].get_matchers() 
     235            #get_matchers must return either one matcher or a list of matchers 
     236            if callable(result): #one matcher 
     237                matchers[result] = plugin_string 
     238            elif result: #a list of matchers 
     239                for a in result: #there should be a more pythonic way to do this 
     240                    matchers[a] = plugin_string 
     241            #else get_match can return None 
     242         
     243        buffer = StringIO.StringIO() 
     244        read_pos = [0] #possition in the buffer from which to start reading. It must be mutable 
     245        writer = SimpleXMLWriter.XMLWriter(buffer, encoding='utf-8') 
     246         
     247        def process_element(element): 
     248            """Processes one unhandled element""" 
     249            #write the start tag and any text after it 
     250            if element.tag != 'sheet': 
     251                writer.start(element.tag, element.attrib) 
     252            writer.data(element.text or '') 
     253            #Get a list of the children 
     254            children = iter(element[:]) 
     255            try: 
     256                child = children.next() 
     257            except StopIteration: 
     258                #The sheet is empty. Create one empty xml block 
     259                self.InsertCell('plaintext', update = False, text = '') 
     260            else: 
     261                while True: 
     262                    result = False 
     263                    #Test the child against the matchers until one returns non False 
     264                    for matcher in matchers.keys(): 
     265                        result = matcher(child) 
     266                        if result: 
     267                            break 
     268                    if not result: 
     269                        #None of the mathchers returned true, so process the 
     270                        #element the standard way 
     271                        process_element(child) 
     272                        #append the tail to the buffer 
     273                        writer.data(child.tail or '') 
     274                        #get the next child 
     275                        try: 
     276                            child = children.next() 
     277                        except: 
     278                            break 
     279                    else: #The element returned a match. 
     280                        #Create a new xml block and empty the buffer if there is something in it 
     281                        if buffer.tell() != read_pos[0]: 
     282                            buffer.seek(read_pos[0]) 
     283                            self.InsertCell('plaintext',update = False, text = buffer.read()) 
     284                            read_pos[0] = buffer.tell() 
     285     
     286                        plugin_string = matchers[matcher] 
     287                        if callable(result): 
     288                            #If the result is callable, this means that we want to 
     289                            #match a list of consequtive elements to give to the 
     290                            #plugin. To do that call the result on the next 
     291                            #element and get the result and with it do the same, 
     292                            #until there are no more elements or one of the 
     293                            #matchers returned false. 
     294                            elem_list = [] 
     295                            while result: 
     296                                elem_list.append(child) 
     297                                try: 
     298                                    child = children.next() 
     299                                except StopIteration: 
     300                                    break 
     301                                result = result(child) 
     302                             
     303                            #Now create the corresponding block 
     304                            self.InsertCell(plugin_string, update = False, 
     305                                           element_list = elem_list) 
     306                        else:  
     307                            #This is a simple match, create the block, giving the 
     308                            #element as a parameter 
     309                            self.InsertCell(plugin_string, update=False, element = child) 
     310                            #get the next element 
     311                            try: 
     312                                child = children.next() 
     313                            except StopIteration: 
     314                                break 
     315            #The element is almost processed, write the end tag and exit 
     316            if element.tag != 'sheet': 
     317                writer.end(tag = element.tag) 
     318        #process_element end 
     319         
     320        #Run the function with the sheet element, and empty the buffer at the end 
     321        process_element(self.element) 
     322        if buffer.tell() != read_pos[0]: 
     323            buffer.seek(read_pos[0]) 
     324            self.InsertCell('plaintext', update = False, text = buffer.read()) 
     325        buffer.close() 
     326        if update: 
     327            self.Update(celllist = False) 
     328         
     329 
     330             
     331    def _old_update_celllist(self, update = False): 
    229332        """Updates the celllist from self.element and updates the view if 
    230333        update == True """ 
  • nbshell/trunk/nbshell/ipnNotebookWidget.py

    r734 r850  
    8686            self.OnKeyDown(evt) 
    8787             
    88         def OnKeyDown(self,evt): 
    89             evt.Skip() 
     88    def OnKeyDown(self,evt): 
     89        evt.Skip() 
    9090 
    9191class ipnNotebook (wx.ScrolledWindow): 
  • nbshell/trunk/nbshell/textplugin.py

    r829 r850  
    7272            return None #Well here I should throw an exception, however I am  
    7373                        #not supposed to get to this line for a long long time 
     74     
     75    def get_matchers(self): 
     76        """matches a sequence of para elements without inner subelements""" 
     77        def matcher(element): 
     78            if element.tag == 'para' and len(element) == 0: 
     79                return matcher 
     80            else: 
     81                return False 
     82         
     83        return matcher 
     84 
    7485#end GenericPluginFactory 
    7586