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 |
|
|
return self.tables + [store.Table() for store in self.ShapeStores()] |
205 |
|
|
|
206 |
bh |
1068 |
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 |
bh |
982 |
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 |
bh |
1124 |
self.changed() |
241 |
bh |
982 |
return transient_table |
242 |
|
|
|
243 |
bh |
987 |
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 |
bh |
1068 |
|
250 |
|
|
Issue a TABLE_REMOVED message after the table has been removed. |
251 |
|
|
The message has the removed table as the single parameter. |
252 |
bh |
987 |
""" |
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 |
bh |
1068 |
self.changed(TABLE_REMOVED, table) |
258 |
bh |
987 |
|
259 |
bh |
1268 |
def DataContainers(self): |
260 |
|
|
"""Return all data containers, i.e. shapestores and tables""" |
261 |
|
|
return self.tables + self.ShapeStores() |
262 |
|
|
|
263 |
bh |
1036 |
def OpenTableFile(self, filename): |
264 |
|
|
"""Open the table file filename and return the table object. |
265 |
|
|
|
266 |
|
|
The filename argument must be the name of a DBF file. |
267 |
|
|
""" |
268 |
|
|
return self.AddTable(DBFTable(filename)) |
269 |
|
|
|
270 |
bh |
765 |
def temp_directory(self): |
271 |
|
|
""" |
272 |
|
|
Return the name of the directory for session specific temporary files |
273 |
|
|
|
274 |
|
|
Create the directory if it doesn't exist yet. |
275 |
|
|
""" |
276 |
|
|
if self.temp_dir is None: |
277 |
|
|
temp_dir = mktemp() |
278 |
|
|
os.mkdir(temp_dir, 0700) |
279 |
|
|
self.temp_dir = temp_dir |
280 |
|
|
self.temp_dir_remover = AutoRemoveDir(self.temp_dir) |
281 |
|
|
return self.temp_dir |
282 |
|
|
|
283 |
bh |
723 |
def OpenShapefile(self, filename): |
284 |
|
|
"""Return a shapefile store object for the data in the given file""" |
285 |
bh |
851 |
store = ShapefileStore(self, filename) |
286 |
|
|
self._add_shapestore(store) |
287 |
|
|
return store |
288 |
bh |
723 |
|
289 |
bh |
1016 |
def AddShapeStore(self, shapestore): |
290 |
|
|
"""Add the shapestore to the session. |
291 |
|
|
|
292 |
|
|
The session only holds a weak reference to the shapestore, so it |
293 |
|
|
will automatically be removed from the session when the last |
294 |
|
|
reference goes away. |
295 |
|
|
""" |
296 |
|
|
self._add_shapestore(shapestore) |
297 |
|
|
return shapestore |
298 |
|
|
|
299 |
bh |
765 |
def TransientDB(self): |
300 |
|
|
if self.transient_db is None: |
301 |
|
|
filename = os.path.join(self.temp_directory(), "transientdb") |
302 |
|
|
self.transient_db = TransientDatabase(filename) |
303 |
|
|
#print self.temp_dir_remover |
304 |
|
|
auto_remover[self.transient_db] = AutoRemoveFile(filename, |
305 |
|
|
self.temp_dir_remover) |
306 |
|
|
return self.transient_db |
307 |
|
|
|
308 |
bh |
6 |
def Destroy(self): |
309 |
|
|
for map in self.maps: |
310 |
|
|
map.Destroy() |
311 |
|
|
self.maps = [] |
312 |
|
|
self.tables = [] |
313 |
bh |
250 |
Modifiable.Destroy(self) |
314 |
bh |
6 |
|
315 |
bh |
778 |
# Close the transient DB explicitly so that it removes any |
316 |
|
|
# journal files from the temporary directory |
317 |
|
|
if self.transient_db is not None: |
318 |
|
|
self.transient_db.close() |
319 |
|
|
|
320 |
bh |
6 |
def forward(self, *args): |
321 |
bh |
232 |
"""Reissue events. |
322 |
|
|
|
323 |
|
|
If the channel the event is forwarded to is a changed-channel |
324 |
|
|
that is not the CHANGED channel issue CHANGED as well. An |
325 |
|
|
channel is considered to be a changed-channel if it's name ends |
326 |
|
|
with 'CHANGED'. |
327 |
|
|
""" |
328 |
bh |
6 |
if len(args) > 1: |
329 |
|
|
args = (args[-1],) + args[:-1] |
330 |
|
|
apply(self.issue, args) |
331 |
bh |
232 |
channel = args[0] |
332 |
|
|
# It's a bit of a kludge to rely on the channel name for this. |
333 |
|
|
if channel.endswith("CHANGED") and channel != CHANGED: |
334 |
|
|
self.issue(CHANGED, self) |
335 |
bh |
6 |
|
336 |
|
|
def WasModified(self): |
337 |
|
|
"""Return true if the session or one of the maps was modified""" |
338 |
|
|
if self.modified: |
339 |
|
|
return 1 |
340 |
|
|
else: |
341 |
|
|
for map in self.maps: |
342 |
|
|
if map.WasModified(): |
343 |
|
|
return 1 |
344 |
|
|
return 0 |
345 |
|
|
|
346 |
|
|
def UnsetModified(self): |
347 |
|
|
"""Unset the modified flag of the session and the maps""" |
348 |
|
|
Modifiable.UnsetModified(self) |
349 |
|
|
for map in self.maps: |
350 |
|
|
map.UnsetModified() |
351 |
|
|
|
352 |
bh |
217 |
def TreeInfo(self): |
353 |
|
|
items = [] |
354 |
|
|
if self.filename is None: |
355 |
jan |
374 |
items.append(_("Filename:")) |
356 |
bh |
217 |
else: |
357 |
jan |
374 |
items.append(_("Filename: %s") % self.filename) |
358 |
bh |
6 |
|
359 |
bh |
217 |
if self.WasModified(): |
360 |
jan |
374 |
items.append(_("Modified")) |
361 |
bh |
217 |
else: |
362 |
jan |
374 |
items.append(_("Unmodified")) |
363 |
bh |
6 |
|
364 |
bh |
217 |
items.extend(self.maps) |
365 |
|
|
items.extend(self.extensions) |
366 |
|
|
|
367 |
jan |
374 |
return (_("Session: %s") % self.title, items) |
368 |
bh |
217 |
|
369 |
|
|
|
370 |
bh |
6 |
def create_empty_session(): |
371 |
|
|
"""Return an empty session useful as a starting point""" |
372 |
jan |
103 |
import os |
373 |
jan |
374 |
session = Session(_('unnamed session')) |
374 |
jan |
103 |
session.SetFilename(None) |
375 |
jan |
374 |
session.AddMap(Map(_('unnamed map'))) |
376 |
bh |
56 |
session.UnsetModified() |
377 |
bh |
6 |
return session |