Guest User

Untitled

a guest
Jun 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. from kivy.properties cimport Property, PropertyStorage
  2. from kivy._event cimport EventDispatcher
  3.  
  4. cdef inline void observable_object_dispatch(object self, str name):
  5. cdef Property prop = self.prop
  6. prop.dispatch(self.obj, name)
  7.  
  8.  
  9. class ObservableObject(object):
  10.  
  11. # Internal class to observe changes inside a native python object.
  12. def __init__(self, *largs):
  13. self.prop = largs[0]
  14. self.obj = largs[1]
  15. super(ObservableObject, self).__init__()
  16.  
  17. def __setattr__(self, name, value):
  18. object.__setattr__(self, name, value)
  19. observable_object_dispatch(self, name)
  20.  
  21.  
  22. cdef class DocProperty(Property):
  23.  
  24. def __init__(self, defaultvalue=None, rebind=False, **kw):
  25. self.baseclass = kw.get('baseclass', object)
  26. super(DocProperty, self).__init__(defaultvalue, **kw)
  27. self.rebind = rebind
  28.  
  29. cpdef link(self, EventDispatcher obj, str name):
  30. Property.link(self, obj, name)
  31. cdef PropertyStorage ps = obj.__storage[self._name]
  32. ps.value = ObservableObject(self, obj, ps.value)
  33.  
  34. cdef check(self, EventDispatcher obj, value):
  35. if Property.check(self, obj, value):
  36. return True
  37. if not isinstance(value, object):
  38. raise ValueError('{}.{} accept only object based on {}'.format(
  39. obj.__class__.__name__,
  40. self.name,
  41. self.baseclass.__name__))
  42.  
  43. cpdef dispatch(self, EventDispatcher obj, str name):
  44. '''Dispatch the value change to all observers.
  45. .. versionchanged:: 1.1.0
  46. The method is now accessible from Python.
  47. This can be used to force the dispatch of the property, even if the
  48. value didn't change::
  49. button = Button()
  50. # get the Property class instance
  51. prop = button.property('text')
  52. # dispatch this property on the button instance
  53. prop.dispatch(button)
  54. '''
  55. cdef PropertyStorage ps = obj.__storage[self._name]
  56. ps.observers.dispatch(obj, ps.value, (name,), None, 0)
  57.  
  58. from kivy.properties cimport Property, PropertyStorage
  59. from kivy._event cimport EventDispatcher
  60.  
  61. cdef class DocProperty(Property):
  62. cdef object baseclass
  63. cdef public int rebind
  64. cpdef dispatch(self, EventDispatcher obj, str name)
  65.  
  66. # -*- coding: utf-8 -*-
  67.  
  68. from kivy.event import EventDispatcher
  69. import pyximport
  70. pyximport.install()
  71. from properties import DocProperty
  72.  
  73. if __name__ == '__main__':
  74.  
  75. class ED(EventDispatcher):
  76.  
  77. doc = DocProperty()
  78.  
  79. def on_doc(self, obj, value):
  80. print 'printing doc', self.doc
  81.  
  82. class DumbObj(object):
  83.  
  84. def __init__(self, num):
  85. self._num = num
  86.  
  87. @property
  88. def num(self):
  89. return 5
  90.  
  91. @num.setter
  92. def num(self, value):
  93. self._num = value
  94.  
  95. ed = ED()
  96. ed.doc = DumbObj(3)
  97. ed.doc.num = 4
Add Comment
Please, Sign In to add comment