1 |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
# Copyright (c) 2001, 2002, 2003, 2004 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# Jan-Oliver Wagner <[email protected]> |
# Jan-Oliver Wagner <[email protected]> |
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, DBCONN_ADDED, DBCONN_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 |
|
import postgisdb |
28 |
|
|
29 |
from transientdb import TransientDatabase |
from transientdb import TransientDatabase, AutoTransientTable |
30 |
|
|
31 |
class AutoRemoveFile: |
class AutoRemoveFile: |
32 |
|
|
132 |
self.filename = None |
self.filename = None |
133 |
self.maps = [] |
self.maps = [] |
134 |
self.tables = [] |
self.tables = [] |
135 |
|
self.shapestores = [] |
136 |
self.extensions = [] |
self.extensions = [] |
137 |
|
self.db_connections = [] |
138 |
self.temp_dir = None |
self.temp_dir = None |
139 |
self.transient_db = None |
self.transient_db = None |
140 |
|
|
183 |
extension.Subscribe(channel, self.forward, channel) |
extension.Subscribe(channel, self.forward, channel) |
184 |
self.changed(EXTENSIONS_CHANGED) |
self.changed(EXTENSIONS_CHANGED) |
185 |
|
|
186 |
|
def ShapeStores(self): |
187 |
|
"""Return a list of all ShapeStore objects open in the session""" |
188 |
|
return [store() for store in self.shapestores] |
189 |
|
|
190 |
|
def _add_shapestore(self, store): |
191 |
|
"""Internal: Add the shapestore to the list of shapestores""" |
192 |
|
self.shapestores.append(weakref.ref(store, |
193 |
|
self._clean_weak_store_refs)) |
194 |
|
|
195 |
|
def _clean_weak_store_refs(self, weakref): |
196 |
|
"""Internal: Remove the weakref from the shapestores list""" |
197 |
|
self.shapestores = [store for store in self.shapestores |
198 |
|
if store is not weakref] |
199 |
|
|
200 |
|
def Tables(self): |
201 |
|
"""Return a list of all table objects open in the session |
202 |
|
|
203 |
|
The list includes all tables that are indirectly opened through |
204 |
|
shape stores and the tables that have been opened explicitly. |
205 |
|
""" |
206 |
|
tables = self.tables[:] |
207 |
|
ids = {} |
208 |
|
for t in tables: |
209 |
|
ids[id(t)] = 1 |
210 |
|
for store in self.ShapeStores(): |
211 |
|
t = store.Table() |
212 |
|
if id(t) not in ids: |
213 |
|
ids[id(t)] = 1 |
214 |
|
tables.append(t) |
215 |
|
return tables |
216 |
|
|
217 |
|
def UnreferencedTables(self): |
218 |
|
"""Return the tables that are not referenced by other data sources""" |
219 |
|
known = {} |
220 |
|
for table in self.tables: |
221 |
|
known[id(table)] = 0 |
222 |
|
for table in self.tables + self.ShapeStores(): |
223 |
|
for dep in table.Dependencies(): |
224 |
|
known[id(dep)] = 1 |
225 |
|
return [table for table in self.tables if known[id(table)] == 0] |
226 |
|
|
227 |
|
def AddTable(self, table): |
228 |
|
"""Add the table to the session |
229 |
|
|
230 |
|
All tables associated with the session that are not implicitly |
231 |
|
created by the OpenShapefile method (and maybe other Open* |
232 |
|
methods in the future) have to be passed to this method to make |
233 |
|
sure the session knows about it. The session keeps a reference |
234 |
|
to the table. Only tables managed by the session in this way |
235 |
|
should be used for layers contained in one of the session's |
236 |
|
maps. |
237 |
|
|
238 |
|
The table parameter may be any object implementing the table |
239 |
|
interface. If it's not already one of the transient tables |
240 |
|
instantiate an AutoTransientTable with it and use that instead |
241 |
|
of the original table (note that the AutoTransientTable keeps a |
242 |
|
reference to the original table). |
243 |
|
|
244 |
|
Return the table object actually used by the session. |
245 |
|
""" |
246 |
|
if not hasattr(table, "transient_table"): |
247 |
|
transient_table = AutoTransientTable(self.TransientDB(), table) |
248 |
|
else: |
249 |
|
transient_table = table |
250 |
|
self.tables.append(transient_table) |
251 |
|
self.changed() |
252 |
|
return transient_table |
253 |
|
|
254 |
|
def RemoveTable(self, table): |
255 |
|
"""Remove the table from the session. |
256 |
|
|
257 |
|
The table object must be a table object previously returned by |
258 |
|
the AddTable method. If the table is not part of the session |
259 |
|
raise a ValueError. |
260 |
|
|
261 |
|
Issue a TABLE_REMOVED message after the table has been removed. |
262 |
|
The message has the removed table as the single parameter. |
263 |
|
""" |
264 |
|
tables = [t for t in self.tables if t is not table] |
265 |
|
if len(tables) == len(self.tables): |
266 |
|
raise ValueError |
267 |
|
self.tables = tables |
268 |
|
self.changed(TABLE_REMOVED, table) |
269 |
|
|
270 |
|
def DataContainers(self): |
271 |
|
"""Return all data containers, i.e. shapestores and tables""" |
272 |
|
return self.tables + self.ShapeStores() |
273 |
|
|
274 |
|
def OpenTableFile(self, filename): |
275 |
|
"""Open the table file filename and return the table object. |
276 |
|
|
277 |
|
The filename argument must be the name of a DBF file. |
278 |
|
""" |
279 |
|
return self.AddTable(DBFTable(filename)) |
280 |
|
|
281 |
def temp_directory(self): |
def temp_directory(self): |
282 |
""" |
""" |
283 |
Return the name of the directory for session specific temporary files |
Return the name of the directory for session specific temporary files |
293 |
|
|
294 |
def OpenShapefile(self, filename): |
def OpenShapefile(self, filename): |
295 |
"""Return a shapefile store object for the data in the given file""" |
"""Return a shapefile store object for the data in the given file""" |
296 |
return ShapefileStore(self, filename) |
store = ShapefileStore(self, filename) |
297 |
|
self._add_shapestore(store) |
298 |
|
return store |
299 |
|
|
300 |
|
def AddShapeStore(self, shapestore): |
301 |
|
"""Add the shapestore to the session. |
302 |
|
|
303 |
|
The session only holds a weak reference to the shapestore, so it |
304 |
|
will automatically be removed from the session when the last |
305 |
|
reference goes away. |
306 |
|
""" |
307 |
|
self._add_shapestore(shapestore) |
308 |
|
return shapestore |
309 |
|
|
310 |
def TransientDB(self): |
def TransientDB(self): |
311 |
if self.transient_db is None: |
if self.transient_db is None: |
316 |
self.temp_dir_remover) |
self.temp_dir_remover) |
317 |
return self.transient_db |
return self.transient_db |
318 |
|
|
319 |
|
def AddDBConnection(self, dbconn): |
320 |
|
"""Add the database connection dbconn to the session |
321 |
|
|
322 |
|
The argument should be an instance of PostGISConnection. |
323 |
|
""" |
324 |
|
self.db_connections.append(dbconn) |
325 |
|
self.changed(DBCONN_ADDED) |
326 |
|
|
327 |
|
def DBConnections(self): |
328 |
|
""" |
329 |
|
Return a list of all database connections registered with the session |
330 |
|
""" |
331 |
|
return self.db_connections |
332 |
|
|
333 |
|
def HasDBConnections(self): |
334 |
|
"""Return whether the session has open database connections""" |
335 |
|
return bool(self.db_connections) |
336 |
|
|
337 |
|
def CanRemoveDBConnection(self, dbconn): |
338 |
|
"""Return whether the database connections dbconn can be removed |
339 |
|
|
340 |
|
If can be removed if none of the shapestores or tables in the |
341 |
|
session references it. |
342 |
|
""" |
343 |
|
for store in self.ShapeStores(): |
344 |
|
if (isinstance(store, postgisdb.PostGISShapeStore) |
345 |
|
and store.db is dbconn): |
346 |
|
return 0 |
347 |
|
for table in self.Tables(): |
348 |
|
if (isinstance(table, postgisdb.PostGISTable) |
349 |
|
and table.db is dbconn): |
350 |
|
return 0 |
351 |
|
return 1 |
352 |
|
|
353 |
|
def RemoveDBConnection(self, dbconn): |
354 |
|
"""Remove the database connection from the session |
355 |
|
|
356 |
|
The parameter must be a connection that was registered |
357 |
|
previously by a AddDBConnection() call. |
358 |
|
""" |
359 |
|
if self.CanRemoveDBConnection(dbconn): |
360 |
|
remaining = [c for c in self.db_connections if c is not dbconn] |
361 |
|
if len(remaining) < len(self.db_connections): |
362 |
|
self.db_connections = remaining |
363 |
|
self.changed(DBCONN_REMOVED) |
364 |
|
else: |
365 |
|
raise ValueError("DBConection %r is not registered" |
366 |
|
" with session %r" % (dbconn, self)) |
367 |
|
else: |
368 |
|
raise ValueError("DBConnection %r is still in use" % (dbconn,)) |
369 |
|
|
370 |
|
def OpenDBShapeStore(self, db, tablename, id_column = None, |
371 |
|
geometry_column = None): |
372 |
|
"""Create and return a shapstore for a table in the database |
373 |
|
|
374 |
|
The db parameter must be a database connection previously passed |
375 |
|
to AddDBConnection(). |
376 |
|
""" |
377 |
|
store = postgisdb.PostGISShapeStore(db, tablename, |
378 |
|
id_column = id_column, |
379 |
|
geometry_column = geometry_column) |
380 |
|
self._add_shapestore(store) |
381 |
|
return store |
382 |
|
|
383 |
def Destroy(self): |
def Destroy(self): |
384 |
for map in self.maps: |
for map in self.maps: |
385 |
map.Destroy() |
map.Destroy() |