1 # Copyright (c) 2018 ARM Limited
4 # The license below extends only to copyright in the software and shall
5 # not be construed as granting a license to any other intellectual
6 # property including but not limited to intellectual property relating
7 # to a hardware implementation of the functionality of the software
8 # licensed hereunder. You may use the software subject to the license
9 # terms below provided that you ensure that this notice is replicated
10 # unmodified and in its entirety in all distributions of the software,
11 # modified or unmodified, in source code or in binary form.
13 # Copyright (c) 2004-2006 The Regents of The University of Michigan
14 # All rights reserved.
16 # Redistribution and use in source and binary forms, with or without
17 # modification, are permitted provided that the following conditions are
18 # met: redistributions of source code must retain the above copyright
19 # notice, this list of conditions and the following disclaimer;
20 # redistributions in binary form must reproduce the above copyright
21 # notice, this list of conditions and the following disclaimer in the
22 # documentation and/or other materials provided with the distribution;
23 # neither the name of the copyright holders nor the names of its
24 # contributors may be used to endorse or promote products derived from
25 # this software without specific prior written permission.
27 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #####################################################################
41 # Proxy object support.
43 #####################################################################
45 from __future__
import print_function
46 from __future__
import absolute_import
54 class BaseProxy(object):
55 def __init__(self
, search_self
, search_up
):
56 self
._search
_self
= search_self
57 self
._search
_up
= search_up
58 self
._multipliers
= []
61 if self
._search
_self
and not self
._search
_up
:
63 elif not self
._search
_self
and self
._search
_up
:
67 return s
+ '.' + self
.path()
69 def __setattr__(self
, attr
, value
):
70 if not attr
.startswith('_'):
72 "cannot set attribute '%s' on proxy object" % attr
)
73 super(BaseProxy
, self
).__setattr
__(attr
, value
)
75 # support for multiplying proxies by constants or other proxies to
77 def __mul__(self
, other
):
78 if not (isinstance(other
, (int, long, float)) or isproxy(other
)):
80 "Proxy multiplier must be a constant or a proxy to a param")
81 self
._multipliers
.append(other
)
86 def _mulcheck(self
, result
, base
):
88 for multiplier
in self
._multipliers
:
89 if isproxy(multiplier
):
90 multiplier
= multiplier
.unproxy(base
)
91 # assert that we are multiplying with a compatible
93 if not isinstance(multiplier
, params
.NumericParamValue
):
95 "Proxy multiplier must be a numerical param")
96 multiplier
= multiplier
.getValue()
97 result
= result
* multiplier
100 def unproxy(self
, base
):
104 if self
._search
_self
:
105 result
, done
= self
.find(obj
)
108 # Search up the tree but mark ourself
109 # as visited to avoid a self-reference
116 result
, done
= self
.find(obj
)
118 self
._visited
= False
119 base
._visited
= False
122 raise AttributeError(
123 "Can't resolve proxy '%s' of type '%s' from '%s'" % \
124 (self
.path(), self
._pdesc
.ptype_str
, base
.path()))
126 if isinstance(result
, BaseProxy
):
128 raise RuntimeError("Cycle in unproxy")
129 result
= result
.unproxy(obj
)
131 return self
._mulcheck
(result
, base
)
133 def getindex(obj
, index
):
141 # if index is 0 and item is not subscriptable, just
142 # use item itself (so cpu[0] works on uniprocessors)
144 getindex
= staticmethod(getindex
)
146 # This method should be called once the proxy is assigned to a
147 # particular parameter or port to set the expected type of the
149 def set_param_desc(self
, pdesc
):
152 class AttrProxy(BaseProxy
):
153 def __init__(self
, search_self
, search_up
, attr
):
154 super(AttrProxy
, self
).__init
__(search_self
, search_up
)
158 def __getattr__(self
, attr
):
159 # python uses __bases__ internally for inheritance
160 if attr
.startswith('_'):
161 return super(AttrProxy
, self
).__getattr
__(self
, attr
)
162 if hasattr(self
, '_pdesc'):
163 raise AttributeError("Attribute reference on bound proxy")
164 # Return a copy of self rather than modifying self in place
165 # since self could be an indirect reference via a variable or
167 new_self
= copy
.deepcopy(self
)
168 new_self
._modifiers
.append(attr
)
171 # support indexing on proxies (e.g., Self.cpu[0])
172 def __getitem__(self
, key
):
173 if not isinstance(key
, int):
174 raise TypeError("Proxy object requires integer index")
175 if hasattr(self
, '_pdesc'):
176 raise AttributeError("Index operation on bound proxy")
177 new_self
= copy
.deepcopy(self
)
178 new_self
._modifiers
.append(key
)
183 val
= getattr(obj
, self
._attr
)
185 if hasattr(val
, '_visited'):
186 visited
= getattr(val
, '_visited')
192 # for any additional unproxying to be done, pass the
193 # current, rather than the original object so that proxy
194 # has the right context
200 val
= val
.unproxy(obj
)
201 for m
in self
._modifiers
:
202 if isinstance(m
, str):
203 val
= getattr(val
, m
)
204 elif isinstance(m
, int):
207 assert("Item must be string or integer")
209 val
= val
.unproxy(obj
)
214 for m
in self
._modifiers
:
215 if isinstance(m
, str):
217 elif isinstance(m
, int):
220 assert("Item must be string or integer")
223 class AnyProxy(BaseProxy
):
225 return obj
.find_any(self
._pdesc
.ptype
)
230 # The AllProxy traverses the entire sub-tree (not only the children)
231 # and adds all objects of a specific type
232 class AllProxy(BaseProxy
):
234 return obj
.find_all(self
._pdesc
.ptype
)
241 if isinstance(obj
, (BaseProxy
, params
.EthernetAddr
)):
243 elif isinstance(obj
, (list, tuple)):
249 class ProxyFactory(object):
250 def __init__(self
, search_self
, search_up
):
251 self
.search_self
= search_self
252 self
.search_up
= search_up
254 def __getattr__(self
, attr
):
256 return AnyProxy(self
.search_self
, self
.search_up
)
259 assert("Parant.all is not supported")
260 return AllProxy(self
.search_self
, self
.search_up
)
262 return AttrProxy(self
.search_self
, self
.search_up
, attr
)
264 # global objects for handling proxies
265 Parent
= ProxyFactory(search_self
= False, search_up
= True)
266 Self
= ProxyFactory(search_self
= True, search_up
= False)
268 # limit exports on 'from proxy import *'
269 __all__
= ['Parent', 'Self']