/[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 1036 - (hide annotations)
Mon May 26 17:30:29 2003 UTC (21 years, 9 months ago) by bh
Original Path: trunk/thuban/Thuban/Model/session.py
File MIME type: text/x-python
File size: 12098 byte(s)
(Session.OpenTableFile): New. Open a dbf
file and add the table to the tables managed by the session

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 232 EXTENSION_CHANGED, EXTENSION_OBJECTS_CHANGED, CHANGED
19 bh 6
20 jan 374 from Thuban import _
21    
22 bh 6 from base import TitledObject, Modifiable
23     from map import Map
24 bh 723 from data import ShapefileStore
25 bh 1036 from table import DBFTable
26 bh 6
27 bh 982 from transientdb import TransientDatabase, AutoTransientTable
28 bh 6
29 bh 765 class AutoRemoveFile:
30    
31     """Remove a file once all references go away."""
32    
33     def __init__(self, filename, tempdir = None):
34     """Initialize the AutoRemoveFile
35    
36     Parameters:
37     filename -- The name of the file to remove in __del__
38     tempdir -- Another object simple stored as an instance variable.
39    
40     As the name suggests the tempdir parameter is intended for a
41     temporary directory the file might be located in. The intended
42     use is that it's an instance of AutoRemoveDir.
43     """
44     self.filename = filename
45     self.tempdir = tempdir
46    
47     def __del__(self, remove = os.remove):
48     remove(self.filename)
49    
50     class AutoRemoveDir:
51    
52     """Remove a directory once all references go away
53    
54     The intended use of this class together with AutoRemoveFile is for
55     temporary directories and files containd therein. An AutoRemoveDir
56     should be instantiated for the directory and passed as the tempdir
57     parameter to every AutoRemoveFile instance created for files in the
58     directory. An AutoRemoveFile shold be instantiated for every file
59     created in the directory so that the directory is automatically
60     removed once the last file is removed.
61     """
62    
63     def __init__(self, filename):
64     """Initialize the AutoRemoveDir
65    
66     The parameter is the name of the directory.
67     """
68     self.filename = filename
69    
70     def __del__(self, rmdir = os.rmdir):
71     rmdir(self.filename)
72    
73    
74     # WeakKey dictionary mapping objects like the transient_db to
75     # AutoRemoveDir or AutoRemoveFile instances to make sure that the
76     # temporary files and the directory are deleted but not before the
77     # objects that use them go away.
78     auto_remover = weakref.WeakKeyDictionary()
79    
80 bh 6 class Session(TitledObject, Modifiable):
81    
82     """A complete session.
83    
84 jan 197 A Session consists of arbitrary numbers of maps, tables and extensions
85 bh 6
86     Session objects send the following events:
87    
88     TITLE_CHANGED -- The title has changed. Parameters: the session.
89    
90     FILENAME_CHANGED -- The filename has changed. No parameters.
91    
92     MAPS_CHANGED -- Maps were added, removed.
93    
94 jan 197 EXTENSIONS_CHANGED -- Extensions were added, removed.
95    
96 jonathan 548 MAP_LAYERS_CHANGED -- Same as the map's event of the same name.
97 bh 6 It's simply resent from the session to make
98     subscriptions easier.
99 bh 232
100     CHANGED -- Generic changed event. Parameters: the session. The
101     event is always issued when any other changed event
102     is issused. This is useful for code that needs to be
103     notified whenever something in the session has
104     changed but it's too cumbersome or error-prone to
105     subscribe to all the individual events.
106 bh 6 """
107    
108 bh 128 # message channels that have to be forwarded from maps contained in
109     # the session.
110     forwarded_channels = (
111 bh 318 # generic channels
112     CHANGED,
113    
114 bh 128 # map specific channels
115     MAP_PROJECTION_CHANGED,
116 jonathan 548 MAP_LAYERS_CHANGED,
117 bh 128
118     # layer channels forwarded by the map
119     LAYER_PROJECTION_CHANGED,
120 jonathan 582 LAYER_CHANGED,
121 jan 197 LAYER_VISIBILITY_CHANGED,
122 bh 128
123 jan 197 # channels forwarded by an extension
124     EXTENSION_CHANGED,
125     EXTENSION_OBJECTS_CHANGED)
126    
127 bh 6 def __init__(self, title):
128     TitledObject.__init__(self, title)
129     Modifiable.__init__(self)
130     self.filename = None
131     self.maps = []
132     self.tables = []
133 bh 851 self.shapestores = []
134 jan 197 self.extensions = []
135 bh 765 self.temp_dir = None
136     self.transient_db = None
137 bh 6
138 bh 232 def changed(self, channel = None, *args):
139     """Like the inherited version but issue a CHANGED message as well.
140    
141     The CHANGED message is only issued if channel given is a
142     different channel than CHANGED.
143     """
144     Modifiable.changed(self, channel, *args)
145     if channel != CHANGED:
146     self.issue(CHANGED, self)
147    
148 bh 6 def SetFilename(self, filename):
149     self.filename = filename
150     self.changed(FILENAME_CHANGED)
151    
152     def Maps(self):
153     return self.maps
154    
155     def HasMaps(self):
156     return len(self.maps) > 0
157    
158     def AddMap(self, map):
159     self.maps.append(map)
160 bh 128 for channel in self.forwarded_channels:
161 bh 6 map.Subscribe(channel, self.forward, channel)
162     self.changed(MAPS_CHANGED)
163    
164 bh 241 def RemoveMap(self, map):
165     for channel in self.forwarded_channels:
166     map.Unsubscribe(channel, self.forward, channel)
167     self.maps.remove(map)
168     self.changed(MAPS_CHANGED)
169     map.Destroy()
170    
171 jan 197 def Extensions(self):
172     return self.extensions
173    
174     def HasExtensions(self):
175     return len(self.extensions) > 0
176    
177     def AddExtension(self, extension):
178     self.extensions.append(extension)
179     for channel in self.forwarded_channels:
180     extension.Subscribe(channel, self.forward, channel)
181     self.changed(EXTENSIONS_CHANGED)
182    
183 bh 851 def ShapeStores(self):
184     """Return a list of all ShapeStore objects open in the session"""
185     return [store() for store in self.shapestores]
186    
187     def _add_shapestore(self, store):
188     """Internal: Add the shapestore to the list of shapestores"""
189     self.shapestores.append(weakref.ref(store,
190     self._clean_weak_store_refs))
191    
192     def _clean_weak_store_refs(self, weakref):
193     """Internal: Remove the weakref from the shapestores list"""
194     self.shapestores = [store for store in self.shapestores
195     if store is not weakref]
196    
197     def Tables(self):
198 bh 982 """Return a list of all table objects open in the session
199 bh 851
200 bh 982 The list includes all tables that are indirectly opened through
201     shape stores and the tables that have been opened explicitly.
202     """
203     return self.tables + [store.Table() for store in self.ShapeStores()]
204    
205     def AddTable(self, table):
206     """Add the table to the session
207    
208     All tables associated with the session that are not implicitly
209     created by the OpenShapefile method (and maybe other Open*
210     methods in the future) have to be passed to this method to make
211     sure the session knows about it. The session keeps a reference
212     to the table. Only tables managed by the session in this way
213     should be used for layers contained in one of the session's
214     maps.
215    
216     The table parameter may be any object implementing the table
217     interface. If it's not already one of the transient tables
218     instantiate an AutoTransientTable with it and use that instead
219     of the original table (note that the AutoTransientTable keeps a
220     reference to the original table).
221    
222     Return the table object actually used by the session.
223     """
224     if not hasattr(table, "transient_table"):
225     transient_table = AutoTransientTable(self.TransientDB(), table)
226     else:
227     transient_table = table
228     self.tables.append(transient_table)
229     return transient_table
230    
231 bh 987 def RemoveTable(self, table):
232     """Remove the table from the session.
233    
234     The table object must be a table object previously returned by
235     the AddTable method. If the table is not part of the session
236     raise a ValueError.
237     """
238     tables = [t for t in self.tables if t is not table]
239     if len(tables) == len(self.tables):
240     raise ValueError
241     self.tables = tables
242    
243 bh 1036 def OpenTableFile(self, filename):
244     """Open the table file filename and return the table object.
245    
246     The filename argument must be the name of a DBF file.
247     """
248     return self.AddTable(DBFTable(filename))
249    
250 bh 765 def temp_directory(self):
251     """
252     Return the name of the directory for session specific temporary files
253    
254     Create the directory if it doesn't exist yet.
255     """
256     if self.temp_dir is None:
257     temp_dir = mktemp()
258     os.mkdir(temp_dir, 0700)
259     self.temp_dir = temp_dir
260     self.temp_dir_remover = AutoRemoveDir(self.temp_dir)
261     return self.temp_dir
262    
263 bh 723 def OpenShapefile(self, filename):
264     """Return a shapefile store object for the data in the given file"""
265 bh 851 store = ShapefileStore(self, filename)
266     self._add_shapestore(store)
267     return store
268 bh 723
269 bh 1016 def AddShapeStore(self, shapestore):
270     """Add the shapestore to the session.
271    
272     The session only holds a weak reference to the shapestore, so it
273     will automatically be removed from the session when the last
274     reference goes away.
275     """
276     self._add_shapestore(shapestore)
277     return shapestore
278    
279 bh 765 def TransientDB(self):
280     if self.transient_db is None:
281     filename = os.path.join(self.temp_directory(), "transientdb")
282     self.transient_db = TransientDatabase(filename)
283     #print self.temp_dir_remover
284     auto_remover[self.transient_db] = AutoRemoveFile(filename,
285     self.temp_dir_remover)
286     return self.transient_db
287    
288 bh 6 def Destroy(self):
289     for map in self.maps:
290     map.Destroy()
291     self.maps = []
292     self.tables = []
293 bh 250 Modifiable.Destroy(self)
294 bh 6
295 bh 778 # Close the transient DB explicitly so that it removes any
296     # journal files from the temporary directory
297     if self.transient_db is not None:
298     self.transient_db.close()
299    
300 bh 6 def forward(self, *args):
301 bh 232 """Reissue events.
302    
303     If the channel the event is forwarded to is a changed-channel
304     that is not the CHANGED channel issue CHANGED as well. An
305     channel is considered to be a changed-channel if it's name ends
306     with 'CHANGED'.
307     """
308 bh 6 if len(args) > 1:
309     args = (args[-1],) + args[:-1]
310     apply(self.issue, args)
311 bh 232 channel = args[0]
312     # It's a bit of a kludge to rely on the channel name for this.
313     if channel.endswith("CHANGED") and channel != CHANGED:
314     self.issue(CHANGED, self)
315 bh 6
316     def WasModified(self):
317     """Return true if the session or one of the maps was modified"""
318     if self.modified:
319     return 1
320     else:
321     for map in self.maps:
322     if map.WasModified():
323     return 1
324     return 0
325    
326     def UnsetModified(self):
327     """Unset the modified flag of the session and the maps"""
328     Modifiable.UnsetModified(self)
329     for map in self.maps:
330     map.UnsetModified()
331    
332 bh 217 def TreeInfo(self):
333     items = []
334     if self.filename is None:
335 jan 374 items.append(_("Filename:"))
336 bh 217 else:
337 jan 374 items.append(_("Filename: %s") % self.filename)
338 bh 6
339 bh 217 if self.WasModified():
340 jan 374 items.append(_("Modified"))
341 bh 217 else:
342 jan 374 items.append(_("Unmodified"))
343 bh 6
344 bh 217 items.extend(self.maps)
345     items.extend(self.extensions)
346    
347 jan 374 return (_("Session: %s") % self.title, items)
348 bh 217
349    
350 bh 6 def create_empty_session():
351     """Return an empty session useful as a starting point"""
352 jan 103 import os
353 jan 374 session = Session(_('unnamed session'))
354 jan 103 session.SetFilename(None)
355 jan 374 session.AddMap(Map(_('unnamed map')))
356 bh 56 session.UnsetModified()
357 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