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

Revision 1066, 4.9 KB checked in by klaus, 4 years ago (diff)

forgot to check in other timeit changes

  • 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    """--MA--
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    """--MA--
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    """--MA--
47    Return value of runtime variable.
48    """
49    def receiver(value, msgid):
50        global hive
51
52        if value is None:
53            value = default
54
55        # store in global var
56        hive[msgid] = value
57
58        # unsubscribe
59        msgs.unsubscribe(receiver, msgid)
60   
61    msgid = "gR%s" % uuid.uuid4().hex
62    msgs.subscribe(receiver, msgid)
63    topic = msgs.getTopic(msgid)
64
65    msgs.sendMessage("getruntime", name=name, msgid=msgid)
66    #print "messages.debug: get_runtime '%s'" % name, \
67    #    sys._current_frames().values()[0].f_back.f_globals['__file__']
68
69    # waiting for receiver drop out of topic
70    while topic.getListeners():
71        time.sleep(0.01)
72
73    return hive.pop(msgid)
74
75
76def get_runtime(name, default=None):
77    """
78    Helper function for getting "RUNTIME" variables.
79    """
80    name = "RUNTIME:%s" % name.lower()
81    res = _get_runtime_var(name, default)
82
83    if default is None and res is None:
84        raise NameError("Runtime variable '%s' not found!" % name)
85    else:
86        return res
87
88
89def set_runtime(name, value):
90    """
91    Helper function for setting "RUNTIME" variables.
92    """
93    name = "RUNTIME:%s" % name.lower()
94    _set_runtime_var(name, value)
95
96
97def set_status(value):
98    """
99    Convenience function for setting status flag.
100    """
101    _set_runtime_var("status", value)
102
103
104def trigger_redraw():
105    """
106    Helper method for sending "redraw" messsage.
107    """
108    send_event("redraw")
109
110#@timeit
111def send_event(name, payload=None):
112    """--MA--
113    Simple event propagating method.
114    """
115    msgs.sendMessage("EVENT:%s" % name, payload=payload)
116    log_message("debug.message", "Event '%s' sent." % name)
117
118#@timeit
119def ui_event(name, payload=None, wait=False):
120    """
121    Send UI event. If "wait" ist True, first is checked if there's a
122    receiver at all. If so, wait for response.
123    """
124    def receiver(value, msgid):
125        global hive
126
127        # store in global var
128        hive[msgid] = value
129
130        # unsubscribe
131        msgs.unsubscribe(receiver, msgid)
132
133    event = "GUI:%s" % name
134   
135    if wait:
136        # check for listeners
137        if event not in msgs.topicsMap:
138            raise NameError("no listeners active on '%s'" % event)
139
140        msgid = "UI%s" % uuid.uuid4().hex
141        msgs.subscribe(receiver, msgid)
142        topic = msgs.getTopic(msgid)
143    else:
144        msgid = None
145
146    msgs.sendMessage(event, payload=payload, msgid=msgid)
147    msg = "UI-Event '%s' sent (payload: %s)."
148    log_message("debug.message",  msg % (name, payload))
149
150    # waiting for receiver drop out of topic
151    if wait:
152        while topic.getListeners():
153            time.sleep(0.01)
154
155        return hive.pop(msgid)
156
157#@timeit
158def subscribe_event(method, event):
159    msgs.subscribe(method, "EVENT:%s" % event)
160
161#@timeit
162def subscribe_ui_event(method, event):
163    msgs.subscribe(method, "GUI:%s" % event)
164
165#@timeit
166def set_style(block, item=None, value=None):
167    """
168    Helper method for setting style information.
169    """
170    styles = get_runtime("styles")
171    block = block.upper()
172
173    if block in styles.keys():
174        if item is None:
175            styles[block] = value
176        else:
177            styles[block.upper()][item.upper()] = value
178        set_runtime("styles", styles)
179    else:
180        # build attribute block
181        attribs = get_runtime("attrib")
182        kwargs = {item: value}
183        a = attribs.get(block, AttributeBlock(**kwargs))
184        setattr(a, item.lower(), value)
185        attribs[block.upper()] = a
186        set_runtime("attrib", attribs)
187
188#@timeit
189def get_style(block, item=None):
190    """
191    Helper method for getting style information
192    """
193    styles = get_runtime("styles")
194
195    if block.upper() not in styles.keys():
196        styles = get_runtime("attrib")
197
198    if item is None:
199        return styles[block.upper()]
200    else:
201        return styles[block.upper()][item.upper()]
202
Note: See TracBrowser for help on using the repository browser.