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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1282 - (hide annotations)
Mon Jun 23 09:47:18 2003 UTC (21 years, 8 months ago) by bh
Original Path: trunk/thuban/Thuban/Model/session.py
File MIME type: text/x-python
File size: 13096 byte(s)
Bug fix for RT #1961:

* Thuban/Model/load.py (SessionLoader.start_derivedshapesource):
Register DerivedShapestores with the session

* Thuban/Model/session.py (Session.Tables): Make sure each table
is only listed once.

* test/test_load.py (TestJoinedTable.test): Add check_format call.
Update file contents to match the one written out.

1 bh 723 # Copyright (c) 2001, 2002, 2003 by Intevation GmbH
2 bh 6 # Authors:
3     # Bernhard Herzog <[email protected]>
4 jan 197 # Jan-Oliver Wagner <[email protected]>
5 bh 6 #
6     # This program is free software under the GPL (>=v2)
7     # Read the file COPYING coming with Thuban for details.
8    
9     __version__ = "$Revision$"
10    
11 bh 765 import os
12     from tempfile import mktemp
13     import weakref
14    
15 jan 197 from messages import MAPS_CHANGED, EXTENSIONS_CHANGED, FILENAME_CHANGED, \
16 jonathan 548 MAP_LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \
17 jonathan 582 LAYER_CHANGED, LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED,\
18 bh 1068 EXTENSION_CHANGED, EXTENSION_OBJECTS_CHANGED, CHANGED, \
19     TABLE_REMOVED
20 bh 6
21 jan 374 from Thuban import _
22    
23 bh 6 from base import TitledObject, Modifiable
24     from map import Map
25 bh 723 from data import ShapefileStore
26 bh 1036 from table import DBFTable
27 bh 6
28 bh 982 from transientdb import TransientDatabase, AutoTransientTable
29 bh 6
30 bh 765 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 bh 6 class Session(TitledObject, Modifiable):
82    
83     """A complete session.
84    
85 jan 197 A Session consists of arbitrary numbers of maps, tables and extensions
86 bh 6
87     Session objects send the following events:
88    
89     TITLE_CHANGED -- The title has changed. Parameters: the session.
90    
91     FILENAME_CHANGED -- The filename has changed. No parameters.
92    
93     MAPS_CHANGED -- Maps were added, removed.
94    
95 jan 197 EXTENSIONS_CHANGED -- Extensions were added, removed.
96    
97 jonathan 548 MAP_LAYERS_CHANGED -- Same as the map's event of the same name.
98 bh 6 It's simply resent from the session to make
99     subscriptions easier.
100 bh 232
101     CHANGED -- Generic changed event. Parameters: the session. The
102     event is always issued when any other changed event
103     is issused. This is useful for code that needs to be
104     notified whenever something in the session has
105     changed but it's too cumbersome or error-prone to
106     subscribe to all the individual events.
107 bh 6 """
108    
109 bh 128 # message channels that have to be forwarded from maps contained in
110     # the session.
111     forwarded_channels = (
112 bh 318 # generic channels
113     CHANGED,
114    
115 bh 128 # map specific channels
116     MAP_PROJECTION_CHANGED,
117 jonathan 548 MAP_LAYERS_CHANGED,
118 bh 128
119     # layer channels forwarded by the map
120     LAYER_PROJECTION_CHANGED,
121 jonathan 582 LAYER_CHANGED,
122 jan 197 LAYER_VISIBILITY_CHANGED,
123 bh 128
124 jan 197 # channels forwarded by an extension
125     EXTENSION_CHANGED,
126     EXTENSION_OBJECTS_CHANGED)
127    
128 bh 6 def __init__(self, title):
129     TitledObject.__init__(self, title)
130     Modifiable.__init__(self)
131     self.filename = None
132     self.maps = []
133     self.tables = []
134 bh 851 self.shapestores = []
135 jan 197 self.extensions = []
136 bh 765 self.temp_dir = None
137     self.transient_db = None
138 bh 6
139 bh 232 def changed(self, channel = None, *args):
140     """Like the inherited version but issue a CHANGED message as well.
141    
142     The CHANGED message is only issued if channel given is a
143     different channel than CHANGED.
144     """
145     Modifiable.changed(self, channel, *args)
146     if channel != CHANGED:
147     self.issue(CHANGED, self)
148    
149 bh 6 def SetFilename(self, filename):
150     self.filename = filename
151     self.changed(FILENAME_CHANGED)
152    
153     def Maps(self):
154     return self.maps
155    
156     def HasMaps(self):
157     return len(self.maps) > 0
158    
159     def AddMap(self, map):
160     self.maps.append(map)
161 bh 128 for channel in self.forwarded_channels:
162 bh 6 map.Subscribe(channel, self.forward, channel)
163     self.changed(MAPS_CHANGED)
164    
165 bh 241 def RemoveMap(self, map):
166     for channel in self.forwarded_channels:
167     map.Unsubscribe(channel, self.forward, channel)
168     self.maps.remove(map)
169     self.changed(MAPS_CHANGED)
170     map.Destroy()
171    
172 jan 197 def Extensions(self):
173     return self.extensions
174    
175     def HasExtensions(self):
176     return len(self.extensions) > 0
177    
178     def AddExtension(self, extension):
179     self.extensions.append(extension)
180     for channel in self.forwarded_channels:
181     extension.Subscribe(channel, self.forward, channel)
182     self.changed(EXTENSIONS_CHANGED)
183    
184 bh 851 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 bh 982 """Return a list of all table objects open in the session
200 bh 851
201 bh 982 The list includes all tables that are indirectly opened through
202     shape stores and the tables that have been opened explicitly.
203     """
204 bh 1282 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 bh 982
215 bh 1068 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 bh 982 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 bh 1124 self.changed()
250 bh 982 return transient_table
251    
252 bh 987 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 bh 1068
259     Issue a TABLE_REMOVED message after the table has been removed.
260     The message has the removed table as the single parameter.
261 bh 987 """
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 bh 1068 self.changed(TABLE_REMOVED, table)
267 bh 987
268 bh 1268 def DataContainers(self):
269     """Return all data containers, i.e. shapestores and tables"""
270     return self.tables + self.ShapeStores()
271    
272 bh 1036 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 bh 765 def temp_directory(self):
280     """
281     Return the name of the directory for session specific temporary files
282    
283     Create the directory if it doesn't exist yet.
284     """
285     if self.temp_dir is None:
286     temp_dir = mktemp()
287     os.mkdir(temp_dir, 0700)
288     self.temp_dir = temp_dir
289     self.temp_dir_remover = AutoRemoveDir(self.temp_dir)
290     return self.temp_dir
291    
292 bh 723 def OpenShapefile(self, filename):
293     """Return a shapefile store object for the data in the given file"""
294 bh 851 store = ShapefileStore(self, filename)
295     self._add_shapestore(store)
296     return store
297 bh 723
298 bh 1016 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 bh 765 def TransientDB(self):
309     if self.transient_db is None:
310     filename = os.path.join(self.temp_directory(), "transientdb")
311     self.transient_db = TransientDatabase(filename)
312     #print self.temp_dir_remover
313     auto_remover[self.transient_db] = AutoRemoveFile(filename,
314     self.temp_dir_remover)
315     return self.transient_db
316    
317 bh 6 def Destroy(self):
318     for map in self.maps:
319     map.Destroy()
320     self.maps = []
321     self.tables = []
322 bh 250 Modifiable.Destroy(self)
323 bh 6
324 bh 778 # Close the transient DB explicitly so that it removes any
325     # journal files from the temporary directory
326     if self.transient_db is not None:
327     self.transient_db.close()
328    
329 bh 6 def forward(self, *args):
330 bh 232 """Reissue events.
331    
332     If the channel the event is forwarded to is a changed-channel
333     that is not the CHANGED channel issue CHANGED as well. An
334     channel is considered to be a changed-channel if it's name ends
335     with 'CHANGED'.
336     """
337 bh 6 if len(args) > 1:
338     args = (args[-1],) + args[:-1]
339     apply(self.issue, args)
340 bh 232 channel = args[0]
341     # It's a bit of a kludge to rely on the channel name for this.
342     if channel.endswith("CHANGED") and channel != CHANGED:
343     self.issue(CHANGED, self)
344 bh 6
345     def WasModified(self):
346     """Return true if the session or one of the maps was modified"""
347     if self.modified:
348     return 1
349     else:
350     for map in self.maps:
351     if map.WasModified():
352     return 1
353     return 0
354    
355     def UnsetModified(self):
356     """Unset the modified flag of the session and the maps"""
357     Modifiable.UnsetModified(self)
358     for map in self.maps:
359     map.UnsetModified()
360    
361 bh 217 def TreeInfo(self):
362     items = []
363     if self.filename is None:
364 jan 374 items.append(_("Filename:"))
365 bh 217 else:
366 jan 374 items.append(_("Filename: %s") % self.filename)
367 bh 6
368 bh 217 if self.WasModified():
369 jan 374 items.append(_("Modified"))
370 bh 217 else:
371 jan 374 items.append(_("Unmodified"))
372 bh 6
373 bh 217 items.extend(self.maps)
374     items.extend(self.extensions)
375    
376 jan 374 return (_("Session: %s") % self.title, items)
377 bh 217
378    
379 bh 6 def create_empty_session():
380     """Return an empty session useful as a starting point"""
381 jan 103 import os
382 jan 374 session = Session(_('unnamed session'))
383 jan 103 session.SetFilename(None)
384 jan 374 session.AddMap(Map(_('unnamed map')))
385 bh 56 session.UnsetModified()
386 bh 6 return session

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26