source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/DiscoveryRecord.py @ 2672

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/DiscoveryRecord.py@2672
Revision 2672, 2.6 KB checked in by lawrence, 13 years ago (diff)

Interim commit of discovery/browse in ows stack.

Line 
1#import getattr
2class ConstrainedDict(dict):
3   
4    ''' This is a class carrying restricted dictionaries, used as a convenience
5    class for multiple applications. 
6            The subclassing gives us two things:
7             (1) the ability to get and set dictionary items either as normal
8                x['a']=1 or x.a=1, and
9             (2) a restricted set of dictionary members which are defined when the
10    object is instantiated '''
11   
12    def __init__(self,default):
13        if isinstance(default,dict):
14            self.allowed=default.keys()
15            for k in default: self[k]=default[k]
16        else:
17            self.allowed=[]
18            for k,v in default:
19                self.allowed.append(k)
20                self[k]=v
21
22    def __getattr_(self,key):
23        return self[key]
24 
25    def __setattr_(self,key,value):       
26        self.__setitem__[key]=value
27
28    def __setitem__(self,key,value):
29        if key in self.allowed:
30            self.__dict__[key]=value
31        else:
32            raise KeyError('Invalid key [%s] to constrained dictionary'%key)
33
34class Member(dict):
35    """An extended dictionary that is used for members of collections
36
37    The extended part allows you to get and set values as attributes.
38    That is,
39
40       d.fred
41       
42    is the same as
43
44       d['fred']
45    """
46   
47    def __init__(self,indict):
48        self.allowed=indict.keys()
49        for k in indict: self[k]=indict[k]
50   
51    def __getattr__(self, key):
52        try:
53            return self.__dict__[key]
54        except KeyError:
55            pass
56        try:
57            #assert not key.startswith('_')
58            return self.__getitem__(key)
59        except:
60            raise AttributeError, "object has no attribute '%s'" % key
61
62    def __setattr__(self, key, value):
63        if key<>'allowed':
64            if key not in self.allowed:
65                raise KeyError('Invalid key [%s] to constrained dictionary'%key)
66        if key.startswith('_'):
67            self.__dict__[key] = value
68        else:
69            return self.__setitem__(key, value)
70
71
72if __name__=="__main__":
73    import unittest
74
75    class TestCase(unittest.TestCase):
76   
77        def testCD(self):
78            ''' test the constrained dictionary class '''
79       
80            x=ConstrainedDict((('a','a'),('b','b')))
81            print x
82            x['a']=1
83            print x
84            x.c=2
85            print x
86            y=ConstrainedDict({'c':'c','d':'d'})
87            print y
88       
89        def testMember(self):
90           
91            x=Member({'c':'c','d':'d'})
92            x.c='1'
93            print x
94            x['d']=2
95            print x
96            print x.d
97   
98    unittest.main()
Note: See TracBrowser for help on using the repository browser.