]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/py/buffer.py
   3 __author__ 
= "Patrick K. O'Brien <pobrien@orbtech.com>" 
   5 __revision__ 
= "$Revision$"[11:-2] 
   7 from interpreter 
import Interpreter
 
  26     def __init__(self
, filename
=None): 
  27         """Create a Buffer instance.""" 
  30         self
.interp 
= Interpreter(locals={}) 
  34         self
.modules 
= sys
.modules
.keys() 
  35         self
.syspath 
= sys
.path
[:] 
  38                 self
.syspath
.remove('') 
  43                 self
.syspath
.remove('.') 
  48     def addEditor(self
, editor
): 
  51         self
.editors
[editor
.id] = editor
 
  54         """Return True if text in editor has changed since last save.""" 
  56             return self
.editor
.hasChanged() 
  60     def new(self
, filepath
): 
  61         """New empty buffer.""" 
  64         if os
.path
.exists(filepath
): 
  65             self
.confirmed 
= self
.overwriteConfirm(filepath
) 
  69     def open(self
, filename
): 
  70         """Open file into buffer.""" 
  71         self
.doc 
= document
.Document(filename
) 
  72         self
.name 
= self
.doc
.filename 
or ('Untitled:' + str(self
.id)) 
  73         self
.modulename 
= self
.doc
.filebase
 
  74         # XXX This should really make sure filedir is first item in syspath. 
  75         # XXX Or maybe this should be moved to the update namespace method. 
  76         if self
.doc
.filedir 
and self
.doc
.filedir 
not in self
.syspath
: 
  77             # To create the proper context for updateNamespace. 
  78             self
.syspath
.insert(0, self
.doc
.filedir
) 
  79         if self
.doc
.filepath 
and os
.path
.exists(self
.doc
.filepath
): 
  82             text 
= self
.doc
.read() 
  83             self
.editor
._setBuffer
(buffer=self
, text
=text
) 
  85     def overwriteConfirm(filepath
): 
  86         """Confirm overwriting an existing file.""" 
  91         filepath 
= self
.doc
.filepath
 
  93             return  # XXX Get filename 
  94         if not os
.path
.exists(filepath
): 
  96         if not self
.confirmed
: 
  97             self
.confirmed 
= self
.overwriteConfirm(filepath
) 
  99             self
.doc
.write(self
.editor
.getText()) 
 101                 self
.editor
.setSavePoint() 
 103     def saveAs(self
, filename
): 
 105         self
.doc 
= document
.Document(filename
) 
 106         self
.name 
= self
.doc
.filename
 
 107         self
.modulename 
= self
.doc
.filebase
 
 110     def updateNamespace(self
): 
 111         """Update the namespace for autocompletion and calltips. 
 113         Return True if updated, False if there was an error.""" 
 114         if not self
.interp 
or not hasattr(self
.editor
, 'getText'): 
 117         sys
.path 
= self
.syspath
 
 118         text 
= self
.editor
.getText() 
 119         text 
= text
.replace('\r\n', '\n') 
 120         text 
= text
.replace('\r', '\n') 
 121         name 
= self
.modulename 
or self
.name
 
 122         module 
= imp
.new_module(name
) 
 123         newspace 
= module
.__dict
__.copy() 
 126                 code 
= compile(text
, name
, 'exec') 
 131                 exec code 
in newspace
 
 136                 # No problems, so update the namespace. 
 137                 self
.interp
.locals.clear() 
 138                 self
.interp
.locals.update(newspace
) 
 142             for m 
in sys
.modules
.keys(): 
 143                 if m 
not in self
.modules
: