1 |
joey |
2299 |
# Copyright (c) 2004 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Martin Schulze <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software; you can redistribute it and/or modify |
6 |
|
|
# it under the terms of the GNU General Public License as published by |
7 |
|
|
# the Free Software Foundation; either version 2 of the License, or |
8 |
|
|
# (at your option) any later version. |
9 |
|
|
# |
10 |
|
|
# This program is distributed in the hope that it will be useful, |
11 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
# GNU General Public License for more details. |
14 |
|
|
# |
15 |
|
|
# You should have received a copy of the GNU General Public License |
16 |
|
|
# along with this program; if not, write to the Free Software |
17 |
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
|
|
|
19 |
|
|
""" |
20 |
|
|
The provided class maintains a mapping between classes and objects. |
21 |
|
|
Instances of the class are used as keys, not classes, though. |
22 |
|
|
|
23 |
|
|
Usually layer classes and their instances are used in combination with |
24 |
|
|
layer property dialog classes in order to utilise the proper dialog |
25 |
|
|
for a given layer instance. |
26 |
|
|
|
27 |
|
|
""" |
28 |
|
|
|
29 |
|
|
__version__ = "$Revision$" |
30 |
|
|
# $Source$ |
31 |
|
|
# $Id$ |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
class ClassMapper: |
35 |
|
|
""" |
36 |
|
|
Thuban class to implement a mapping of classes and objects, |
37 |
|
|
usually also classes. |
38 |
|
|
""" |
39 |
|
|
|
40 |
|
|
mapping = None |
41 |
|
|
|
42 |
|
|
|
43 |
|
|
def __init__(self): |
44 |
|
|
""" |
45 |
|
|
Initialises a mapping. |
46 |
|
|
""" |
47 |
|
|
|
48 |
|
|
self.mapping = [] |
49 |
|
|
|
50 |
|
|
|
51 |
|
|
def add(self, key_class, obj): |
52 |
|
|
""" |
53 |
|
|
Register an object with a class. |
54 |
|
|
|
55 |
|
|
This method adds the provided pair (key_class,obj) to the |
56 |
|
|
internal list. Subsequently instances of key_class are used |
57 |
|
|
to retrieved the stored object. |
58 |
|
|
""" |
59 |
|
|
|
60 |
|
|
self.mapping.append((key_class, obj)) |
61 |
|
|
|
62 |
|
|
|
63 |
|
|
def get(self, instance): |
64 |
|
|
""" |
65 |
|
|
Retrieve a stored object corresponding to the provided class |
66 |
|
|
instance. |
67 |
|
|
""" |
68 |
|
|
|
69 |
|
|
for key_class, obj in self.mapping: |
70 |
|
|
if isinstance(instance, key_class): |
71 |
|
|
return obj |
72 |
|
|
return None |
73 |
|
|
|
74 |
|
|
|
75 |
|
|
def has(self, instance): |
76 |
|
|
""" |
77 |
|
|
Determine if the ClassMapper contains an object corresponding |
78 |
|
|
to the provided instance |
79 |
|
|
""" |
80 |
|
|
|
81 |
|
|
return self.get(instance) is not None |