summaryrefslogtreecommitdiff
path: root/python/server.py
blob: 9aa9c518d4fcbe211aa9735a6c09c471718b2949 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# -*- coding: utf-8 -*-

import ledger
import cgi
import sys
import types
import posixpath
import urllib
import shutil
import os
import re

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from os.path import exists, join, isfile

from Cheetah.Template import Template
from Cheetah.Filters import Filter, WebSafe

webroot = join(os.getcwd(), 'python', 'res')

class UnicodeFilter(Filter):
    def filter(self, s, **kargs):
        return Filter.filter(self, s, str=unicode, **kargs)

def strip(value):
    #return re.sub('\n', '<br />', value.strip_annotations().to_string())
    return value.strip_annotations().to_string()

templateDef = '''#encoding utf-8
 <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>$title</title>
      <link rel="stylesheet" href="/style.css" type="text/css" media="print, projection, screen" />
      <script type="text/javascript" src="/jquery-latest.js"></script>
      <script type="text/javascript" src="/jquery.tablesorter.min.js"></script>
      <script type="text/javascript" src="/jquery.tablesorter.pager.js"></script>
      <script type="text/javascript" src="/jquery.dimensions.min.js"></script>
      <script type="text/javascript">
        \$(function() { 
          \$("table")
              .tablesorter({textExtraction: 'complex',
                            widthFixed:     true,
                            widgets:        ['zebra']}) 
              .tablesorterPager({size: 100,
                                 container: \$("\#pager")});
        }); 
      </script>
    </head>
    <body>
      <div id="main">
      <h1>Register report</h1>
      <table id="register" cellspacing="1" class="tablesorter">
        <thead>
          <tr>
            <th>Date</th>
            <th>Payee</th>
            <th>Account</th>
            <th>Amount</th>
            <th>Total</th>
          </tr>
        </thead>
        <tfoot>
          <tr>
            <th>Date</th>
            <th>Payee</th>
            <th>Account</th>
            <th>Amount</th>
            <th>Total</th>
          </tr>
        </tfoot>
        <tbody>
          #for $xact in $journal
          #for $post in $xact
          #set $total = $total + $post.amount
          <tr>
            <!--<td>${$xact.date if $xact is not $last_xact else $empty}</td>
            <td>${$xact.payee if $xact is not $last_xact else $empty}</td>-->
            <td>$xact.date</td>
            <td>$xact.payee</td>
            <td>$post.account</td>
            <td>${strip($post.amount)}</td>
            <td>${strip($total)}</td>
          </tr>
          #set $last_xact = $xact
          #end for
          #end for
        </tbody>
      </table>
      <div id="pager" class="pager">
        <form>
          <img src="/icons/first.png" class="first"/>
          <img src="/icons/prev.png" class="prev"/>
          <input type="text" class="pagedisplay"/>
          <img src="/icons/next.png" class="next"/>
          <img src="/icons/last.png" class="last"/>
          <select class="pagesize">
            <option selected="selected" value="100">100</option>
            <option value="200">200</option>
            <option value="500">500</option>
            <option  value="1000">1000</option>
          </select>
        </form>
      </div>
    </body>
  </html>
'''

class LedgerHandler(BaseHTTPRequestHandler):
    def __init__(self, *args):
        self.journal = ledger.Journal('/Users/johnw/src/ledger/doc/sample.dat')
        BaseHTTPRequestHandler.__init__(self, *args)

    def do_GET(self):
        path = self.translate_path(self.path)

        if path and exists(path) and isfile(path):
            self.copyfile(open(path), self.wfile)
        else:
            tmpl = Template(templateDef, filter=UnicodeFilter)

            tmpl.title     = 'Ledger Journal'
            tmpl.journal   = self.journal
            tmpl.total     = ledger.Value(0)
            tmpl.strip     = strip
            tmpl.last_xact = None
            tmpl.empty     = ""

            html = unicode(tmpl)
            html = html.encode('utf-8')
            self.wfile.write(html)

    def do_POST(self):
        print "Saw a POST request!"
        try:
            ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
            if ctype == 'multipart/form-data':
                query = cgi.parse_multipart(self.rfile, pdict)
            self.send_response(301)
            self.end_headers()
        except Exception:
            print "Saw exception in POST handler"

    # This code is straight from SimpleHTTPServer.py
    def copyfile(self, source, outputfile):
        """Copy all data between two file objects.

        The SOURCE argument is a file object open for reading
        (or anything with a read() method) and the DESTINATION
        argument is a file object open for writing (or
        anything with a write() method).

        The only reason for overriding this would be to change
        the block size or perhaps to replace newlines by CRLF
        -- note however that this the default server uses this
        to copy binary data as well.

        """
        shutil.copyfileobj(source, outputfile)

    def translate_path(self, path):
        """Translate a /-separated PATH to the local filename syntax.

        Components that mean special things to the local file system
        (e.g. drive or directory names) are ignored.  (XXX They should
        probably be diagnosed.)

        """
        # abandon query parameters
        path  = path.split('?',1)[0]
        path  = path.split('#',1)[0]
        path  = posixpath.normpath(urllib.unquote(path))
        words = path.split('/')
        words = filter(None, words)
        path  = webroot
        for word in words:
            drive, word = os.path.splitdrive(word)
            head, word = os.path.split(word)
            if word in (os.curdir, os.pardir): continue
            path = os.path.join(path, word)
        return path

def main(*args):
    try:
        port   = 9000
        server = HTTPServer(('', port), LedgerHandler)
        print "Local HTTP server listening on port %d... (Control-C to exit)" \
            % port
        server.serve_forever()
    except KeyboardInterrupt:
        print "Shutting down server"
        server.socket.close()

print __name__
if __name__ == '__main__':
    main()