1 |
jan |
1726 |
# Copyright (C) 2003 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Jan-Oliver Wagner <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
""" |
9 |
|
|
Classes for generic ArcView ODB Objects |
10 |
|
|
as in '.apr', '.avl' and other files. |
11 |
|
|
""" |
12 |
|
|
|
13 |
|
|
__version__ = "$Revision$" |
14 |
|
|
|
15 |
|
|
from types import StringType |
16 |
|
|
|
17 |
|
|
from Thuban import _ |
18 |
|
|
|
19 |
|
|
class ODBBaseObject: |
20 |
|
|
|
21 |
|
|
"""Base class for ODB objects.""" |
22 |
|
|
|
23 |
|
|
_obj_refs = [] # override this list with the object names |
24 |
|
|
# as strings that are referenced. |
25 |
|
|
_values = [] # override this list with the keywords |
26 |
|
|
# for known values of the class. |
27 |
|
|
|
28 |
|
|
def __init__(self, parent_odb, type, number): |
29 |
|
|
self._parent_odb = parent_odb |
30 |
|
|
self.type = type |
31 |
|
|
self.number = number |
32 |
|
|
|
33 |
|
|
def __setattr__(self, attr, value): |
34 |
|
|
"""If an attribute is set that already exists, |
35 |
|
|
a list is created to contain both. Any further |
36 |
|
|
attribute with the same name is appended to the list. |
37 |
|
|
""" |
38 |
|
|
if hasattr(self, attr): |
39 |
|
|
v = getattr(self, attr) |
40 |
|
|
if isinstance(v, list): |
41 |
|
|
v.append(value) |
42 |
|
|
else: |
43 |
|
|
self.__dict__[attr] = [v, value] |
44 |
|
|
else: |
45 |
|
|
self.__dict__[attr] = value |
46 |
|
|
|
47 |
|
|
def __repr__(self): |
48 |
|
|
"""The string represenation of an object is the syntax |
49 |
|
|
as used in the ODB file. |
50 |
|
|
""" |
51 |
|
|
s = '(' + self.type + '.' + str(self.number) + '\n' |
52 |
|
|
for k in self.__dict__.keys(): |
53 |
|
|
if k in [ '_parent_odb', 'type', 'number']: continue |
54 |
|
|
v = self.__dict__[k] |
55 |
|
|
if isinstance(v, list): |
56 |
|
|
for val in v: |
57 |
|
|
s += "\t%s:\t%s\n" % (k, val) |
58 |
|
|
else: |
59 |
|
|
s += "\t%s:\t%s\n" % (k, v) |
60 |
|
|
s += ')\n' |
61 |
|
|
return s |
62 |
|
|
|
63 |
|
|
def Get(self, attr): |
64 |
|
|
"""If an attribute is a pointer (or a list of pointers) |
65 |
|
|
to another object (other objects), references are resolved |
66 |
|
|
to point to the corresponding objects. |
67 |
|
|
|
68 |
|
|
attr -- string with the name of the attribute |
69 |
|
|
""" |
70 |
|
|
if not hasattr(self, attr): |
71 |
|
|
print "Object %s.%s has no attribute %s!" % ( self.type, |
72 |
|
|
self.number, attr) |
73 |
|
|
return None |
74 |
|
|
|
75 |
|
|
if attr in self._obj_refs: |
76 |
|
|
if isinstance(self.__dict__[attr], list): |
77 |
|
|
lst = [] |
78 |
|
|
for obj_id in self.__dict__[attr]: |
79 |
|
|
obj_id = int(obj_id) |
80 |
|
|
if not self._parent_odb.GetObjects().has_key(obj_id): |
81 |
|
|
print "Object #%d missing!" % obj_id |
82 |
|
|
else: |
83 |
|
|
lst.append(self._parent_odb.GetObjects()[obj_id]) |
84 |
|
|
return lst |
85 |
|
|
else: |
86 |
|
|
obj_id = int(self.__dict__[attr]) |
87 |
|
|
if not self._parent_odb.GetObjects().has_key(obj_id): |
88 |
|
|
print "Object #%d missing!" % s |
89 |
|
|
return None |
90 |
|
|
return self._parent_odb.GetObjects()[obj_id] |
91 |
|
|
|
92 |
|
|
return self.__dict__[attr] |
93 |
|
|
|
94 |
|
|
def TreeInfo(self): |
95 |
|
|
"""Return the information in a tree-like structure.""" |
96 |
|
|
items = [] |
97 |
|
|
|
98 |
|
|
# known values |
99 |
|
|
for o in self._values: |
100 |
|
|
if hasattr(self, o): |
101 |
|
|
items.append('%s: %s' % (o, self.Get(o))) |
102 |
|
|
|
103 |
|
|
# the objects |
104 |
|
|
for o in self._obj_refs: |
105 |
|
|
if not hasattr(self, o): continue |
106 |
|
|
if not isinstance(self.Get(o), list): |
107 |
|
|
items.append(self.Get(o).TreeInfo()) |
108 |
|
|
continue |
109 |
|
|
for obj in self.Get(o): |
110 |
|
|
items.append(obj.TreeInfo()) |
111 |
|
|
|
112 |
|
|
# unknown values |
113 |
|
|
for k in self.__dict__.keys(): |
114 |
|
|
if k in [ '_parent_odb', 'type', 'number']: continue |
115 |
|
|
if k in self._obj_refs: continue |
116 |
|
|
if k in self._values: continue |
117 |
|
|
v = self.__dict__[k] |
118 |
|
|
if isinstance(v, list): |
119 |
|
|
items.append(_("Unknown Object list named: '%s'") % k) |
120 |
|
|
else: |
121 |
|
|
if isinstance(v, StringType): |
122 |
|
|
items.append(_("Unknown Value named: '%s' with value '%s'")\ |
123 |
|
|
% ( k, v )) |
124 |
|
|
else: |
125 |
|
|
items.append(_('Unknown Object named: %s') % k) |
126 |
|
|
|
127 |
|
|
return [self.type, items] |