root/MGET/Trunk/PythonPackage/src/GeoEco/DynamicDocString.py

Revision 42, 5.2 kB (checked in by jjr8, 3 years ago)

Inserted copyright statements in all source code that place GeoEco? under the GNU General Public License. (Resolved ticket #3).

Line 
1 # DynamicDocString.py - Provides the DynamicDocString class, which is used by
2 # classes in the GeoEco Python package to store metadata within the classes'
3 # __doc__ attributes.
4 #
5 # Copyright (C) 2007 Jason J. Roberts
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License (available in the file LICENSE.TXT)
16 # for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
22 import types
23
24
25 class DynamicDocString(types.UnicodeType):
26
27     def __init__(self, obj=None):
28         self.Obj = obj
29
30     def _GetObj(self):
31         return self._Obj
32    
33     def _SetObj(self, value):
34         self._Obj = value
35
36     Obj = property(_GetObj, _SetObj)
37
38     def __getattribute__(self, name):
39         assert isinstance(name, basestring)
40         if isinstance(name, types.StringType):
41             name = unicode(name)
42         if name == u'Obj' or name == u'_Obj' or name == u'_GetObj' or name == u'_SetObj' or name.startswith(u'__'):
43             return object.__getattribute__(self, name)
44         return getattr(unicode(self._Obj), name)
45
46     # Python always calls __getattribute__ to obtain normal unicode functions
47     # such as upper. This allows our __getattribute__ to expose these as if we
48     # were a unicode ourself. But Python does NOT appear to call __getattribute__
49     # to obtain "magic" functions such as __eq__ or __getslice__. So we can't
50     # use __getattribute__ to forward these calls to unicode(self._Obj).__eq__,
51     # unicode(self._Obj).__getslice__, and so on. We have to create our own versions
52     # that just call through to unicode(self._Obj). This is unfortunate because if
53     # new versions of Python add new magic methods, we have to add them here
54     # if we wish to remain a perfect replication of types.UnicodeType.
55     #
56     # Note: This situation has has nothing to do with the fact that the if
57     # statement preceding this comment includes the OR clause
58     # "name.startswith(u'__')". That OR clause was added to prevent infinite
59     # recusion that occurred when I discovered that __getattribute__ IS called
60     # to obtain the __objclass__ attribute. Somehow Python treats this attribute
61     # different than the others (such as __add__, __unicode__, etc).
62
63     def __add__(self, other):
64         return unicode(self._Obj).__add__(other)
65
66     def __contains__(self, other):
67         return unicode(self._Obj).__contains__(other)
68
69     def __eq__(self, other):
70         return unicode(self._Obj).__eq__(other)
71
72     def __ge__(self, other):
73         return unicode(self._Obj).__ge__(other)
74
75     def __getitem__(self, key):
76         return unicode(self._Obj).__getitem__(key)
77
78     def __getnewargs__(self, *args):
79         raise NotImplementedError, u'DynamicDocString does not currently support pickling.'
80
81     def __getslice__(self, start, end):
82         return unicode(self._Obj).__getslice__(start, end)
83
84     def __gt__(self, other):
85         return unicode(self._Obj).__gt__(other)
86
87     def __hash__(self):
88         raise NotImplementedError, u'DynamicDocString is not hashable because it is mutable. DynamicDocString cannot serve as a dictionary key.'
89
90     def __le__(self, other):
91         return unicode(self._Obj).__le__(other)
92
93     def __len__(self):
94         return unicode(self._Obj).__len__()
95
96     def __lt__(self, other):
97         return unicode(self._Obj).__lt__(other)
98
99     def __mod__(self, other):
100         return unicode(self._Obj).__mod__(other)
101
102     def __mul__(self, other):
103         return unicode(self._Obj).__mul__(other)
104
105     def __ne__(self, other):
106         return unicode(self._Obj).__ne__(other)
107
108     def __reduce__(self):
109         raise NotImplementedError, u'DynamicDocString does not currently support pickling.'
110
111     def __reduce_ex__(self, protocol):
112         raise NotImplementedError, u'DynamicDocString does not currently support pickling.'
113
114     def __radd__(self, other):
115         return other.__add__(unicode(self._Obj))        # types.UnicodeType does not define __radd__ but we cannot be concatenated with other strings unless we do.
116
117     def __repr__(self):
118         return unicode(self._Obj).__repr__()
119
120     def __rmod__(self, other):
121         return unicode(self._Obj).__rmod__(other)
122
123     def __rmul__(self, other):
124         return unicode(self._Obj).__rmul__(other)
125
126     def __str__(self):
127         return str(self._Obj)
128
129     def __unicode__(self):
130         return unicode(self._Obj)
131
132
133 ###############################################################################
134 # Names exported by this module
135 ###############################################################################
136
137 __all__ = ['DynamicDocString']
Note: See TracBrowser for help on using the browser.