kss.py 2.16 KB
Newer Older
Thimo Kraemer's avatar
Thimo Kraemer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
########################################################################
#
#  kss.py - Server-side KSS library
#
#  Copyright (c) 2017 joonis new media
#  Author: Thimo Kraemer <thimo.kraemer@joonis.de>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
########################################################################

"""
Usage:

Thimo Kraemer's avatar
Thimo Kraemer committed
28
# Create a response instance
Thimo Kraemer's avatar
Thimo Kraemer committed
29 30
kss = KSSResponse();

Thimo Kraemer's avatar
Thimo Kraemer committed
31
# Add a command
Thimo Kraemer's avatar
Thimo Kraemer committed
32 33 34 35 36
kss.add_command('setNodeAttr', '#my-selector',
    name='foo',
    value='bar',
    )

Thimo Kraemer's avatar
Thimo Kraemer committed
37
# Or via dynamic method
Thimo Kraemer's avatar
Thimo Kraemer committed
38 39 40 41 42
kss.setNodeAttr('#my-selector', 
    name='foo',
    value='bar',
    )

Thimo Kraemer's avatar
Thimo Kraemer committed
43
# Commands that do not require a selector
Thimo Kraemer's avatar
Thimo Kraemer committed
44 45
kss.add_command('alert', message='Hello!')

Thimo Kraemer's avatar
Thimo Kraemer committed
46
# Or via dynamic method
Thimo Kraemer's avatar
Thimo Kraemer committed
47 48
kss.alert(message='Hello!')

Thimo Kraemer's avatar
Thimo Kraemer committed
49
# Return the KSS response as JSON
Thimo Kraemer's avatar
Thimo Kraemer committed
50 51
return str(kss)

Thimo Kraemer's avatar
Thimo Kraemer committed
52
# Or return it within an RPC service
Thimo Kraemer's avatar
Thimo Kraemer committed
53
return kss.response
Thimo Kraemer's avatar
Thimo Kraemer committed
54 55 56 57

# Or from a Django view
return JsonResponse(kss.response, charset='utf-8')

Thimo Kraemer's avatar
Thimo Kraemer committed
58 59 60 61 62 63 64 65 66
"""

import json
from functools import partial

__all__ = ['KSSResponse']

class KSSResponse(object):
    
Thimo Kraemer's avatar
Thimo Kraemer committed
67
    def __init__(self):
Thimo Kraemer's avatar
Thimo Kraemer committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        self.response = {'commands': []}
    
    def add_command(self, _action, _selector=None, **_params):
        self.response['commands'].append({
            'action': _action,
            'selector': _selector,
            'params': _params,
            })
        
    def __getattr__(self, name):
        if name.startswith('_'):
            raise AttributeError(name)
        return partial(self.add_command, name)
        
    def __str__(self):
Thimo Kraemer's avatar
Thimo Kraemer committed
83
        return json.dumps(self.response)