*** empty log message ***
[pli.git] / pli / pattern / store / storeclient.py
blob1517c6ea59cdfa1b0e5ff6ccdaee06dd7a28c0c9
1 #=======================================================================
3 __version__ = '''0.0.01'''
4 __sub_version__ = '''20040803232005'''
5 __copyright__ = '''(c) Alex A. Naanou 2003'''
8 #-----------------------------------------------------------------------
10 import store
13 #-----------------------------------------------------------------------
14 ##!!! revise !!!##
15 #---------------------------------------------------------StoreClient---
16 class GenericStaticStoreClient(store.BaseStore):
17 '''
18 '''
19 __source_store__ = None
21 def __init__(self, filters=()):
22 '''
23 '''
24 if self.__source_store__ == None:
25 raise TypeError, 'no store defined.'
26 super(GenericStaticStoreClient, self).__init__(None)
27 self._store_data = self.__source_store__
28 self._store_types = self.__source_store__._store_types
29 self._store_filters = filters
30 def __getattr__(self, name):
31 '''
32 '''
33 # deligate the call to the original store object
34 # constructors...
35 store = self.__source_store__
36 if not hasattr(store, name):
37 raise AttributeError, '%s has no attribute "%s"' % (self, name)
38 if name in store._store_types:
39 return getattr(store, name)
40 raise AttributeError, ''
41 def isaccessible(self, key):
42 '''
43 '''
44 obj = self._store_data[key]
45 if hasattr(obj, '__store_public__') and obj.__store_public__ or\
46 None not in [f(obj) for f in self._store_filters]:
47 return True
48 return False
49 def __getitem__(self, key):
50 '''
51 '''
52 if self.isaccessible(key):
53 return self._store_data[key]
54 def __setitem__(self, key, obj):
55 '''
56 '''
57 if key in self._store_data and not self.isaccessible(key):
58 raise KeyError, 'can\'t set the "%s" item.' % key
59 ## BaseStore.__setitem__(self, key, obj)
60 super(GenericStaticStoreClient, self).__setitem__(key, obj)
61 def __delitem__(self, key):
62 '''
63 '''
64 if key in self._store_data and not self.isaccessible(key):
65 raise KeyError, 'can\'t remove the "%s" item.' % key
66 ## BaseStore.__delitem__(self, key)
67 super(GenericStaticStoreClient, self).__delitem__(key)
68 def __iter__(self):
69 '''
70 '''
71 for n in self._store_data:
72 if self.isaccessible(n):
73 yield n
74 def iterkeys(self):
75 '''
76 '''
77 ## print '>>>', self._store_data.__getitem__('ttt')
78 ## print '!!!', self._store_data, id(self._store_data)
79 for e in self._store_data.iterkeys():
80 if self.isaccessible(e):
81 yield e
82 def itervalues(self):
83 '''
84 '''
85 for e in self._store_data.iterkeys():
86 if self.isaccessible(e):
87 yield self._store_data[e]
88 def iteritems(self):
89 '''
90 '''
91 for n, o in self._store_data.iteritems():
92 if self.isaccessible(n):
93 yield n, o
94 def keys(self):
95 '''
96 '''
97 return list(self.iterkeys())
98 def values(self):
99 '''
101 return list(self.itervalues())
102 def get_object_data(self, key, *attrs):
105 if not self.isaccessible(key):
106 raise KeyError, key
107 return super(GenericStaticStoreClient, self).get_object_data(self, key, *attrs)
110 #---------------------------------------------------StaticStoreClient---
111 # TODO move this into storeutils... (??)
112 class StaticStoreClient(GenericStaticStoreClient):
116 __ignore_nonexistant_filter_attrs__ = False
118 def __init__(self, filters={}):
121 super(StaticStoreClient, self).__init__(filters)
122 def isaccessible(self, key):
125 obj = self._store_data[key]
126 ## obj = self[key]
127 if hasattr(self, '__ignore_nonexistant_filter_attrs__') \
128 and self.__ignore_nonexistant_filter_attrs__:
129 res = []
130 for attr, val in self._store_filters.items():
131 try:
132 res += [getattr(obj, attr) == val]
133 except AttributeError:
134 pass
135 if False not in res:
136 return True
137 else:
138 if hasattr(obj, '__store_public__') and obj.__store_public__ or\
139 False not in [getattr(obj, attr) == val for attr, val in self._store_filters.items()]:
140 return True
141 return False
144 #----------------------------------------StaticStoreClientConstructor---
145 class StaticStoreClientConstructor(StaticStoreClient):
148 def __init__(self, source_store=None, filters={}):
151 self.__source_store__ = source_store
152 super(StaticStoreClientConstructor, self).__init__(filters)
156 #=======================================================================
157 # vim:set ts=4 sw=4 nowrap :