server.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # -*- coding: utf-8 -*-
  2. import ledger
  3. import cgi
  4. import sys
  5. import types
  6. import posixpath
  7. import urllib
  8. import shutil
  9. import os
  10. import re
  11. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  12. from os.path import exists, join, isfile
  13. from Cheetah.Template import Template
  14. from Cheetah.Filters import Filter, WebSafe
  15. webroot = join(os.getcwd(), 'python', 'res')
  16. class UnicodeFilter(Filter):
  17. def filter(self, s, **kargs):
  18. return Filter.filter(self, s, str=unicode, **kargs)
  19. def strip(value):
  20. #return re.sub('\n', '<br />', value.strip_annotations().to_string())
  21. return value.strip_annotations().to_string()
  22. templateDef = '''#encoding utf-8
  23. <html>
  24. <head>
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <title>$title</title>
  27. <link rel="stylesheet" href="/style.css" type="text/css" media="print, projection, screen" />
  28. <script type="text/javascript" src="/jquery-latest.js"></script>
  29. <script type="text/javascript" src="/jquery.tablesorter.min.js"></script>
  30. <script type="text/javascript" src="/jquery.tablesorter.pager.js"></script>
  31. <script type="text/javascript" src="/jquery.dimensions.min.js"></script>
  32. <script type="text/javascript">
  33. \$(function() {
  34. \$("table")
  35. .tablesorter({textExtraction: 'complex',
  36. widthFixed: true,
  37. widgets: ['zebra']})
  38. .tablesorterPager({size: 100,
  39. container: \$("\#pager")});
  40. });
  41. </script>
  42. </head>
  43. <body>
  44. <div id="main">
  45. <h1>Register report</h1>
  46. <table id="register" cellspacing="1" class="tablesorter">
  47. <thead>
  48. <tr>
  49. <th>Date</th>
  50. <th>Payee</th>
  51. <th>Account</th>
  52. <th>Amount</th>
  53. <th>Total</th>
  54. </tr>
  55. </thead>
  56. <tfoot>
  57. <tr>
  58. <th>Date</th>
  59. <th>Payee</th>
  60. <th>Account</th>
  61. <th>Amount</th>
  62. <th>Total</th>
  63. </tr>
  64. </tfoot>
  65. <tbody>
  66. #for $post in $posts
  67. #set $total = $total + $post.amount
  68. <tr>
  69. <!--<td>${$post.xact.date if $post.xact is not $last_xact else $empty}</td>
  70. <td>${$post.xact.payee if $post.xact is not $last_xact else $empty}</td>-->
  71. <td>$post.xact.date</td>
  72. <td>$post.xact.payee</td>
  73. <td>$post.account</td>
  74. <td>${strip($post.amount)}</td>
  75. <td>${strip($total)}</td>
  76. </tr>
  77. #set $last_xact = $post.xact
  78. #end for
  79. </tbody>
  80. </table>
  81. <div id="pager" class="pager">
  82. <form>
  83. <img src="/icons/first.png" class="first"/>
  84. <img src="/icons/prev.png" class="prev"/>
  85. <input type="text" class="pagedisplay"/>
  86. <img src="/icons/next.png" class="next"/>
  87. <img src="/icons/last.png" class="last"/>
  88. <select class="pagesize">
  89. <option selected="selected" value="40">40</option>
  90. <option value="100">100</option>
  91. <option value="200">200</option>
  92. <option value="300">300</option>
  93. </select>
  94. </form>
  95. </div>
  96. </body>
  97. </html>
  98. '''
  99. class LedgerHandler(BaseHTTPRequestHandler):
  100. def __init__(self, *args):
  101. self.journal = ledger.Journal(sys.argv[1])
  102. BaseHTTPRequestHandler.__init__(self, *args)
  103. def do_GET(self):
  104. path = self.translate_path(self.path)
  105. if path and exists(path) and isfile(path):
  106. self.copyfile(open(path), self.wfile)
  107. else:
  108. tmpl = Template(templateDef, filter=UnicodeFilter)
  109. tmpl.title = 'Ledger Journal'
  110. tmpl.posts = self.journal.collect(sys.argv[2])
  111. tmpl.total = ledger.Value(0)
  112. tmpl.strip = strip
  113. tmpl.last_xact = None
  114. tmpl.empty = ""
  115. html = unicode(tmpl)
  116. html = html.encode('utf-8')
  117. self.wfile.write(html)
  118. def do_POST(self):
  119. print "Saw a POST request!"
  120. try:
  121. ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
  122. if ctype == 'multipart/form-data':
  123. query = cgi.parse_multipart(self.rfile, pdict)
  124. self.send_response(301)
  125. self.end_headers()
  126. except Exception:
  127. print "Saw exception in POST handler"
  128. # This code is straight from SimpleHTTPServer.py
  129. def copyfile(self, source, outputfile):
  130. """Copy all data between two file objects.
  131. The SOURCE argument is a file object open for reading
  132. (or anything with a read() method) and the DESTINATION
  133. argument is a file object open for writing (or
  134. anything with a write() method).
  135. The only reason for overriding this would be to change
  136. the block size or perhaps to replace newlines by CRLF
  137. -- note however that this the default server uses this
  138. to copy binary data as well.
  139. """
  140. shutil.copyfileobj(source, outputfile)
  141. def translate_path(self, path):
  142. """Translate a /-separated PATH to the local filename syntax.
  143. Components that mean special things to the local file system
  144. (e.g. drive or directory names) are ignored. (XXX They should
  145. probably be diagnosed.)
  146. """
  147. # abandon query parameters
  148. path = path.split('?',1)[0]
  149. path = path.split('#',1)[0]
  150. path = posixpath.normpath(urllib.unquote(path))
  151. words = path.split('/')
  152. words = filter(None, words)
  153. path = webroot
  154. for word in words:
  155. drive, word = os.path.splitdrive(word)
  156. head, word = os.path.split(word)
  157. if word in (os.curdir, os.pardir): continue
  158. path = os.path.join(path, word)
  159. return path
  160. def main(*args):
  161. try:
  162. port = 9000
  163. server = HTTPServer(('', port), LedgerHandler)
  164. print "Local HTTP server listening on port %d... (Control-C to exit)" \
  165. % port
  166. server.serve_forever()
  167. except KeyboardInterrupt:
  168. print "Shutting down server"
  169. server.socket.close()
  170. if __name__ == '__main__':
  171. if len(sys.argv) < 3:
  172. print "usage: server.py <DATA-FILE> <REPORT-QUERY>"
  173. sys.exit(1)
  174. main()