Package proton :: Module wrapper
[frames] | no frames]

Source Code for Module proton.wrapper

  1  # 
  2  # Licensed to the Apache Software Foundation (ASF) under one 
  3  # or more contributor license agreements.  See the NOTICE file 
  4  # distributed with this work for additional information 
  5  # regarding copyright ownership.  The ASF licenses this file 
  6  # to you under the Apache License, Version 2.0 (the 
  7  # "License"); you may not use this file except in compliance 
  8  # with the License.  You may obtain a copy of the License at 
  9  # 
 10  #   http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  # Unless required by applicable law or agreed to in writing, 
 13  # software distributed under the License is distributed on an 
 14  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
 15  # KIND, either express or implied.  See the License for the 
 16  # specific language governing permissions and limitations 
 17  # under the License. 
 18  # 
 19  from cproton import * 
 20   
21 -class EmptyAttrs:
22
23 - def __contains__(self, name):
24 return False
25
26 - def __getitem__(self, name):
27 raise KeyError(name)
28
29 - def __setitem__(self, name, value):
30 raise TypeError("does not support item assignment")
31 32 EMPTY_ATTRS = EmptyAttrs() 33
34 -class Wrapper(object):
35
36 - def __init__(self, impl_or_constructor, get_context=None):
37 init = False 38 if callable(impl_or_constructor): 39 # we are constructing a new object 40 impl = impl_or_constructor() 41 init = True 42 else: 43 # we are wrapping an existing object 44 impl = impl_or_constructor 45 pn_incref(impl) 46 47 if get_context: 48 record = get_context(impl) 49 attrs = pn_void2py(pn_record_get(record, PYCTX)) 50 if attrs is None: 51 attrs = {} 52 pn_record_def(record, PYCTX, PN_PYREF) 53 pn_record_set(record, PYCTX, pn_py2void(attrs)) 54 init = True 55 else: 56 attrs = EMPTY_ATTRS 57 init = False 58 self.__dict__["_impl"] = impl 59 self.__dict__["_attrs"] = attrs 60 if init: self._init()
61
62 - def __getattr__(self, name):
63 attrs = self.__dict__["_attrs"] 64 if name in attrs: 65 return attrs[name] 66 else: 67 raise AttributeError(name)
68
69 - def __setattr__(self, name, value):
70 if hasattr(self.__class__, name): 71 object.__setattr__(self, name, value) 72 else: 73 attrs = self.__dict__["_attrs"] 74 attrs[name] = value
75
76 - def __delattr__(self, name):
77 attrs = self.__dict__["_attrs"] 78 if attrs: 79 del attrs[name]
80
81 - def __hash__(self):
82 return hash(addressof(self._impl))
83
84 - def __cmp__(self, other):
85 if isinstance(other, Wrapper): 86 return cmp(addressof(self._impl), addressof(other._impl)) 87 else: 88 return -1
89
90 - def __del__(self):
91 pn_decref(self._impl)
92
93 - def __repr__(self):
94 return '<%s.%s 0x%x ~ 0x%x>' % (self.__class__.__module__, 95 self.__class__.__name__, 96 id(self), addressof(self._impl))
97 98 99 if pn_py2void(Wrapper) is Wrapper: 100 PYCTX = Wrapper 101 import java.lang.System 102 addressof = java.lang.System.identityHashCode 103 else: 104 PYCTX = int(pn_py2void(Wrapper)) 105 addressof = int 106