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