]>
Commit | Line | Data |
---|---|---|
d14a1e28 | 1 | """Buffer class.""" |
1fded56b | 2 | |
d14a1e28 RD |
3 | __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>" |
4 | __cvsid__ = "$Id$" | |
5 | __revision__ = "$Revision$"[11:-2] | |
1fded56b | 6 | |
d14a1e28 RD |
7 | from interpreter import Interpreter |
8 | import imp | |
9 | import os | |
10 | import sys | |
11 | ||
12 | import document | |
13 | ||
14 | try: | |
15 | True | |
16 | except NameError: | |
17 | True = 1==1 | |
18 | False = 1==0 | |
19 | ||
20 | ||
21 | class Buffer: | |
22 | """Buffer class.""" | |
23 | ||
24 | id = 0 | |
25 | ||
26 | def __init__(self, filename=None): | |
27 | """Create a Buffer instance.""" | |
28 | Buffer.id += 1 | |
29 | self.id = Buffer.id | |
30 | self.interp = Interpreter(locals={}) | |
31 | self.name = '' | |
32 | self.editors = {} | |
33 | self.editor = None | |
34 | self.modules = sys.modules.keys() | |
35 | self.syspath = sys.path[:] | |
36 | while True: | |
37 | try: | |
38 | self.syspath.remove('') | |
39 | except ValueError: | |
40 | break | |
41 | while True: | |
42 | try: | |
43 | self.syspath.remove('.') | |
44 | except ValueError: | |
45 | break | |
46 | self.open(filename) | |
47 | ||
48 | def addEditor(self, editor): | |
49 | """Add an editor.""" | |
50 | self.editor = editor | |
51 | self.editors[editor.id] = editor | |
52 | ||
53 | def hasChanged(self): | |
54 | """Return True if text in editor has changed since last save.""" | |
55 | if self.editor: | |
56 | return self.editor.hasChanged() | |
57 | else: | |
58 | return False | |
59 | ||
60 | def new(self, filepath): | |
61 | """New empty buffer.""" | |
62 | if not filepath: | |
63 | return | |
64 | if os.path.exists(filepath): | |
65 | self.confirmed = self.overwriteConfirm(filepath) | |
66 | else: | |
67 | self.confirmed = True | |
68 | ||
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): | |
80 | self.confirmed = True | |
81 | if self.editor: | |
82 | text = self.doc.read() | |
83 | self.editor._setBuffer(buffer=self, text=text) | |
84 | ||
85 | def overwriteConfirm(filepath): | |
86 | """Confirm overwriting an existing file.""" | |
87 | return False | |
88 | ||
89 | def save(self): | |
90 | """Save buffer.""" | |
91 | filepath = self.doc.filepath | |
92 | if not filepath: | |
93 | return # XXX Get filename | |
94 | if not os.path.exists(filepath): | |
95 | self.confirmed = True | |
96 | if not self.confirmed: | |
97 | self.confirmed = self.overwriteConfirm(filepath) | |
98 | if self.confirmed: | |
99 | self.doc.write(self.editor.getText()) | |
100 | if self.editor: | |
101 | self.editor.setSavePoint() | |
102 | ||
103 | def saveAs(self, filename): | |
104 | """Save buffer.""" | |
105 | self.doc = document.Document(filename) | |
106 | self.name = self.doc.filename | |
107 | self.modulename = self.doc.filebase | |
108 | self.save() | |
109 | ||
110 | def updateNamespace(self): | |
111 | """Update the namespace for autocompletion and calltips. | |
112 | ||
113 | Return True if updated, False if there was an error.""" | |
114 | if not self.interp or not hasattr(self.editor, 'getText'): | |
115 | return False | |
116 | syspath = sys.path | |
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() | |
124 | try: | |
125 | try: | |
126 | code = compile(text, name, 'exec') | |
127 | except: | |
128 | raise | |
129 | # return False | |
130 | try: | |
131 | exec code in newspace | |
132 | except: | |
133 | raise | |
134 | # return False | |
135 | else: | |
136 | # No problems, so update the namespace. | |
137 | self.interp.locals.clear() | |
138 | self.interp.locals.update(newspace) | |
139 | return True | |
140 | finally: | |
141 | sys.path = syspath | |
142 | for m in sys.modules.keys(): | |
143 | if m not in self.modules: | |
144 | del sys.modules[m] |