22 |
from base import TitledObject, Modifiable |
from base import TitledObject, Modifiable |
23 |
from map import Map |
from map import Map |
24 |
from data import ShapefileStore |
from data import ShapefileStore |
25 |
|
from table import DBFTable |
26 |
|
|
27 |
from transientdb import TransientDatabase |
from transientdb import TransientDatabase, AutoTransientTable |
28 |
|
|
29 |
class AutoRemoveFile: |
class AutoRemoveFile: |
30 |
|
|
130 |
self.filename = None |
self.filename = None |
131 |
self.maps = [] |
self.maps = [] |
132 |
self.tables = [] |
self.tables = [] |
133 |
|
self.shapestores = [] |
134 |
self.extensions = [] |
self.extensions = [] |
135 |
self.temp_dir = None |
self.temp_dir = None |
136 |
self.transient_db = None |
self.transient_db = None |
180 |
extension.Subscribe(channel, self.forward, channel) |
extension.Subscribe(channel, self.forward, channel) |
181 |
self.changed(EXTENSIONS_CHANGED) |
self.changed(EXTENSIONS_CHANGED) |
182 |
|
|
183 |
|
def ShapeStores(self): |
184 |
|
"""Return a list of all ShapeStore objects open in the session""" |
185 |
|
return [store() for store in self.shapestores] |
186 |
|
|
187 |
|
def _add_shapestore(self, store): |
188 |
|
"""Internal: Add the shapestore to the list of shapestores""" |
189 |
|
self.shapestores.append(weakref.ref(store, |
190 |
|
self._clean_weak_store_refs)) |
191 |
|
|
192 |
|
def _clean_weak_store_refs(self, weakref): |
193 |
|
"""Internal: Remove the weakref from the shapestores list""" |
194 |
|
self.shapestores = [store for store in self.shapestores |
195 |
|
if store is not weakref] |
196 |
|
|
197 |
|
def Tables(self): |
198 |
|
"""Return a list of all table objects open in the session |
199 |
|
|
200 |
|
The list includes all tables that are indirectly opened through |
201 |
|
shape stores and the tables that have been opened explicitly. |
202 |
|
""" |
203 |
|
return self.tables + [store.Table() for store in self.ShapeStores()] |
204 |
|
|
205 |
|
def AddTable(self, table): |
206 |
|
"""Add the table to the session |
207 |
|
|
208 |
|
All tables associated with the session that are not implicitly |
209 |
|
created by the OpenShapefile method (and maybe other Open* |
210 |
|
methods in the future) have to be passed to this method to make |
211 |
|
sure the session knows about it. The session keeps a reference |
212 |
|
to the table. Only tables managed by the session in this way |
213 |
|
should be used for layers contained in one of the session's |
214 |
|
maps. |
215 |
|
|
216 |
|
The table parameter may be any object implementing the table |
217 |
|
interface. If it's not already one of the transient tables |
218 |
|
instantiate an AutoTransientTable with it and use that instead |
219 |
|
of the original table (note that the AutoTransientTable keeps a |
220 |
|
reference to the original table). |
221 |
|
|
222 |
|
Return the table object actually used by the session. |
223 |
|
""" |
224 |
|
if not hasattr(table, "transient_table"): |
225 |
|
transient_table = AutoTransientTable(self.TransientDB(), table) |
226 |
|
else: |
227 |
|
transient_table = table |
228 |
|
self.tables.append(transient_table) |
229 |
|
return transient_table |
230 |
|
|
231 |
|
def RemoveTable(self, table): |
232 |
|
"""Remove the table from the session. |
233 |
|
|
234 |
|
The table object must be a table object previously returned by |
235 |
|
the AddTable method. If the table is not part of the session |
236 |
|
raise a ValueError. |
237 |
|
""" |
238 |
|
tables = [t for t in self.tables if t is not table] |
239 |
|
if len(tables) == len(self.tables): |
240 |
|
raise ValueError |
241 |
|
self.tables = tables |
242 |
|
|
243 |
|
def OpenTableFile(self, filename): |
244 |
|
"""Open the table file filename and return the table object. |
245 |
|
|
246 |
|
The filename argument must be the name of a DBF file. |
247 |
|
""" |
248 |
|
return self.AddTable(DBFTable(filename)) |
249 |
|
|
250 |
def temp_directory(self): |
def temp_directory(self): |
251 |
""" |
""" |
252 |
Return the name of the directory for session specific temporary files |
Return the name of the directory for session specific temporary files |
262 |
|
|
263 |
def OpenShapefile(self, filename): |
def OpenShapefile(self, filename): |
264 |
"""Return a shapefile store object for the data in the given file""" |
"""Return a shapefile store object for the data in the given file""" |
265 |
return ShapefileStore(self, filename) |
store = ShapefileStore(self, filename) |
266 |
|
self._add_shapestore(store) |
267 |
|
return store |
268 |
|
|
269 |
|
def AddShapeStore(self, shapestore): |
270 |
|
"""Add the shapestore to the session. |
271 |
|
|
272 |
|
The session only holds a weak reference to the shapestore, so it |
273 |
|
will automatically be removed from the session when the last |
274 |
|
reference goes away. |
275 |
|
""" |
276 |
|
self._add_shapestore(shapestore) |
277 |
|
return shapestore |
278 |
|
|
279 |
def TransientDB(self): |
def TransientDB(self): |
280 |
if self.transient_db is None: |
if self.transient_db is None: |
292 |
self.tables = [] |
self.tables = [] |
293 |
Modifiable.Destroy(self) |
Modifiable.Destroy(self) |
294 |
|
|
295 |
|
# Close the transient DB explicitly so that it removes any |
296 |
|
# journal files from the temporary directory |
297 |
|
if self.transient_db is not None: |
298 |
|
self.transient_db.close() |
299 |
|
|
300 |
def forward(self, *args): |
def forward(self, *args): |
301 |
"""Reissue events. |
"""Reissue events. |
302 |
|
|