[PyCrust-cvs] PyCrust document.py,NONE,1.1 buffer.py,NONE,1.1 editor.py,1.13,1.14
Brought to you by:
pobrien
From: <po...@us...> - 2003-04-03 02:35:05
|
Update of /cvsroot/pycrust/PyCrust In directory sc8-pr-cvs1:/tmp/cvs-serv16207 Modified Files: editor.py Added Files: document.py buffer.py Log Message: Restructured files. --- NEW FILE: document.py --- """Document class.""" __author__ = "Patrick K. O'Brien <po...@or...>" __cvsid__ = "$Id: document.py,v 1.1 2003/04/03 02:34:48 pobrien Exp $" __revision__ = "$Revision: 1.1 $"[11:-2] import os try: True except NameError: True = 1==1 False = 1==0 class Document: """Document class.""" def __init__(self, filename=None): """Create a Document instance.""" self.filename = filename self.filepath = None self.filedir = None self.filebase = None self.fileext = None if self.filename: self.filepath = os.path.abspath(self.filename) self.filedir, self.filename = os.path.split(self.filepath) self.filebase, self.fileext = os.path.splitext(self.filename) def read(self): """Return contents of file.""" f = file(self.filepath, 'rb') try: return f.read() finally: f.close() def write(self, text): """Write text to file.""" try: f = file(self.filepath, 'w') f.write(text) finally: if f: f.close() --- NEW FILE: buffer.py --- """Buffer class.""" __author__ = "Patrick K. O'Brien <po...@or...>" __cvsid__ = "$Id: buffer.py,v 1.1 2003/04/03 02:34:48 pobrien Exp $" __revision__ = "$Revision: 1.1 $"[11:-2] from wxPython import wx import imp import os import sys import document try: True except NameError: True = 1==1 False = 1==0 class Buffer: """Buffer class.""" id = 0 def __init__(self, editor, interp, filename=None): """Create a Buffer instance.""" Buffer.id += 1 self.id = Buffer.id self.name = '' self.editor = editor self.interp = interp self.modules = sys.modules.keys() self.syspath = sys.path[:] while True: try: self.syspath.remove('') except ValueError: break while True: try: self.syspath.remove('.') except ValueError: break self.open(filename) def getStatus(self): """Return (filepath, line, column) status tuple.""" editor = self.editor pos = editor.GetCurrentPos() line = editor.LineFromPosition(pos) + 1 col = editor.GetColumn(pos) + 1 status = (self.doc.filepath or self.name, line, col) return status def hasChanged(self): """Return True if text in editor has changed since last save.""" return self.editor.GetModify() def new(self, filepath): """New empty buffer.""" if not filepath: return if os.path.exists(filepath): self.confirmed = self.overwriteConfirm(filepath) else: self.confirmed = True def open(self, filename): """Open file into buffer.""" self.doc = document.Document(filename) self.name = self.doc.filename or ('Untitled:' + str(self.id)) self.modulename = self.doc.filebase if self.doc.filepath and os.path.exists(self.doc.filepath): self.editor.SetText(self.doc.read()) self.editor.EmptyUndoBuffer() self.confirmed = True if self.doc.filedir and self.doc.filedir not in self.syspath: self.syspath.insert(0, self.doc.filedir) def overwriteConfirm(filepath): """Confirm overwriting an existing file.""" return False def save(self): """Save buffer.""" filepath = self.doc.filepath if not filepath: return # XXX Get filename if not os.path.exists(filepath): self.confirmed = True if not self.confirmed: self.confirmed = self.overwriteConfirm(filepath) if self.confirmed: self.doc.write(self.editor.GetText()) self.editor.SetSavePoint() def updateNamespace(self): """Update the namespace for autocompletion and calltips. Return True if updated, False if there was an error.""" backup = self.interp.locals syspath = sys.path sys.path = self.syspath code = self.editor.GetText() module = imp.new_module(str(self.modulename)) namespace = module.__dict__ try: try: exec code in namespace except: self.interp.locals = backup return False else: self.interp.locals = namespace.copy() return True finally: sys.path = syspath for m in sys.modules.keys(): if m not in self.modules: del sys.modules[m] Index: editor.py =================================================================== RCS file: /cvsroot/pycrust/PyCrust/editor.py,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** editor.py 3 Apr 2003 01:28:52 -0000 1.13 --- editor.py 3 Apr 2003 02:34:48 -0000 1.14 *************** *** 1,3 **** ! """PyAlaMode code editor.""" __author__ = "Patrick K. O'Brien <po...@or...>" --- 1,3 ---- ! """PyAlaMode editor.""" __author__ = "Patrick K. O'Brien <po...@or...>" *************** *** 7,19 **** from wxPython import wx - import imp - import os - import sys - import time - import base import dispatcher import frame ! from shell import Shell try: --- 7,15 ---- from wxPython import wx import base + import buffer import dispatcher import frame ! import shell try: *************** *** 177,182 **** class EditorNotebook(wx.wxNotebook): ! """Combines a code editor with a shell.""" def __init__(self, parent, filename=None): --- 173,196 ---- + class BufferNotebook(wx.wxNotebook): + """A notebook containing a page for each buffer.""" + + def __init__(self, parent): + """Create a BufferNotebook instance.""" + wx.wxNotebook.__init__(self, parent, id=-1) + wx.EVT_NOTEBOOK_PAGE_CHANGED(self, self.GetId(), self.OnPageChanged) + + def OnPageChanged(self, event): + """Page changed event handler.""" + selection = event.GetSelection() + page = self.GetPage(selection) + buffer = page.buffer + dispatcher.send(signal='BufferChange', sender=self, buffer=buffer) + selection = page.GetSelection() + page.focus(selection) + + class EditorNotebook(wx.wxNotebook): ! """A notebook containing an editor page and a shell page.""" def __init__(self, parent, filename=None): *************** *** 190,195 **** shellparent = self editorparent = self ! self.shell = Shell(parent=shellparent, ! style=wx.wxCLIP_CHILDREN | wx.wxSUNKEN_BORDER) self.editor = Editor(interp=self.shell.interp, parent=editorparent, filename=filename) --- 204,209 ---- shellparent = self editorparent = self ! self.shell = shell.Shell(parent=shellparent, ! style=wx.wxCLIP_CHILDREN | wx.wxSUNKEN_BORDER) self.editor = Editor(interp=self.shell.interp, parent=editorparent, filename=filename) *************** *** 241,246 **** wx.EVT_CHAR(self, self.OnChar) wx.EVT_KEY_DOWN(self, self.OnKeyDown) ! self.buffer = Buffer(editor=self, interp=self.interp, ! filename=filename) def OnChar(self, event): --- 255,260 ---- wx.EVT_CHAR(self, self.OnChar) wx.EVT_KEY_DOWN(self, self.OnKeyDown) ! self.buffer = buffer.Buffer(editor=self, interp=self.interp, ! filename=filename) def OnChar(self, event): *************** *** 336,491 **** self.CallTipShow(tippos, tip) self.CallTipSetHighlight(0, size) - - - class BufferNotebook(wx.wxNotebook): - """A notebook containing a code editor and shell for each buffer.""" - - def __init__(self, parent): - """Create a BufferNotebook instance.""" - wx.wxNotebook.__init__(self, parent, id=-1) - wx.EVT_NOTEBOOK_PAGE_CHANGED(self, self.GetId(), self.OnPageChanged) - - def OnPageChanged(self, event): - """Page changed event handler.""" - selection = event.GetSelection() - notebook = self.GetPage(selection) - buffer = notebook.buffer - dispatcher.send(signal='BufferChange', sender=self, buffer=buffer) - selection = notebook.GetSelection() - notebook.focus(selection) - - - class Buffer: - """Buffer class.""" - - id = 0 - - def __init__(self, editor, interp, filename=None): - """Create a Buffer instance.""" - Buffer.id += 1 - self.id = Buffer.id - self.name = '' - self.editor = editor - self.interp = interp - self.modules = sys.modules.keys() - self.syspath = sys.path[:] - while True: - try: - self.syspath.remove('') - except ValueError: - break - while True: - try: - self.syspath.remove('.') - except ValueError: - break - self.open(filename) - - def getStatus(self): - """Return (filepath, line, column) status tuple.""" - editor = self.editor - pos = editor.GetCurrentPos() - line = editor.LineFromPosition(pos) + 1 - col = editor.GetColumn(pos) + 1 - status = (self.doc.filepath or self.name, line, col) - return status - - def hasChanged(self): - """Return True if text in editor has changed since last save.""" - return self.editor.GetModify() - - def new(self, filepath): - """New empty buffer.""" - if not filepath: - return - if os.path.exists(filepath): - self.confirmed = self.overwriteConfirm(filepath) - else: - self.confirmed = True - - def open(self, filename): - """Open file into buffer.""" - self.doc = Document(filename) - self.name = self.doc.filename or ('Untitled:' + str(self.id)) - self.modulename = self.doc.filebase - if self.doc.filepath and os.path.exists(self.doc.filepath): - self.editor.SetText(self.doc.read()) - self.editor.EmptyUndoBuffer() - self.confirmed = True - if self.doc.filedir and self.doc.filedir not in self.syspath: - self.syspath.insert(0, self.doc.filedir) - - def overwriteConfirm(filepath): - """Confirm overwriting an existing file.""" - return False - - def save(self): - """Save buffer.""" - filepath = self.doc.filepath - if not filepath: - return # XXX Get filename - if not os.path.exists(filepath): - self.confirmed = True - if not self.confirmed: - self.confirmed = self.overwriteConfirm(filepath) - if self.confirmed: - self.doc.write(self.editor.GetText()) - self.editor.SetSavePoint() - - def updateNamespace(self): - """Update the namespace for autocompletion and calltips. - - Return True if updated, False if there was an error.""" - backup = self.interp.locals - syspath = sys.path - sys.path = self.syspath - code = self.editor.GetText() - module = imp.new_module(str(self.modulename)) - namespace = module.__dict__ - try: - try: - exec code in namespace - except: - self.interp.locals = backup - return False - else: - self.interp.locals = namespace.copy() - return True - finally: - sys.path = syspath - for m in sys.modules.keys(): - if m not in self.modules: - del sys.modules[m] - - - class Document: - """Document class.""" - - def __init__(self, filename=None): - """Create a Document instance.""" - self.filename = filename - self.filepath = None - self.filedir = None - self.filebase = None - self.fileext = None - if self.filename: - self.filepath = os.path.abspath(self.filename) - self.filedir, self.filename = os.path.split(self.filepath) - self.filebase, self.fileext = os.path.splitext(self.filename) - - def read(self): - """Return contents of file.""" - f = file(self.filepath, 'rb') - try: - return f.read() - finally: - f.close() - - def write(self, text): - """Write text to file.""" - try: - f = file(self.filepath, 'w') - f.write(text) - finally: - if f: - f.close() --- 350,351 ---- |