Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/wirerope/wire.py: 75%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

40 statements  

1""":mod:`wirerope.wire` --- end-point instant for each bound method 

2=================================================================== 

3""" 

4import six 

5import types 

6from .callable import Descriptor 

7from ._compat import functools 

8 

9__all__ = 'Wire', 

10 

11 

12@functools.singledispatch 

13def descriptor_bind(descriptor, obj, type_): 

14 binder = Descriptor(descriptor).detect_binder(obj, type_) 

15 return binder(descriptor, obj, type_) 

16 

17 

18@descriptor_bind.register(types.FunctionType) 

19def descriptor_bind_function(descriptor, obj, type): 

20 return obj, obj 

21 

22 

23class Wire(object): 

24 """The core data object for each function for bound method. 

25 

26 Inherit this class to implement your own Wire classes. 

27 

28 - For normal functions, each function is directly wrapped by **Wire**. 

29 - For any methods or descriptors (including classmethod, staticmethod), 

30 each one is wrapped by :class:`wirerope.rope.MethodRopeMixin` 

31 and it creates **Wire** object for each bound object. 

32 """ 

33 

34 __slots__ = ( 

35 '_rope', '_callable', '_binding', '__func__', '_owner', 

36 '_bound_objects') 

37 

38 def __init__(self, rope, owner, binding): 

39 self._rope = rope 

40 self._callable = rope.callable 

41 self._owner = owner 

42 self._binding = binding 

43 if binding: 

44 func = self._callable.wrapped_object.__get__ 

45 if self._callable.is_property: 

46 wrapped = functools.partial(func, *binding) 

47 if six.PY2: 

48 # functools.wraps requires those attributes but 

49 # py2 functools.partial doesn't have them 

50 wrapped.__module__ = owner.__module__ 

51 wrapped.__name__ = func.__name__ 

52 self.__func__ = wrapped 

53 else: 

54 self.__func__ = func(*binding) 

55 else: 

56 self.__func__ = self._callable.wrapped_object 

57 if self._binding is None: 

58 self._bound_objects = () 

59 else: 

60 _, binder = descriptor_bind( 

61 self._callable.wrapped_object, *self._binding) 

62 if binder is not None: 

63 self._bound_objects = (binder, ) 

64 else: 

65 self._bound_objects = () 

66 assert callable(self.__func__), self.__func__ 

67 if rope._wrapped: 

68 functools.wraps(self.__func__)(self) 

69 

70 def _on_property(self): 

71 return self.__func__()