public
Description: Python interface to backpack.
Homepage: http://bleu.west.spy.net/~dustin/projects/backpack/
Clone URL: git://github.com/dustin/py-backpack.git
py-backpack / wapsupport.py
100644 60 lines (45 sloc) 1.43 kb
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env /usr/local/bin/python
"""
WML support.
 
Copyright (c) 2005 Dustin Sallings <dustin@spy.net>
"""
 
import sys
import cgi
import ConfigParser
 
import backpack
 
CONFIG_FILE="/usr/local/etc/backpack.conf"
 
# Get the config loaded
conf=ConfigParser.ConfigParser()
conf.read(CONFIG_FILE)
 
HEADER="""<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC
  "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
"""
 
def sendContent(data):
    """Send the content as wml"""
    sys.stdout.write("Content-type: text/vnd.wap.wml\n")
    toSend=HEADER + data
    sys.stdout.write("Content-length: %d\n\n" % len(toSend))
    sys.stdout.write(toSend)
 
def wml(s):
    """Wrap the contents in wml tags."""
    return "<wml>%s</wml>" % (s,)
 
def card(id, title, s):
    """Build a card by ID."""
    return """<card id="%(id)s" title="%(title)s"><p>%(s)s</p></card>""" % \
        {'id': id, 'title': title, 's': s}
 
def handleException(tvt):
    """Print out any exception that may occur."""
    type, value, tb = tvt
 
    sendContent(wml(card("error", "Error",
        "<b>Got an error:</b><br/> %s" % (value,))))
 
def doCallback(funcs):
    """Execute the action."""
    fs=cgi.FieldStorage()
    bp=backpack.Backpack(conf.get("backpack", "url"),
        conf.get("backpack", "key"))
 
    action=funcs[fs.getvalue("action", "list")]
 
    try:
        action(bp, fs)
    except:
        handleException(sys.exc_info())