/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/session.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/Model/session.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1124 by bh, Mon Jun 2 14:14:25 2003 UTC revision 1607 by bh, Tue Aug 19 12:50:35 2003 UTC
# Line 16  from messages import MAPS_CHANGED, EXTEN Line 16  from messages import MAPS_CHANGED, EXTEN
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       TABLE_REMOVED, DBCONN_ADDED, DBCONN_REMOVED
20    
21  from Thuban import _  from Thuban import _
22    
# Line 24  from base import TitledObject, Modifiabl Line 24  from base import TitledObject, Modifiabl
24  from map import Map  from map import Map
25  from data import ShapefileStore  from data import ShapefileStore
26  from table import DBFTable  from table import DBFTable
27    import postgisdb
28    
29  from transientdb import TransientDatabase, AutoTransientTable  from transientdb import TransientDatabase, AutoTransientTable
30    
# Line 133  class Session(TitledObject, Modifiable): Line 134  class Session(TitledObject, Modifiable):
134          self.tables = []          self.tables = []
135          self.shapestores = []          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    
# Line 201  class Session(TitledObject, Modifiable): Line 203  class Session(TitledObject, Modifiable):
203          The list includes all tables that are indirectly opened through          The list includes all tables that are indirectly opened through
204          shape stores and the tables that have been opened explicitly.          shape stores and the tables that have been opened explicitly.
205          """          """
206          return self.tables + [store.Table() for store in self.ShapeStores()]          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):      def UnreferencedTables(self):
218          """Return the tables that are not referenced by other data sources"""          """Return the tables that are not referenced by other data sources"""
# Line 256  class Session(TitledObject, Modifiable): Line 267  class Session(TitledObject, Modifiable):
267          self.tables = tables          self.tables = tables
268          self.changed(TABLE_REMOVED, table)          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):      def OpenTableFile(self, filename):
275          """Open the table file filename and return the table object.          """Open the table file filename and return the table object.
276    
# Line 301  class Session(TitledObject, Modifiable): Line 316  class Session(TitledObject, Modifiable):
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):
371            """Create and return a shapstore for a table in the database
372    
373            The db parameter must be a database connection previously passed
374            to AddDBConnection().
375            """
376            store = postgisdb.PostGISShapeStore(db, tablename)
377            self._add_shapestore(store)
378            return store
379    
380      def Destroy(self):      def Destroy(self):
381          for map in self.maps:          for map in self.maps:
382              map.Destroy()              map.Destroy()

Legend:
Removed from v.1124  
changed lines
  Added in v.1607

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26