]>
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
 
  20     def __init__(self
, filename
=None): 
  21         """Create a Buffer instance.""" 
  24         self
.interp 
= Interpreter(locals={}) 
  28         self
.modules 
= sys
.modules
.keys() 
  29         self
.syspath 
= sys
.path
[:] 
  32                 self
.syspath
.remove('') 
  37                 self
.syspath
.remove('.') 
  42     def addEditor(self
, editor
): 
  45         self
.editors
[editor
.id] = editor
 
  48         """Return True if text in editor has changed since last save.""" 
  50             return self
.editor
.hasChanged() 
  54     def new(self
, filepath
): 
  55         """New empty buffer.""" 
  58         if os
.path
.exists(filepath
): 
  59             self
.confirmed 
= self
.overwriteConfirm(filepath
) 
  63     def open(self
, filename
): 
  64         """Open file into buffer.""" 
  65         self
.doc 
= document
.Document(filename
) 
  66         self
.name 
= self
.doc
.filename 
or ('Untitled:' + str(self
.id)) 
  67         self
.modulename 
= self
.doc
.filebase
 
  68         # XXX This should really make sure filedir is first item in syspath. 
  69         # XXX Or maybe this should be moved to the update namespace method. 
  70         if self
.doc
.filedir 
and self
.doc
.filedir 
not in self
.syspath
: 
  71             # To create the proper context for updateNamespace. 
  72             self
.syspath
.insert(0, self
.doc
.filedir
) 
  73         if self
.doc
.filepath 
and os
.path
.exists(self
.doc
.filepath
): 
  76             text 
= self
.doc
.read() 
  77             self
.editor
._setBuffer
(buffer=self
, text
=text
) 
  79     def overwriteConfirm(filepath
): 
  80         """Confirm overwriting an existing file.""" 
  85         filepath 
= self
.doc
.filepath
 
  87             return  # XXX Get filename 
  88         if not os
.path
.exists(filepath
): 
  90         if not self
.confirmed
: 
  91             self
.confirmed 
= self
.overwriteConfirm(filepath
) 
  93             self
.doc
.write(self
.editor
.getText()) 
  95                 self
.editor
.setSavePoint() 
  97     def saveAs(self
, filename
): 
  99         self
.doc 
= document
.Document(filename
) 
 100         self
.name 
= self
.doc
.filename
 
 101         self
.modulename 
= self
.doc
.filebase
 
 104     def updateNamespace(self
): 
 105         """Update the namespace for autocompletion and calltips. 
 107         Return True if updated, False if there was an error.""" 
 108         if not self
.interp 
or not hasattr(self
.editor
, 'getText'): 
 111         sys
.path 
= self
.syspath
 
 112         text 
= self
.editor
.getText() 
 113         text 
= text
.replace('\r\n', '\n') 
 114         text 
= text
.replace('\r', '\n') 
 115         name 
= self
.modulename 
or self
.name
 
 116         module 
= imp
.new_module(name
) 
 117         newspace 
= module
.__dict
__.copy() 
 120                 code 
= compile(text
, name
, 'exec') 
 125                 exec code 
in newspace
 
 130                 # No problems, so update the namespace. 
 131                 self
.interp
.locals.clear() 
 132                 self
.interp
.locals.update(newspace
) 
 136             for m 
in sys
.modules
.keys(): 
 137                 if m 
not in self
.modules
: