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]> |
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 |
LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \ |
MAP_LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \ |
17 |
LAYER_LEGEND_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 |
|
|
20 |
from base import TitledObject, Modifiable |
from Thuban import _ |
21 |
|
|
22 |
|
from base import TitledObject, Modifiable |
23 |
from map import Map |
from map import Map |
24 |
|
from data import ShapefileStore |
25 |
|
|
26 |
|
from transientdb import TransientDatabase |
27 |
|
|
28 |
|
class AutoRemoveFile: |
29 |
|
|
30 |
|
"""Remove a file once all references go away.""" |
31 |
|
|
32 |
|
def __init__(self, filename, tempdir = None): |
33 |
|
"""Initialize the AutoRemoveFile |
34 |
|
|
35 |
|
Parameters: |
36 |
|
filename -- The name of the file to remove in __del__ |
37 |
|
tempdir -- Another object simple stored as an instance variable. |
38 |
|
|
39 |
|
As the name suggests the tempdir parameter is intended for a |
40 |
|
temporary directory the file might be located in. The intended |
41 |
|
use is that it's an instance of AutoRemoveDir. |
42 |
|
""" |
43 |
|
self.filename = filename |
44 |
|
self.tempdir = tempdir |
45 |
|
|
46 |
|
def __del__(self, remove = os.remove): |
47 |
|
remove(self.filename) |
48 |
|
|
49 |
|
class AutoRemoveDir: |
50 |
|
|
51 |
|
"""Remove a directory once all references go away |
52 |
|
|
53 |
|
The intended use of this class together with AutoRemoveFile is for |
54 |
|
temporary directories and files containd therein. An AutoRemoveDir |
55 |
|
should be instantiated for the directory and passed as the tempdir |
56 |
|
parameter to every AutoRemoveFile instance created for files in the |
57 |
|
directory. An AutoRemoveFile shold be instantiated for every file |
58 |
|
created in the directory so that the directory is automatically |
59 |
|
removed once the last file is removed. |
60 |
|
""" |
61 |
|
|
62 |
|
def __init__(self, filename): |
63 |
|
"""Initialize the AutoRemoveDir |
64 |
|
|
65 |
|
The parameter is the name of the directory. |
66 |
|
""" |
67 |
|
self.filename = filename |
68 |
|
|
69 |
|
def __del__(self, rmdir = os.rmdir): |
70 |
|
rmdir(self.filename) |
71 |
|
|
72 |
|
|
73 |
|
# WeakKey dictionary mapping objects like the transient_db to |
74 |
|
# AutoRemoveDir or AutoRemoveFile instances to make sure that the |
75 |
|
# temporary files and the directory are deleted but not before the |
76 |
|
# objects that use them go away. |
77 |
|
auto_remover = weakref.WeakKeyDictionary() |
78 |
|
|
79 |
class Session(TitledObject, Modifiable): |
class Session(TitledObject, Modifiable): |
80 |
|
|
92 |
|
|
93 |
EXTENSIONS_CHANGED -- Extensions were added, removed. |
EXTENSIONS_CHANGED -- Extensions were added, removed. |
94 |
|
|
95 |
LAYERS_CHANGED -- Same as the map's event of the same name. |
MAP_LAYERS_CHANGED -- Same as the map's event of the same name. |
96 |
It's simply resent from the session to make |
It's simply resent from the session to make |
97 |
subscriptions easier. |
subscriptions easier. |
98 |
|
|
107 |
# message channels that have to be forwarded from maps contained in |
# message channels that have to be forwarded from maps contained in |
108 |
# the session. |
# the session. |
109 |
forwarded_channels = ( |
forwarded_channels = ( |
110 |
|
# generic channels |
111 |
|
CHANGED, |
112 |
|
|
113 |
# map specific channels |
# map specific channels |
114 |
MAP_PROJECTION_CHANGED, |
MAP_PROJECTION_CHANGED, |
115 |
LAYERS_CHANGED, |
MAP_LAYERS_CHANGED, |
116 |
|
|
117 |
# layer channels forwarded by the map |
# layer channels forwarded by the map |
118 |
LAYER_PROJECTION_CHANGED, |
LAYER_PROJECTION_CHANGED, |
119 |
LAYER_LEGEND_CHANGED, |
LAYER_CHANGED, |
120 |
LAYER_VISIBILITY_CHANGED, |
LAYER_VISIBILITY_CHANGED, |
121 |
|
|
122 |
# channels forwarded by an extension |
# channels forwarded by an extension |
129 |
self.filename = None |
self.filename = None |
130 |
self.maps = [] |
self.maps = [] |
131 |
self.tables = [] |
self.tables = [] |
132 |
|
self.shapestores = [] |
133 |
self.extensions = [] |
self.extensions = [] |
134 |
|
self.temp_dir = None |
135 |
|
self.transient_db = None |
136 |
|
|
137 |
def changed(self, channel = None, *args): |
def changed(self, channel = None, *args): |
138 |
"""Like the inherited version but issue a CHANGED message as well. |
"""Like the inherited version but issue a CHANGED message as well. |
179 |
extension.Subscribe(channel, self.forward, channel) |
extension.Subscribe(channel, self.forward, channel) |
180 |
self.changed(EXTENSIONS_CHANGED) |
self.changed(EXTENSIONS_CHANGED) |
181 |
|
|
182 |
|
def ShapeStores(self): |
183 |
|
"""Return a list of all ShapeStore objects open in the session""" |
184 |
|
return [store() for store in self.shapestores] |
185 |
|
|
186 |
|
def _add_shapestore(self, store): |
187 |
|
"""Internal: Add the shapestore to the list of shapestores""" |
188 |
|
self.shapestores.append(weakref.ref(store, |
189 |
|
self._clean_weak_store_refs)) |
190 |
|
|
191 |
|
def _clean_weak_store_refs(self, weakref): |
192 |
|
"""Internal: Remove the weakref from the shapestores list""" |
193 |
|
self.shapestores = [store for store in self.shapestores |
194 |
|
if store is not weakref] |
195 |
|
|
196 |
|
def Tables(self): |
197 |
|
"""Return a list of all table objects open in the session""" |
198 |
|
# Tables are only available from shapestores at the moment, so |
199 |
|
# we just take them from there |
200 |
|
return [store.Table() for store in self.ShapeStores()] |
201 |
|
|
202 |
|
def temp_directory(self): |
203 |
|
""" |
204 |
|
Return the name of the directory for session specific temporary files |
205 |
|
|
206 |
|
Create the directory if it doesn't exist yet. |
207 |
|
""" |
208 |
|
if self.temp_dir is None: |
209 |
|
temp_dir = mktemp() |
210 |
|
os.mkdir(temp_dir, 0700) |
211 |
|
self.temp_dir = temp_dir |
212 |
|
self.temp_dir_remover = AutoRemoveDir(self.temp_dir) |
213 |
|
return self.temp_dir |
214 |
|
|
215 |
|
def OpenShapefile(self, filename): |
216 |
|
"""Return a shapefile store object for the data in the given file""" |
217 |
|
store = ShapefileStore(self, filename) |
218 |
|
self._add_shapestore(store) |
219 |
|
return store |
220 |
|
|
221 |
|
def TransientDB(self): |
222 |
|
if self.transient_db is None: |
223 |
|
filename = os.path.join(self.temp_directory(), "transientdb") |
224 |
|
self.transient_db = TransientDatabase(filename) |
225 |
|
#print self.temp_dir_remover |
226 |
|
auto_remover[self.transient_db] = AutoRemoveFile(filename, |
227 |
|
self.temp_dir_remover) |
228 |
|
return self.transient_db |
229 |
|
|
230 |
def Destroy(self): |
def Destroy(self): |
231 |
for map in self.maps: |
for map in self.maps: |
232 |
map.Destroy() |
map.Destroy() |
233 |
self.maps = [] |
self.maps = [] |
234 |
self.tables = [] |
self.tables = [] |
235 |
Publisher.Destroy(self) |
Modifiable.Destroy(self) |
236 |
|
|
237 |
|
# Close the transient DB explicitly so that it removes any |
238 |
|
# journal files from the temporary directory |
239 |
|
if self.transient_db is not None: |
240 |
|
self.transient_db.close() |
241 |
|
|
242 |
def forward(self, *args): |
def forward(self, *args): |
243 |
"""Reissue events. |
"""Reissue events. |
274 |
def TreeInfo(self): |
def TreeInfo(self): |
275 |
items = [] |
items = [] |
276 |
if self.filename is None: |
if self.filename is None: |
277 |
items.append("Filename:") |
items.append(_("Filename:")) |
278 |
else: |
else: |
279 |
items.append("Filename: %s" % self.filename) |
items.append(_("Filename: %s") % self.filename) |
280 |
|
|
281 |
if self.WasModified(): |
if self.WasModified(): |
282 |
items.append("Modified") |
items.append(_("Modified")) |
283 |
else: |
else: |
284 |
items.append("Unmodified") |
items.append(_("Unmodified")) |
285 |
|
|
286 |
items.extend(self.maps) |
items.extend(self.maps) |
287 |
items.extend(self.extensions) |
items.extend(self.extensions) |
288 |
|
|
289 |
return ("Session: %s" % self.title, items) |
return (_("Session: %s") % self.title, items) |
290 |
|
|
291 |
|
|
292 |
def create_empty_session(): |
def create_empty_session(): |
293 |
"""Return an empty session useful as a starting point""" |
"""Return an empty session useful as a starting point""" |
294 |
import os |
import os |
295 |
session = Session('unnamed session') |
session = Session(_('unnamed session')) |
296 |
session.SetFilename(None) |
session.SetFilename(None) |
297 |
session.AddMap(Map('unnamed map')) |
session.AddMap(Map(_('unnamed map'))) |
298 |
session.UnsetModified() |
session.UnsetModified() |
299 |
return session |
return session |