Package gavo :: Package web :: Module customrender
[frames] | no frames]

Source Code for Module gavo.web.customrender

 1  """ 
 2  User-defined renderers. 
 3  """ 
 4   
 5  #c Copyright 2008-2019, the GAVO project 
 6  #c 
 7  #c This program is free software, covered by the GNU GPL.  See the 
 8  #c COPYING file in the source distribution. 
 9   
10   
11  import imp 
12   
13  from nevow import url 
14   
15  from gavo import svcs 
16  from gavo.web import grend 
17 18 19 -class CustomRenderer(grend.ServiceBasedPage):
20 """A renderer defined in a python module. 21 22 To define a custom renderer write a python module and define a 23 class MainPage inheriting from gavo.web.ServiceBasedPage. 24 25 This class basically is a nevow resource, i.e., you can define 26 docFactory, locateChild, renderHTTP, and so on. 27 28 To use it, you have to define a service with the resdir-relative path 29 to the module in the customPage attribute and probably a nullCore. You 30 also have to allow the custom renderer (but you may have other renderers, 31 e.g., static). 32 33 If the custom page is for display in web browsers, define a 34 class method isBrowseable(cls, service) returning true. This is 35 for the generation of links like "use this service from your browser" 36 only; it does not change the service's behaviour with your renderer. 37 38 There should really be a bit more docs on this, but alas, there's 39 none as yet. 40 """ 41 name = "custom" 42
43 - def __init__(self, ctx, service):
44 grend.ServiceBasedPage.__init__(self, ctx, service) 45 if not service.customPage: 46 raise svcs.UnknownURI("No custom page defined for this service.") 47 pageClass, self.reloadInfo = service.customPageCode 48 self.realPage = pageClass(ctx, service)
49 50 @classmethod
51 - def isBrowseable(self, service):
52 return getattr(service, "customPageCode", None 53 ) and service.customPageCode[0].isBrowseable(service)
54
55 - def _reload(self, ctx):
56 mod = imp.load_module(*self.reloadInfo) 57 pageClass = mod.MainPage 58 self.service.customPageCode = (pageClass, self.reloadInfo) 59 return url.here.curdir()
60
61 - def renderHTTP(self, ctx):
62 return self.realPage.renderHTTP(ctx)
63
64 - def locateChild(self, ctx, segments):
65 return self.realPage.locateChild(ctx, segments)
66