Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
# Sugar interface for Enyo JavaScript framework
5
6
import logging
7
import json
8
import inspect
9
10
11
class Enyo:
12
    """Class to handle communication with Enyo"""
13
14
    def __init__(self, webview):
15
        """Constructor: link to the WebKit widget"""
16
        self.webview = webview
17
        self.handlers = {}
18
        self.webview.connect("console-message", self._message_emitted)
19
20
    def connect(self, name, callback):
21
        """Add a new handler for an event"""
22
        self.handlers[name] = callback
23
24
    def send_message(self, name, args=None):
25
        """Send a message to Enyo"""
26
        script = "enyo.Sugar.sendMessage('"+name+"', "
27
        if not args is None:
28
            value = "'"+self.json_encode(args)+"'"
29
        else:
30
            value = "null"
31
        script = script+value+")"
32
        logging.warning("sugar://"+name+"/"+value)
33
        return self.webview.execute_script(script)
34
35
    def _message_emitted(self, widget, value, line, source):
36
        """Raised when a message from Enyo has been received"""
37
        # Only consider prefixed message
38
        prefix = "enyo://"
39
        if not value.startswith(prefix):
40
            return False
41
42
        # Get name
43
        prefixlen = len(prefix)
44
        size = value[prefixlen:prefixlen+value[prefixlen:].index("/")]
45
        start = prefixlen+1+len(size)
46
        name = value[start:start+int(size)]
47
48
        # Get param
49
        start = start + len(name)+1
50
        size = value[start:start+value[start:].index("/")]
51
        if int(size) == 0:
52
            args = None
53
        else:
54
            start = start+1+len(size)
55
            args = value[start:start+int(size)]
56
57
        # Call handler if exist
58
        logging.warning(value);
59
        if name in self.handlers:
60
            callback = self.handlers[name]
61
            if args:
62
                callback(json.loads(args))
63
            else:
64
                callback(None)
65
66
        return True
67
68
    def json_encode(self, obj):
69
        """Encode object as a JSON string"""
70
        try:
71
            result = json.dumps(obj)
72
        except TypeError:
73
            result = "{"
74
            first = True
75
            for name in dir(obj):
76
                value = getattr(obj, name)
77
                if not name.startswith('__') and not inspect.ismethod(value) and not inspect.isroutine(value) and not inspect.isbuiltin(value) and not isinstance(value, obj.__class__):
78
                    if not first:
79
                        result = result + ', '
80
                    else:
81
                        first = False
82
                    result = result + '"'+name+'": '
83
                    result = result + self.json_encode(value)
84
            result = result + "}"
85
        return result