# -*- 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', '
', value.strip_annotations().to_string())
return value.strip_annotations().to_string()
templateDef = '''#encoding utf-8
Register report
Date |
Payee |
Account |
Amount |
Total |
Date |
Payee |
Account |
Amount |
Total |
#for $xact in $journal
#for $post in $xact
#set $total = $total + $post.amount
$xact.date |
$xact.payee |
$post.account |
${strip($post.amount)} |
${strip($total)} |
#set $last_xact = $xact
#end for
#end for
'''
class LedgerHandler(BaseHTTPRequestHandler):
def __init__(self, *args):
self.journal = ledger.Journal(sys.argv[1])
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()
if __name__ == '__main__':
if len(sys.argv) < 2:
print "usage: server.py
"
sys.exit(1)
main()