def add_proxies(parent, obj): """Add proxies for methods and properties of C{obj} to C{parent}.""" # add functions and methods for key, value in _getmembers(obj): if key.startswith("_"): continue if isinstance(value, (_types.FunctionType, _types.MethodType)): setattr(parent, key, with_obj(parent, value)) # add properties and other descriptors (can be accessed via the class only) for key, value in _getmembers(type(obj)): if key.startswith("_"): continue if hasattr(value, "__get__") and not isinstance( value, collections.Callable): # exclude member functions setattr(parent, key, PropertyProxy(obj, key)) # add instance descriptors if applicable if isinstance(obj, InstanceDescriptorMixin): for key, value in list(obj.__dict__.items()): if key.startswith("_"): continue if hasattr(value, "__get__"): setattr(parent, key, PropertyProxy(obj, key))
def add_proxies(parent, obj): """Add proxies for methods and properties of C{obj} to C{parent}.""" # add functions and methods for key, value in _getmembers(obj): if key.startswith("_"): continue if isinstance(value, (_types.FunctionType, _types.MethodType)): setattr(parent, key, with_obj(parent, value)) # add properties and other descriptors (can be accessed via the class only) for key, value in _getmembers(type(obj)): if key.startswith("_"): continue if hasattr(value, "__get__") and not isinstance(value, collections.Callable): # exclude member functions setattr(parent, key, PropertyProxy(obj, key)) # add instance descriptors if applicable if isinstance(obj, InstanceDescriptorMixin): for key, value in list(obj.__dict__.items()): if key.startswith("_"): continue if hasattr(value, "__get__"): setattr(parent, key, PropertyProxy(obj, key))
def _make_block_thread_safe(cls): # have __init__ create a recursive lock __init_old__ = getattr(cls, '__init__') def __init_new__(self, *args, **kargs): __init_old__(self, *args, **kargs) self._rlock = _RLock() setattr(cls, '__init__', __init_new__) # protect each public method (or 'private' methods in the # 'non_public_methods' arg) with the recursive lock via FunctionObject for m,f in [i for i in _getmembers(cls, predicate=_isfunction) \ if i[0][0] != '_' or i[0] in non_public_methods]: c = FunctionObject(f) c.__doc__ = f.__doc__ c.__name__ = repr(c) setattr(cls, m, c) # override is_thread_safe @_doxtend(_TOSDB_DataBlock) def is_thread_safe(cls): return True setattr(cls, 'is_thread_safe', is_thread_safe) return cls
def _make_asserter(member): """ Make fun part of member an asserting function _and_ take ownership of the wrapper. The wrapped function should have a correct doc string, and a module attribute that shows it belongs to us instead of ``check``, which could be confusing. """ name, fun = member new_fun = assert_if_truthy(fun) if new_fun.__doc__: new_fun.__doc__ = new_fun.__doc__.replace("Check", "Assert") new_fun.__module__ = __name__ return (name, new_fun) def _is_exported_name(name): """Is ``name`` something that check wants exported.""" # If ``check`` ever switches to using the ``__all__`` mechanism, update this code: return not name.startswith("_") def _should_be_wrapped(member): name, obj = member return _is_exported_name(name) and _isfunction(obj) and _getmodule( obj) == _check globals().update( map(_make_asserter, filter(_should_be_wrapped, _getmembers(_check))))
_vACK = 'ACK' _vFAILURE = 'FAILURE' _vEXCEPTION = 'EXCEPTION' _vSUCCESS = 'SUCCESS' _vSUCCESS_NT = 'SUCCESS_NT' _vCONN_BLOCK = 'CONN_BLOCK' _vCONN_ADMIN = 'CONN_ADMIN' _vREQUIRE_AUTH = 'REQUIRE_AUTH' _vREQUIRE_AUTH_NO = 'REQUIRE_AUTH_NO' _vALLOWED_ADMIN = ('init', 'connect', 'connected', 'connection_state', 'clean_up', 'get_block_limit', 'set_block_limit', 'get_block_count', 'type_bits', 'type_string') # just the name, can't get bound method with ismethod pred (why?) _vALLOWED_METHS = tuple(m[0] for m in _getmembers(_TOSDB_DataBlock, predicate=_isfunction) \ if m[0][0] != '_') + ('__init__','__str__') ## !! _vDELIM MUST NOT HAVE THE SAME VALUE AS _vEEXOR !! ## _vDELIM = b'\x7E' _vESC = b'\x7D' _vDEXOR = chr(ord(_vDELIM) ^ ord(_vESC)).encode() _vEEXOR = chr(ord(_vESC) ^ ord(_vESC)).encode() # 0 # NOTE: for time being preface virtual calls and objects with 'v' so # we can load both on the windows side for easier debugging, def vinit(dllpath=None, root="C:\\"): """ Initialize the underlying tos-databridge DLL on host, try to connect.