pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Python pastebin - collaborative debugging tool View Help


Posted by psichron on Thu 3 Jul 00:55
report abuse | download | new post

  1. # Import all the stuff that is needed
  2. from nevow import athena, loaders, tags as T, inevow
  3. from nevow import appserver, rend
  4.  
  5. from twisted.python import filepath
  6. from twisted.application import service, internet
  7. from twisted.internet import reactor
  8.  
  9. class AlertElement(athena.LiveElement):
  10.     '''
  11.    This is the class that will control the behaviour of the Alert element (essentially the table that rows gets added to)
  12.    '''
  13.     # Set LiveElement properties, these two are required:    
  14.     jsClass = u'MyModule.MyWidget'  # Define jsClass, the name of the javascript module we're going to use. This links the javascript with the python source
  15.     docFactory = loaders.xmlfile('tabletemplate.html') # Docfactory is what is returned to the client's browser
  16.  
  17.     def __init__(self, eventHandler, *a, **kw):
  18.         super(AlertElement, self).__init__(*a, **kw) # Required.
  19.         eventHandler.addSubscriber(self) # Subcribe this element to the eventHandler so it can be notified of new events.
  20.  
  21.     def fireEvent(self, eventID):
  22.         '''
  23.        fireEvent is called by the event handler when a new event is created by an EventCreator object. See EventHandler class.
  24.        '''
  25.         self.callRemote('addRow', unicode(eventID))
  26.  
  27.    
  28. class AlertPage(athena.LivePage):
  29.     '''
  30.    This is the /alerts page. Note that it is an athena LivePage.
  31.    '''
  32.    
  33.     # LivePage property, required. This is what gets returned to the client's browser,
  34.     # notice that alertElement gets included on this page.
  35.     docFactory = loaders.stan(T.html[
  36.         T.head(render=T.directive('liveglue')), #Required.
  37.         T.body(render=T.directive('alertElement'))]) #An Alert Element(table thing) gets included on the /alerts page.
  38.  
  39.     def __init__(self, eventHandler, *a, **kw):
  40.         super(AlertPage, self).__init__(*a,**kw) # Required
  41.         self.eventHandler = eventHandler # Save the event handler we're using so we can subscribe the alert element to it.
  42.         # Required, just tells Nevow where to look for our javascript module source.
  43.         here = filepath.FilePath(__file__).parent().child('mymodule.js')
  44.         self.jsModules.mapping.update({ # Still part of that.
  45.             'MyModule': here.path})
  46.    
  47.     # Tells Nevow how to render the 'alertElement' that we specified in docFactory,
  48.     # ie, just create a new AlertElement object, set its parent and return it.
  49.     def render_alertElement(self, ctx, data):
  50.         f = AlertElement(self.eventHandler)
  51.         f.setFragmentParent(self)
  52.         return ctx.tag[f]
  53.    
  54. class CreateEvent(rend.Page):
  55.     '''
  56.    This class creates an event and notifies the event handler when the page /event/<event ID> is visited..
  57.    '''
  58.     # As always, required. This is what is sent to the client's web browser.
  59.     docFactory = loaders.stan (
  60.         T.html [ T.head [T.title["Event Created"]],
  61.                  T.body [ render_body ]
  62.         ])
  63.    
  64.     def __init__(self,eventHandler):
  65.         self.eventHandler = eventHandler # Store the event handler that's being used.
  66.  
  67.     def render_body ( self, ctx, data ):
  68.         request = inevow.IRequest ( ctx )
  69.         eventID = request.prepath [ 1 ] # Get the event ID (ie, the part after /event/)
  70.         self.eventHandler.fireEvent(eventID) # Tell the eventHandler about it.
  71.         return "Event Created with ID: %s" % (eventID,)
  72.  
  73.    
  74. class CreateEventPage(rend.Page):
  75.     '''
  76.    This is /event.
  77.    '''
  78.  
  79.     #Nevow property, this gets sent to the client's web browser.
  80.     docFactory = loaders.stan(T.html[
  81.         T.head(title = "Create an Event"),
  82.         T.body[ T.h2["Create an Event by visiting /event/<ID of event to be created>"]]
  83.         ])
  84.  
  85.     def __init__(self, eventHandler):
  86.         self.eventHandler = eventHandler
  87.  
  88.     # "locateChild" is a catch-all that will handle any child page of /event/ , e.g. /event/lolpage, /event/foo
  89.     def locateChild ( self, ctx, segments ):
  90.         return ( CreateEvent(self.eventHandler), () ) # Create an event with the name of the child.
  91.  
  92.  
  93. class RootPage(rend.Page):    
  94.     '''
  95.    This is the root page. What more explaining do you need?
  96.    '''
  97.     # Nevow properties:
  98.     addSlash = True # Add a slash after the url.
  99.     # Every page or element has a docFactory. This is what gets sent to the client's web browser.
  100.     # It might look funny, but if you look closely you will see the way it is structured
  101.     # is very much like html, ie <html><head><title>Root Page</title></head><body>...</body>.
  102.     # And that is exactly what loaders.stan generates.
  103.     docFactory = loaders.stan(T.html[
  104.         T.head[T.title["Root Page"]],
  105.         T.body[ T.h1["Index"],
  106.             T.h2 [T.a ( href = 'alerts' ) [ "Alerts Page" ]],          
  107.             T.h2 [T.a ( href = 'event' ) [ "Generate an event"]]
  108.         ]])
  109.  
  110.     def __init__ ( self, *args, **kwargs ):
  111.         rend.Page.__init__ ( self, *args, **kwargs )
  112.         self.eventHandler = EventHandler() # Create the event handler we're going to use.
  113.        
  114.     # This is what is returned when you try to access /alerts
  115.     def child_alerts ( self, ctx ):
  116.         return AlertPage(self.eventHandler)
  117.     # This is what is returned when you try to access /event
  118.     def child_event ( self, ctx ):
  119.         return CreateEventPage(self.eventHandler)
  120.        
  121.  
  122. class EventHandler():
  123.     '''
  124.    The event handler class holds a list of subscribers, and notifies all of them if fireEvent is called.
  125.    This class should be straight forward.
  126.    '''
  127.     def __init__(self):
  128.         self.subscriberList = []
  129.  
  130.     def addSubscriber(self, subscriber):
  131.         self.subscriberList.append(subscriber)
  132.  
  133.     def fireEvent(self, eventID):
  134.         for subscriber in self.subscriberList:
  135.             subscriber.fireEvent(eventID)
  136.  
  137.  
  138. # Mandatory boilerplate stuff to start up the server. Should be self explanatory.
  139. site = appserver.NevowSite(RootPage())
  140. application = service.Application("Copied from Athena Demo")
  141. webService = internet.TCPServer(8080, site)
  142. webService.setServiceParent(application)

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me