source: SHX/trunk/SeismicHandler/basics/messages.py @ 1062

Revision 1062, 5.1 KB checked in by klaus, 4 years ago (diff)

timeit decorator for runtime info

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Author Revision Date
Line 
1# -*- coding: utf-8 -*-
2
3#    This file is part of Seismic Handler eXtended (SHX). For terms of use and
4#    license information please see license.txt and visit
5#    http://www.seismic-handler.org/portal/wiki/Shx/LicenseTerms
6
7"""
8Simple message system using pubsub as basis. pubsub details should be hidden
9from client software, since this system is maybe changed in later versions.
10"""
11
12import uuid
13import time
14from SeismicHandler.utils.pubsub import pub
15from SeismicHandler.basics.tools import AttributeBlock, timeit
16
17# shortcut
18msgs = MessageSystem = pub
19
20# place to store messages
21hive = {}
22
23@timeit
24def log_message(level, *message):
25    """
26    Small helper method for logging via messaging system.
27    Considerable execution time goes into this. Check or disable!
28    """
29
30    message = " ".join(map(str, message))
31    MessageSystem.sendMessage("log.%s" % level, message=message)
32    #pass
33
34
35@timeit
36def _set_runtime_var(name, value):
37    """
38    Helper function for setting runtime variables.
39    """
40    msgs.sendMessage("setruntime", name=name, value=value)
41
42
43
44@timeit
45def _get_runtime_var(name, default):
46    "Return value of runtime variable."
47    def receiver(value, msgid):
48        global hive
49
50        if value is None:
51            value = default
52
53        # store in global var
54        hive[msgid] = value
55
56        # unsubscribe
57        msgs.unsubscribe(receiver, msgid)
58   
59    runtime_a = time.time()
60    msgid = "gR%s" % uuid.uuid4().hex
61    runtime_0 = time.time()
62    msgs.subscribe(receiver, msgid)
63    runtime_b = time.time()
64    topic = msgs.getTopic(msgid)
65    runtime_c = time.time()
66
67    msgs.sendMessage("getruntime", name=name, msgid=msgid)
68    runtime_d = time.time()
69
70    # waiting for receiver drop out of topic
71    while topic.getListeners():
72        time.sleep(0.01)
73    runtime_e = time.time()
74
75    #print "runtime runtimevar %s %g 0: %g w: %g x: %g y: %g z: %g" % (name,
76    #    (runtime_e-runtime_a),(runtime_0-runtime_a),(runtime_b-runtime_0),(runtime_c-runtime_b),
77    #    (runtime_d-runtime_c),(runtime_e-runtime_d))
78    return hive.pop(msgid)
79
80
81def get_runtime(name, default=None):
82    """
83    Helper function for getting "RUNTIME" variables.
84    """
85    name = "RUNTIME:%s" % name.lower()
86    res = _get_runtime_var(name, default)
87
88    if default is None and res is None:
89        raise NameError("Runtime variable '%s' not found!" % name)
90    else:
91        return res
92
93
94def set_runtime(name, value):
95    """
96    Helper function for setting "RUNTIME" variables.
97    """
98    name = "RUNTIME:%s" % name.lower()
99    _set_runtime_var(name, value)
100
101
102def set_status(value):
103    """
104    Convenience function for setting status flag.
105    """
106    _set_runtime_var("status", value)
107
108
109def trigger_redraw():
110    """
111    Helper method for sending "redraw" messsage.
112    """
113    send_event("redraw")
114
115@timeit
116def send_event(name, payload=None):
117    """
118    Simple event propagating method.
119    """
120    msgs.sendMessage("EVENT:%s" % name, payload=payload)
121    log_message("debug.message", "Event '%s' sent." % name)
122
123@timeit
124def ui_event(name, payload=None, wait=False):
125    """
126    Send UI event. If "wait" ist True, first is checked if there's a
127    receiver at all. If so, wait for response.
128    """
129    def receiver(value, msgid):
130        global hive
131
132        # store in global var
133        hive[msgid] = value
134
135        # unsubscribe
136        msgs.unsubscribe(receiver, msgid)
137
138    event = "GUI:%s" % name
139   
140    if wait:
141        # check for listeners
142        if event not in msgs.topicsMap:
143            raise NameError("no listeners active on '%s'" % event)
144
145        msgid = "UI%s" % uuid.uuid4().hex
146        msgs.subscribe(receiver, msgid)
147        topic = msgs.getTopic(msgid)
148    else:
149        msgid = None
150
151    msgs.sendMessage(event, payload=payload, msgid=msgid)
152    msg = "UI-Event '%s' sent (payload: %s)."
153    log_message("debug.message",  msg % (name, payload))
154
155    # waiting for receiver drop out of topic
156    if wait:
157        while topic.getListeners():
158            time.sleep(0.01)
159
160        return hive.pop(msgid)
161
162@timeit
163def subscribe_event(method, event):
164    msgs.subscribe(method, "EVENT:%s" % event)
165
166@timeit
167def subscribe_ui_event(method, event):
168    msgs.subscribe(method, "GUI:%s" % event)
169
170@timeit
171def set_style(block, item=None, value=None):
172    """
173    Helper method for setting style information.
174    """
175    styles = get_runtime("styles")
176    block = block.upper()
177
178    if block in styles.keys():
179        if item is None:
180            styles[block] = value
181        else:
182            styles[block.upper()][item.upper()] = value
183        set_runtime("styles", styles)
184    else:
185        # build attribute block
186        attribs = get_runtime("attrib")
187        kwargs = {item: value}
188        a = attribs.get(block, AttributeBlock(**kwargs))
189        setattr(a, item.lower(), value)
190        attribs[block.upper()] = a
191        set_runtime("attrib", attribs)
192
193@timeit
194def get_style(block, item=None):
195    """
196    Helper method for getting style information
197    """
198    styles = get_runtime("styles")
199
200    if block.upper() not in styles.keys():
201        styles = get_runtime("attrib")
202
203    if item is None:
204        return styles[block.upper()]
205    else:
206        return styles[block.upper()][item.upper()]
207
Note: See TracBrowser for help on using the repository browser.