/[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 582 by jonathan, Tue Apr 1 10:22:01 2003 UTC revision 1124 by bh, Mon Jun 2 14:14:25 2003 UTC
# Line 1  Line 1 
1  # Copyright (c) 2001, 2002 by Intevation GmbH  # Copyright (c) 2001, 2002, 2003 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]>
# Line 8  Line 8 
8    
9  __version__ = "$Revision$"  __version__ = "$Revision$"
10    
11  from Thuban.Lib.connector import Publisher  import os
12    from tempfile import mktemp
13    import weakref
14    
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
26    from table import DBFTable
27    
28    from transientdb import TransientDatabase, AutoTransientTable
29    
30    class AutoRemoveFile:
31    
32        """Remove a file once all references go away."""
33    
34        def __init__(self, filename, tempdir = None):
35            """Initialize the AutoRemoveFile
36    
37            Parameters:
38               filename -- The name of the file to remove in __del__
39               tempdir -- Another object simple stored as an instance variable.
40    
41            As the name suggests the tempdir parameter is intended for a
42            temporary directory the file might be located in. The intended
43            use is that it's an instance of AutoRemoveDir.
44            """
45            self.filename = filename
46            self.tempdir = tempdir
47    
48        def __del__(self, remove = os.remove):
49            remove(self.filename)
50    
51    class AutoRemoveDir:
52    
53        """Remove a directory once all references go away
54    
55        The intended use of this class together with AutoRemoveFile is for
56        temporary directories and files containd therein. An AutoRemoveDir
57        should be instantiated for the directory and passed as the tempdir
58        parameter to every AutoRemoveFile instance created for files in the
59        directory. An AutoRemoveFile shold be instantiated for every file
60        created in the directory so that the directory is automatically
61        removed once the last file is removed.
62        """
63    
64        def __init__(self, filename):
65            """Initialize the AutoRemoveDir
66    
67            The parameter is the name of the directory.
68            """
69            self.filename = filename
70    
71        def __del__(self, rmdir = os.rmdir):
72            rmdir(self.filename)
73    
74    
75    # WeakKey dictionary mapping objects like the transient_db to
76    # AutoRemoveDir or AutoRemoveFile instances to make sure that the
77    # temporary files and the directory are deleted but not before the
78    # objects that use them go away.
79    auto_remover = weakref.WeakKeyDictionary()
80    
81  class Session(TitledObject, Modifiable):  class Session(TitledObject, Modifiable):
82    
# Line 75  class Session(TitledObject, Modifiable): Line 131  class Session(TitledObject, Modifiable):
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
137            self.transient_db = None
138    
139      def changed(self, channel = None, *args):      def changed(self, channel = None, *args):
140          """Like the inherited version but issue a CHANGED message as well.          """Like the inherited version but issue a CHANGED message as well.
# Line 122  class Session(TitledObject, Modifiable): Line 181  class Session(TitledObject, Modifiable):
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            return self.tables + [store.Table() for store in self.ShapeStores()]
205    
206        def UnreferencedTables(self):
207            """Return the tables that are not referenced by other data sources"""
208            known = {}
209            for table in self.tables:
210                known[id(table)] = 0
211            for table in self.tables + self.ShapeStores():
212                for dep in table.Dependencies():
213                    known[id(dep)] = 1
214            return [table for table in self.tables if known[id(table)] == 0]
215    
216        def AddTable(self, table):
217            """Add the table to the session
218    
219            All tables associated with the session that are not implicitly
220            created by the OpenShapefile method (and maybe other Open*
221            methods in the future) have to be passed to this method to make
222            sure the session knows about it. The session keeps a reference
223            to the table. Only tables managed by the session in this way
224            should be used for layers contained in one of the session's
225            maps.
226    
227            The table parameter may be any object implementing the table
228            interface. If it's not already one of the transient tables
229            instantiate an AutoTransientTable with it and use that instead
230            of the original table (note that the AutoTransientTable keeps a
231            reference to the original table).
232    
233            Return the table object actually used by the session.
234            """
235            if not hasattr(table, "transient_table"):
236                transient_table = AutoTransientTable(self.TransientDB(), table)
237            else:
238                transient_table = table
239            self.tables.append(transient_table)
240            self.changed()
241            return transient_table
242    
243        def RemoveTable(self, table):
244            """Remove the table from the session.
245    
246            The table object must be a table object previously returned by
247            the AddTable method. If the table is not part of the session
248            raise a ValueError.
249    
250            Issue a TABLE_REMOVED message after the table has been removed.
251            The message has the removed table as the single parameter.
252            """
253            tables = [t for t in self.tables if t is not table]
254            if len(tables) == len(self.tables):
255                raise ValueError
256            self.tables = tables
257            self.changed(TABLE_REMOVED, table)
258    
259        def OpenTableFile(self, filename):
260            """Open the table file filename and return the table object.
261    
262            The filename argument must be the name of a DBF file.
263            """
264            return self.AddTable(DBFTable(filename))
265    
266        def temp_directory(self):
267            """
268            Return the name of the directory for session specific temporary files
269    
270            Create the directory if it doesn't exist yet.
271            """
272            if self.temp_dir is None:
273                temp_dir = mktemp()
274                os.mkdir(temp_dir, 0700)
275                self.temp_dir = temp_dir
276                self.temp_dir_remover = AutoRemoveDir(self.temp_dir)
277            return self.temp_dir
278    
279        def OpenShapefile(self, filename):
280            """Return a shapefile store object for the data in the given file"""
281            store = ShapefileStore(self, filename)
282            self._add_shapestore(store)
283            return store
284    
285        def AddShapeStore(self, shapestore):
286            """Add the shapestore to the session.
287    
288            The session only holds a weak reference to the shapestore, so it
289            will automatically be removed from the session when the last
290            reference goes away.
291            """
292            self._add_shapestore(shapestore)
293            return shapestore
294    
295        def TransientDB(self):
296            if self.transient_db is None:
297                filename = os.path.join(self.temp_directory(), "transientdb")
298                self.transient_db = TransientDatabase(filename)
299                #print self.temp_dir_remover
300                auto_remover[self.transient_db] = AutoRemoveFile(filename,
301                                                            self.temp_dir_remover)
302            return self.transient_db
303    
304      def Destroy(self):      def Destroy(self):
305          for map in self.maps:          for map in self.maps:
306              map.Destroy()              map.Destroy()
# Line 129  class Session(TitledObject, Modifiable): Line 308  class Session(TitledObject, Modifiable):
308          self.tables = []          self.tables = []
309          Modifiable.Destroy(self)          Modifiable.Destroy(self)
310    
311            # Close the transient DB explicitly so that it removes any
312            # journal files from the temporary directory
313            if self.transient_db is not None:
314                self.transient_db.close()
315    
316      def forward(self, *args):      def forward(self, *args):
317          """Reissue events.          """Reissue events.
318    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26