Mercurial > MadButterfly
comparison pyink/MBScene.py @ 1248:2f9fa5d59e67
Add data_monitor meta-class to monitor accessing on domview_ui.
- The monitoring is to prevent from reentry triggered by DOM events.
- call 'do*' methods of a monitored class would try to lock
domview_ui at first.
- The method would not be executed actually if being fault to lock.
- You can make monitor to print debug message with environment variable
- DATA_MONITOR_DBG=1, or
- DATA_MONITOR_DBG=2
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Tue, 11 Jan 2011 10:04:14 +0800 |
parents | 42c4874c8d1e |
children | 16a1166c3850 |
comparison
equal
deleted
inserted
replaced
1247:45e9566ea5c0 | 1248:2f9fa5d59e67 |
---|---|
35 # scene is 10, we will set the scene field as "8-10". | 35 # scene is 10, we will set the scene field as "8-10". |
36 # - 5. If this scene are filled screne, we will split the existing | 36 # - 5. If this scene are filled screne, we will split the existing |
37 # scene into two scenes with the same content. | 37 # scene into two scenes with the same content. |
38 # | 38 # |
39 | 39 |
40 ## \brief Monitor accessing on the dta model. | |
41 # | |
42 # This class is a meta-class that monitor data accessing for its instance | |
43 # classes. | |
44 # | |
45 # All methods, of instance classes, who's name is prefixed with 'do' are | |
46 # monitored. When a monitored method was called, monitor will try to lock | |
47 # _domview of the object. The method is only called if successfully acquiring | |
48 # the lock, or return immediately. The lock is released after the calling | |
49 # returned. | |
50 # | |
51 class data_monitor(type): | |
52 def __new__(mclazz, name, bases, clazz_dict): | |
53 import os | |
54 | |
55 debug_level = 0 | |
56 if os.environ.has_key('DATA_MONITOR_DBG'): | |
57 debug_level = int(os.environ['DATA_MONITOR_DBG']) | |
58 pass | |
59 | |
60 def gen_sentinel(name, func): | |
61 def sentinel(self, *args, **kws): | |
62 if debug_level >= 1: | |
63 print 'calling %s' % (name) | |
64 pass | |
65 if debug_level >= 2: | |
66 print ' args: %s' % (repr(args)) | |
67 print ' kws: %s' % (repr(kws)) | |
68 pass | |
69 | |
70 if not self._domview.lock(): # can not lock | |
71 return | |
72 | |
73 try: | |
74 func(self, *args, **kws) | |
75 finally: | |
76 self._domview.unlock() | |
77 pass | |
78 pass | |
79 return sentinel | |
80 | |
81 for attr_name in clazz_dict: | |
82 if (not attr_name.startswith('do')) or \ | |
83 (not callable(clazz_dict[attr_name])): | |
84 continue | |
85 clazz_dict[attr_name] = \ | |
86 gen_sentinel(attr_name, clazz_dict[attr_name]) | |
87 pass | |
88 | |
89 clazz = type.__new__(mclazz, name, bases, clazz_dict) | |
90 | |
91 return clazz | |
92 pass | |
93 | |
40 ## \brief MBScene connect GUI and DOM-tree | 94 ## \brief MBScene connect GUI and DOM-tree |
41 # | 95 # |
42 # This method accepts user actions and involves domview_ui to update | 96 # This method accepts user actions and involves domview_ui to update |
43 # data on the document. | 97 # data on the document. |
44 # | 98 # |
45 class MBScene(object): | 99 class MBScene(object): |
100 __metaclass__ = data_monitor | |
101 | |
46 _tween_type_names = ('normal', 'scale') | 102 _tween_type_names = ('normal', 'scale') |
47 | 103 |
48 _num_frames_of_line = 100 | 104 _num_frames_of_line = 100 |
49 | 105 |
50 def __init__(self, desktop, win, root=None): | 106 def __init__(self, desktop, win, root=None): |