From 8c5a90362070e17dbb38a5c4002cee54ace686e4 Mon Sep 17 00:00:00 2001 From: "M. Ian Graham" Date: Fri, 4 Apr 2008 21:35:50 +0000 Subject: [PATCH] First pass at default landing page for WebRequestDispatcher --- direct/src/http/LandingPage.py | 86 +++++++++ direct/src/http/LandingPageHTML.py | 291 +++++++++++++++++++++++++++++ direct/src/http/WebRequest.py | 30 ++- 3 files changed, 405 insertions(+), 2 deletions(-) create mode 100755 direct/src/http/LandingPage.py create mode 100755 direct/src/http/LandingPageHTML.py diff --git a/direct/src/http/LandingPage.py b/direct/src/http/LandingPage.py new file mode 100755 index 0000000000..68c9056bed --- /dev/null +++ b/direct/src/http/LandingPage.py @@ -0,0 +1,86 @@ +import LandingPageHTML + +class LandingPage: + def __init__(self): + self.headerTemplate = LandingPageHTML.header + self.footerTemplate = LandingPageHTML.footer + + self.title = LandingPageHTML.title + self.contactInfo = LandingPageHTML.contactInfo + + self.menu = {} + + def addTab(self, title, uri): + self.menu[title] = uri + + def getMenu(self, activeTab): + tabList = self.menu.keys() + if "Main" in tabList: + tabList.remove("Main") + + tabList.sort() + if "Main" in self.menu.keys(): + tabList.insert(0, "Main") + + s = "" + tabNum = 0 + + for tab in tabList: + if tabNum == 0: + if tab == activeTab: + s += "
  • %s
  • \n" % \ + (self.menu[tab], tab) + else: + s += "
  • %s
  • \n" % \ + (self.menu[tab], tab) + else: + if tab == activeTab: + s += "
  • %s
  • \n" % \ + (self.menu[tab], tab) + else: + s += "
  • %s
  • \n" % \ + (self.menu[tab], tab) + tabNum += 1 + + return s + + + def getHeader(self, activeTab = "Main"): + s = self.headerTemplate % {'titlestring' : self.title, + 'menustring' : self.getMenu(activeTab)} + return s + + + def getFooter(self): + return self.footerTemplate % {'contact' : self.contactInfo} + + def listHandlerPage(self, uriToHandler): + output = self.getHeader("Services") + + uriList = uriToHandler.keys() + uriList.sort() + + output += "\n\n\n" + output += "\n" + + rowNum = 0 + for uri in uriList: + rowNum += 1 + handlerFunc = str(uriToHandler[uri][0]).split(" ")[2] + + output += "\n" % \ + (LandingPageHTML.getRowClassString(rowNum), + uri, + uri, + handlerFunc) + #handlerFunc) + + output += "
    Services
    URIHandler
    %s%s
    \n" + + output = output + self.getFooter() + return output + + def main(self): + output = self.getHeader("Main") + "

    Welcome!

    \n" + self.getFooter() + return output + diff --git a/direct/src/http/LandingPageHTML.py b/direct/src/http/LandingPageHTML.py new file mode 100755 index 0000000000..8db29f203d --- /dev/null +++ b/direct/src/http/LandingPageHTML.py @@ -0,0 +1,291 @@ +# -- Text content for the landing page. You should change these for yours! -- + +title = "Landing Page" +contactInfo = "M. Ian Graham - ian.graham@dig.com - 818-623-3219" + + + + +# -- Begin fancy layout stuff, change at your own risk -- + +header = ''' + +%(titlestring)s + + + + +
    +
    +''' + +mainMenu = ''' +

    Whee!

    +''' + +footer = ''' +
    +
    + + + +\r\n''' + + +def getRowClassString(rowNum): + if rowNum % 2 == 0: + return "" + else: + return " class=\"odd\"" diff --git a/direct/src/http/WebRequest.py b/direct/src/http/WebRequest.py index de95753220..9f1370fdfd 100755 --- a/direct/src/http/WebRequest.py +++ b/direct/src/http/WebRequest.py @@ -3,6 +3,7 @@ from pandac.PandaModules import HttpRequest from direct.directnotify.DirectNotifyGlobal import directNotify from direct.task.TaskManagerGlobal import taskMgr from direct.task import Task +from LandingPage import LandingPage notify = directNotify.newCategory('WebRequestDispatcher') @@ -90,8 +91,8 @@ class WebRequestDispatcher(object): obj.__dict__ = self._shared_state return obj - def __init__(self): - pass + def __init__(self, wantLandingPage = True, landingPageTitle = None): + self.enableLandingPage(wantLandingPage, landingPageTitle) def listenOnPort(self,listenPort): """ @@ -199,3 +200,28 @@ class WebRequestDispatcher(object): def stopCheckingIncomingHTTP(self): taskMgr.remove('pollHTTPTask') + + # -- Landing page convenience functions -- + + def enableLandingPage(self, enable, title): + if enable: + if not self.__dict__.has_key("landingPage"): + self.landingPage = LandingPage() + if title is None: + title = self.__class__.__name__ + self.landingPage.title = title + self.registerGETHandler("/", self.landingPage.main, returnsResponse = True) + #self.registerGETHandler("/main", self.landingPage.main, returnsResponse = True) + self.registerGETHandler("/list", self._listHandlers, returnsResponse = True) + self.landingPage.addTab("Main", "/") + self.landingPage.addTab("Services", "/list") + else: + self.landingPage = None + self.unregisterGETHandler("/") + #self.unregisterGETHandler("/main") + self.unregisterGETHandler("/list") + + + def _listHandlers(self): + return self.landingPage.listHandlerPage(self.uriToHandler) +