Package toon :: Module web
[hide private]
[frames] | no frames]

Source Code for Module toon.web

  1  #!/usr/bin/env python 
  2  # 
  3  # Toonloop for Python 
  4  # 
  5  # Copyright 2008 Alexandre Quessy & Tristan Matthews 
  6  # <alexandre@quessy.net> & <le.businessman@gmail.com> 
  7  # http://www.toonloop.com 
  8  # 
  9  # Original idea by Alexandre Quessy 
 10  # http://alexandre.quessy.net 
 11  # 
 12  # Toonloop is free software: you can redistribute it and/or modify 
 13  # it under the terms of the GNU General Public License as published by 
 14  # the Free Software Foundation, either version 3 of the License, or 
 15  # (at your option) any later version. 
 16  # 
 17  # Toonloop is distributed in the hope that it will be useful, 
 18  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  # GNU General Public License for more details. 
 21  # 
 22  # You should have received a copy of the gnu general public license 
 23  # along with Toonloop.  If not, see <http://www.gnu.org/licenses/>. 
 24  # 
 25  """ 
 26  Toonloop Web server for files and RSS feeds. 
 27  """ 
 28  import os 
 29  import sys 
 30   
 31  from twisted.internet import reactor 
 32  from nevow import static 
 33  from nevow import appserver 
 34  from nevow import rend 
 35   
 36  from toon import rss 
 37  from toon import rst 
 38   
39 -class Index(rst.ReStructured, rend.Page):
40 """ 41 Class representing the root (/) of the web server. 42 """ 43 addSlash = True 44
45 - def __init__(self, toonloop, **kwargs):
46 self.static_files_path = os.curdir 47 self.index_file_path = os.path.join(os.path.dirname(__file__), 'data', 'index.rst') 48 self.port = 8000 49 self.__dict__.update(**kwargs) 50 51 print "Static files root:", self.static_files_path 52 try: 53 self.toonloop = toonloop 54 except AttributeError, e: 55 print 'web_server: Index', e.message 56 self.toonloop = None 57 try: 58 rst.ReStructured.__init__(self, self.index_file_path) 59 except IOError, e: 60 print "Error reading rst file.", e.message, self.index_file_path 61 # child_* attributes serve some static files 62 # TODO: RSS feed and web form 63 self.child_files = static.File(self.static_files_path) # os.path.join 64 self.child_data = static.File(os.path.join(os.path.dirname(__file__), 'data')) 65 self.child_rss = rss.RSSPage(port=self.port, root=self.static_files_path)
66
67 - def renderHTTP(self, request):
68 """ 69 Renders the / page 70 """ 71 return rst.ReStructured.render(self, request)
72
73 -def start(toonloop, port=8000, **kwargs):
74 """ 75 Called from the main application to start the web UI. 76 Config argmuments ARE overriden from the application. 77 78 :param toonloop: The application 79 :param port: web server port 80 """ 81 # These are default values and are overriden in the main toonloop script. 82 web_config = { 83 'static_files_path':os.path.expanduser("~/Documents/toonloop"), 84 'index_file_path':os.path.join(os.path.dirname(__file__), 'data', 'index.rst'), 85 'port':port 86 } 87 web_config.update(kwargs) 88 #Index.static_files_path = web_config['static_files_path'] 89 #Index.index_file_path = web_config['index_file_path'] 90 site = appserver.NevowSite(Index(toonloop, **web_config)) 91 if toonloop.config.verbose: 92 print 'Starting web server on port', port 93 print 'Static Documentation Files Path : ', web_config['index_file_path'] 94 reactor.listenTCP(port, site) 95 96 if __name__ == '__main__': 97 # print sys.path 98 start(None) 99 try: 100 reactor.run() 101 except: 102 raise 103