Changeset 683
- Timestamp:
- 08/11/05 06:40:31 (3 years ago)
- Files:
-
- nbshell/trunk/nbshell/PlainTextPlugin.py (modified) (7 diffs)
- nbshell/trunk/nbshell/Sheet.py (modified) (5 diffs)
- nbshell/trunk/nbshell/SimpleXMLWriter.py (added)
- nbshell/trunk/nbshell/ipnDocument.py (modified) (1 diff)
- nbshell/trunk/nbshell/utils.py (modified) (1 diff)
- nbshell/trunk/tests/tut-2.3.5-db.nbk (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
nbshell/trunk/nbshell/PlainTextPlugin.py
r679 r683 12 12 __version__ = Release.version 13 13 14 import StringIO 15 14 16 import wx 15 17 from wx import stc 18 19 from nbshell.SimpleXMLWriter import XMLWriter 16 20 17 21 def GetPluginFactory(): … … 42 46 # return "encoded" #Probably only the python code plugin should be raw 43 47 44 def CreateDocumentPlugin(self,document , element):48 def CreateDocumentPlugin(self,document): 45 49 """Creates the document part of the plugin. The returned object is 46 50 stored in ipgDocument.celllist and is responsible for storing and 47 51 serialization of data. "data" contains initial data for the plugin. 48 52 """ 49 return PlainTextDocumentPlugin(document , element)53 return PlainTextDocumentPlugin(document) 50 54 51 55 def CreateViewPlugin(self,docplugin, view): … … 68 72 69 73 class PlainTextDocumentPlugin(object): 70 def __init__(self, document , element):74 def __init__(self, document): 71 75 """Initialization. If element is <sheet> then the text is 72 76 element.text. If the element is something else, then the text is … … 75 79 self.document = document 76 80 self.sheet = document.sheet 77 self.element = element 78 if element.tag == 'sheet': 79 self.start = True #self.start stores if the text block is the first in the sheet 80 else: 81 self.start = False 81 self.text = '' 82 82 self.index = None #Set by AddCell, InsertCell, DeleteCell 83 83 self.view = None #This plugin is designed for a single view. For 84 84 #multiple views there should be some modifications 85 #self.LoadData(data) 86 85 87 86 type='plaintext' 88 87 88 def LoadXML(self, iterator, prevlist, elemlist, endtaglist): 89 """The LoadXML method gets text representing a part of the xml tree. 90 The first element in this part is 'elemlist[-1]', the next are 91 retrieved by 'iterator'. 'prevlist' and 'elemlist' are the previous two 92 results of calling 'iterator.next()'. The LoadXML method stops when the 93 'iterator' throws a 'StopIteration' exception, or it finds an element for 94 which there is a plugin to process it. Currently there are such 95 plugins for the <ipython-block> and <ipython-figure> elements. In that 96 case it returns the last 'elemlist'. The tags on which LoadXML 97 should exit are in 'endtaglist'""" 98 99 text = StringIO.StringIO() 100 #TODO: what encoding should I choose? 101 writer = XMLWriter(text, encoding='utf-8') 102 103 l1 = len(prevlist) 104 i = l1-1 105 l2 = len(elemlist) 106 while i>=0 and (i>= l2 or prevlist[i] != elemlist[i]): 107 writer.end(prevlist[i].tag) 108 writer.data(prevlist[i].tail or '') 109 i-=1 110 111 while elemlist != () and elemlist[-1].tag not in endtaglist: 112 prevlist = elemlist 113 writer.start(elemlist[-1].tag, elemlist[-1].attrib) 114 writer.data(elemlist[-1].text or '') 115 try: 116 elemlist = iterator.next() 117 except StopIteration: 118 elemlist = () 119 l1 = len(prevlist) 120 i = l1-1 121 l2 = len(elemlist) 122 while i>=0 and (i>= l2 or prevlist[i] != elemlist[i]): 123 writer.end(prevlist[i].tag) 124 writer.data(prevlist[i].tail or '') 125 i-=1 126 127 writer.flush() 128 self.text = text.getvalue() 129 if elemlist == (): 130 raise StopIteration 131 else: #elemlist[-1].tag in endtaglist: 132 return elemlist 133 134 89 135 def __len__(self): 90 136 return self.view.window.GetLenght() … … 96 142 modified = property(fget = IsModified) 97 143 98 def GetText(self):99 text = self.element.text100 if self.start:101 text = self.element.text102 else:103 text = self.element.tail104 return (text is not None) and text or ''105 106 def SetText(self, text):107 """Sets the text in the document"""108 if self.start:109 self.element.text = text110 else:111 self.element.tail = text112 return text144 #def GetText(self): 145 # text = self.element.text 146 # if self.start: 147 # text = self.element.text 148 # else: 149 # text = self.element.tail 150 # return (text is not None) and text or '' 151 152 #def SetText(self, text): 153 # """Sets the text in the document""" 154 # if self.start: 155 # self.element.text = text 156 # else: 157 # self.element.tail = text 158 # return text 113 159 114 160 def Clear(self): 115 161 """Clears all data""" 116 self. SetText('')162 self.text = '' 117 163 if self.view is not None: 118 164 self.view.Update() 119 165 120 text = property(GetText, SetText, Clear, doc = """The text contained in this instance""")166 #text = property(GetText, SetText, Clear, doc = """The text contained in this instance""") 121 167 122 168 def GetFactory(self): … … 125 171 def Split(self, pos, update = True): 126 172 """Removes the text after the given position and returns it""" 127 text = self. GetText()128 self. SetText(text[:pos])173 text = self.text 174 self.text = text[:pos] 129 175 if update: 130 176 self.view.Update() … … 232 278 def InsertCode(self): 233 279 #lazy document update. We update the document when it is needed 234 self.doc. SetText(self.window.GetText())280 self.doc.text = self.window.GetText() 235 281 pos = self.window.GetCurrentPos() 236 282 self.doc.sheet.InsertCode(self.doc,pos, update = True) nbshell/trunk/nbshell/Sheet.py
r679 r683 19 19 20 20 from nbshell import PythonPlugin 21 from nbshell.utils import getindex 21 from nbshell.utils import getindex, getiterator2 22 22 23 23 … … 84 84 #This is the first or the last text cell and cannot be deleted. 85 85 #Just clear the text. 86 cell. SetText('')86 cell.text= '' 87 87 if update: 88 88 cell.view.Update() … … 151 151 for doccell in self.celllist: 152 152 doccell.view.UpdateDoc() 153 154 155 def __append_plaintext_cell(self, iterator, prevlist, elemlist,\ 156 endtaglist, update = True): 157 """Append a plaintext cell at the end of the document. Plaintext cells 158 are cells which deal with yet unsupported parts of the notebook 159 format. They simply display the xml for these parts. The supported 160 tags are in endtaglist""" 161 factory = self.factory['plaintext'] 162 cell = factory.CreateDocumentPlugin(self.doc) 163 try: 164 elemlist = cell.LoadXML(iterator, prevlist, elemlist,\ 165 endtaglist) 166 finally: 167 if cell.text != '': 168 view = factory.CreateViewPlugin(cell, self.view) 169 self.__add_cell(cell) 170 if update: 171 view.Update() 172 self.view.Update() 173 return elemlist 174 153 175 154 176 def __update_celllist(self, update = False): 155 177 """Updates the celllist from self.element and updates the view if 156 178 update == True """ 157 if self.element.text is not None: 158 self.InsertCell('plaintext',update=False,element = self.element) 159 l = len(self.element) 160 for i in range(l): 161 elem = self.element[i] 162 if elem.tag == 'ipython-block': 163 self.InsertCell('python', update=False, ipython_block = elem) 164 elif elem.tag == 'ipython-figure': 165 self.InsertCell('figure', update = False, element = elem) 166 self.InsertCell('plaintext', update=False, element = elem) 179 180 iter = getiterator2(self.element) 181 elemlist = iter.next() 182 tag2type = {'ipython-block':'python', 'ipython-figure':'figure'} 183 flag = False 184 while True: 185 try: 186 elem = elemlist[-1] 187 if elem.tag in tag2type.keys(): 188 self.InsertCell(tag2type[elem.tag], update = False, \ 189 ipython_block = elem) 190 l = len(elemlist) 191 prevlist = elemlist[:-1] 192 while len(elemlist)>=l and elemlist[l-1] == elem: 193 flag = True 194 elemlist = iter.next() 195 flag = False 196 elemlist = self.__append_plaintext_cell(iter, prevlist,\ 197 elemlist, update = False,\ 198 endtaglist = tuple(tag2type.keys())) 199 else: 200 #plaintext cells will deal with all the elements we have not 201 #dealt with yet 202 prevlist = () 203 flag = False 204 elemlist = self.__append_plaintext_cell(iter, prevlist,\ 205 elemlist, update = False,\ 206 endtaglist = ('ipython-block', 'ipython-figure')) 207 except StopIteration: 208 #Add any remaining tags 209 if flag: 210 try: 211 self.__append_plaintext_cell(iter, 212 prevlist,(), update = False,\ 213 endtaglist = ('ipython-block', 'ipython-figure')) 214 except StopIteration: 215 pass 216 break 167 217 if update: 168 218 self.view.Update() 219 169 220 170 221 def __clear_celllist(self, update = False): … … 306 357 #We only need to set self.last 307 358 self.element = self.notebook.default_sheet() 308 self.element.text = \309 """ This is a temporary message, until I write proper help. Please use Return 310 to insert new lines and Shift-Return to execute inputs.311 """312 359 textelem = etree.Element('para') 360 textelem.text =\ 361 """ This is a temporary message, until I write proper help. 362 Please use Return to insert new lines and Shift-Return to execute inputs. """ 363 self.element[0:0] = [textelem] 313 364 # Now remove the old sheet and replace it with the new one 314 365 oldsheet = self.notebook.root.find('sheet') … … 320 371 #etree.dump(self.notebook.root) #dbg 321 372 #Here we do not call InsertElement, so we must update the dictionaries 373 etree.dump(self.notebook.root) 322 374 self.Update(update, celllist = True, dicts = True) 323 375 nbshell/trunk/nbshell/ipnDocument.py
r679 r683 20 20 from nbshell import IPythonLog 21 21 from nbshell import Sheet 22 from nbshell import utils 22 23 23 24 class ipnDocument(object): nbshell/trunk/nbshell/utils.py
r667 r683 41 41 return i 42 42 return None 43 44 def getiterator2(root): 45 """Returns an iterator which for each subelement yields a tuple of all 46 elements in the path from the root to the given subelement""" 47 48 root_tup = (root,) 49 yield root_tup 50 #Recursive generators, yummy :) 51 for subelement in root: 52 iter = getiterator2(subelement) 53 for result in iter: 54 yield root_tup + result
