15 |
from messages import MAPS_CHANGED, EXTENSIONS_CHANGED, FILENAME_CHANGED, \ |
from messages import MAPS_CHANGED, EXTENSIONS_CHANGED, FILENAME_CHANGED, \ |
16 |
MAP_LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \ |
MAP_LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \ |
17 |
LAYER_CHANGED, LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED,\ |
LAYER_CHANGED, LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED,\ |
18 |
EXTENSION_CHANGED, EXTENSION_OBJECTS_CHANGED, CHANGED |
EXTENSION_CHANGED, EXTENSION_OBJECTS_CHANGED, CHANGED, \ |
19 |
|
TABLE_REMOVED |
20 |
|
|
21 |
from Thuban import _ |
from Thuban import _ |
22 |
|
|
23 |
from base import TitledObject, Modifiable |
from base import TitledObject, Modifiable |
24 |
from map import Map |
from map import Map |
25 |
from data import ShapefileStore |
from data import ShapefileStore |
26 |
|
from table import DBFTable |
27 |
|
|
28 |
from transientdb import TransientDatabase |
from transientdb import TransientDatabase, AutoTransientTable |
29 |
|
|
30 |
class AutoRemoveFile: |
class AutoRemoveFile: |
31 |
|
|
131 |
self.filename = None |
self.filename = None |
132 |
self.maps = [] |
self.maps = [] |
133 |
self.tables = [] |
self.tables = [] |
134 |
|
self.shapestores = [] |
135 |
self.extensions = [] |
self.extensions = [] |
136 |
self.temp_dir = None |
self.temp_dir = None |
137 |
self.transient_db = None |
self.transient_db = None |
181 |
extension.Subscribe(channel, self.forward, channel) |
extension.Subscribe(channel, self.forward, channel) |
182 |
self.changed(EXTENSIONS_CHANGED) |
self.changed(EXTENSIONS_CHANGED) |
183 |
|
|
184 |
|
def ShapeStores(self): |
185 |
|
"""Return a list of all ShapeStore objects open in the session""" |
186 |
|
return [store() for store in self.shapestores] |
187 |
|
|
188 |
|
def _add_shapestore(self, store): |
189 |
|
"""Internal: Add the shapestore to the list of shapestores""" |
190 |
|
self.shapestores.append(weakref.ref(store, |
191 |
|
self._clean_weak_store_refs)) |
192 |
|
|
193 |
|
def _clean_weak_store_refs(self, weakref): |
194 |
|
"""Internal: Remove the weakref from the shapestores list""" |
195 |
|
self.shapestores = [store for store in self.shapestores |
196 |
|
if store is not weakref] |
197 |
|
|
198 |
|
def Tables(self): |
199 |
|
"""Return a list of all table objects open in the session |
200 |
|
|
201 |
|
The list includes all tables that are indirectly opened through |
202 |
|
shape stores and the tables that have been opened explicitly. |
203 |
|
""" |
204 |
|
tables = self.tables[:] |
205 |
|
ids = {} |
206 |
|
for t in tables: |
207 |
|
ids[id(t)] = 1 |
208 |
|
for store in self.ShapeStores(): |
209 |
|
t = store.Table() |
210 |
|
if id(t) not in ids: |
211 |
|
ids[id(t)] = 1 |
212 |
|
tables.append(t) |
213 |
|
return tables |
214 |
|
|
215 |
|
def UnreferencedTables(self): |
216 |
|
"""Return the tables that are not referenced by other data sources""" |
217 |
|
known = {} |
218 |
|
for table in self.tables: |
219 |
|
known[id(table)] = 0 |
220 |
|
for table in self.tables + self.ShapeStores(): |
221 |
|
for dep in table.Dependencies(): |
222 |
|
known[id(dep)] = 1 |
223 |
|
return [table for table in self.tables if known[id(table)] == 0] |
224 |
|
|
225 |
|
def AddTable(self, table): |
226 |
|
"""Add the table to the session |
227 |
|
|
228 |
|
All tables associated with the session that are not implicitly |
229 |
|
created by the OpenShapefile method (and maybe other Open* |
230 |
|
methods in the future) have to be passed to this method to make |
231 |
|
sure the session knows about it. The session keeps a reference |
232 |
|
to the table. Only tables managed by the session in this way |
233 |
|
should be used for layers contained in one of the session's |
234 |
|
maps. |
235 |
|
|
236 |
|
The table parameter may be any object implementing the table |
237 |
|
interface. If it's not already one of the transient tables |
238 |
|
instantiate an AutoTransientTable with it and use that instead |
239 |
|
of the original table (note that the AutoTransientTable keeps a |
240 |
|
reference to the original table). |
241 |
|
|
242 |
|
Return the table object actually used by the session. |
243 |
|
""" |
244 |
|
if not hasattr(table, "transient_table"): |
245 |
|
transient_table = AutoTransientTable(self.TransientDB(), table) |
246 |
|
else: |
247 |
|
transient_table = table |
248 |
|
self.tables.append(transient_table) |
249 |
|
self.changed() |
250 |
|
return transient_table |
251 |
|
|
252 |
|
def RemoveTable(self, table): |
253 |
|
"""Remove the table from the session. |
254 |
|
|
255 |
|
The table object must be a table object previously returned by |
256 |
|
the AddTable method. If the table is not part of the session |
257 |
|
raise a ValueError. |
258 |
|
|
259 |
|
Issue a TABLE_REMOVED message after the table has been removed. |
260 |
|
The message has the removed table as the single parameter. |
261 |
|
""" |
262 |
|
tables = [t for t in self.tables if t is not table] |
263 |
|
if len(tables) == len(self.tables): |
264 |
|
raise ValueError |
265 |
|
self.tables = tables |
266 |
|
self.changed(TABLE_REMOVED, table) |
267 |
|
|
268 |
|
def DataContainers(self): |
269 |
|
"""Return all data containers, i.e. shapestores and tables""" |
270 |
|
return self.tables + self.ShapeStores() |
271 |
|
|
272 |
|
def OpenTableFile(self, filename): |
273 |
|
"""Open the table file filename and return the table object. |
274 |
|
|
275 |
|
The filename argument must be the name of a DBF file. |
276 |
|
""" |
277 |
|
return self.AddTable(DBFTable(filename)) |
278 |
|
|
279 |
def temp_directory(self): |
def temp_directory(self): |
280 |
""" |
""" |
281 |
Return the name of the directory for session specific temporary files |
Return the name of the directory for session specific temporary files |
291 |
|
|
292 |
def OpenShapefile(self, filename): |
def OpenShapefile(self, filename): |
293 |
"""Return a shapefile store object for the data in the given file""" |
"""Return a shapefile store object for the data in the given file""" |
294 |
return ShapefileStore(self, filename) |
store = ShapefileStore(self, filename) |
295 |
|
self._add_shapestore(store) |
296 |
|
return store |
297 |
|
|
298 |
|
def AddShapeStore(self, shapestore): |
299 |
|
"""Add the shapestore to the session. |
300 |
|
|
301 |
|
The session only holds a weak reference to the shapestore, so it |
302 |
|
will automatically be removed from the session when the last |
303 |
|
reference goes away. |
304 |
|
""" |
305 |
|
self._add_shapestore(shapestore) |
306 |
|
return shapestore |
307 |
|
|
308 |
def TransientDB(self): |
def TransientDB(self): |
309 |
if self.transient_db is None: |
if self.transient_db is None: |