/[glsr]/trunk/glsr_framework/dispatcher.py
Gentoo

Contents of /trunk/glsr_framework/dispatcher.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 174 - (show annotations) (download) (as text)
Sun Oct 30 20:33:57 2005 UTC (8 years, 5 months ago) by hadfield
File MIME type: text/x-python
File size: 3822 byte(s)
modified the dispatcher to work with nested domains in htdocs/
1 # Copyright 2005 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 #
4
5 """A generic page dispatcher.
6
7 This module can be used as an extremely simple framework to help with the
8 development. Simply import this into your index.{cgi,py} and let it do
9 all the work.
10 """
11
12 __revision__ = "$Id$"
13 __author__ = "Scott Hadfield <hadfield@gentoo.org>"
14 __modulename__ = "dispatcher"
15
16 import os
17
18 from request import CgiHandler
19
20 class _PageDispatch:
21
22 def __init__(self, request, failover = ""):
23 """Initialize the class.
24
25 Sets up the cgi form and other common member variables.
26 """
27
28 self._request = request
29 self._form = self._request.request.forms
30
31 self._failover = failover
32 self._domain = self._get_domain()
33
34 self._action = self._form.getvalue("page", False)
35
36 def _get_domain(self):
37
38 basename = os.environ["SCRIPT_NAME"]
39 queries = os.environ["QUERY_STRING"]
40
41 domain = os.environ["REQUEST_URI"][len(basename) + 1:]
42 if len(queries):
43 domain = domain[:-len(queries) - 1]
44
45 # Strip of the .py
46 domain = domain[:-3]
47
48 return domain.replace("/", ".")
49
50 def dispatch(self, module_package):
51
52 package = __import__(module_package, globals(), locals(), [])
53 module_listing = package.__all__
54
55 if self._domain not in module_listing:
56 self._domain = self._failover
57
58 if self._domain.find(".") != -1:
59 dot_index = self._domain.rfind(".")
60 module_package += "." + self._domain[:dot_index]
61 self._domain = self._domain[dot_index + 1:]
62
63 module_object = __import__(module_package + "." + self._domain,
64 globals(), locals(),
65 [self._domain.capitalize()])
66
67 # FIXME: This is pretty ugly.
68 module_object = eval("module_object.%s(self._request)" %
69 self._domain.capitalize())
70
71
72 call_action = self._action
73 if (call_action and (not hasattr(module_object, call_action)
74 or not hasattr(getattr(module_object, call_action),
75 'exposed'))):
76 # TODO: Consider adding some kind of error message here.
77 call_action = False
78
79 if not call_action:
80 for method in dir(module_object):
81 if hasattr(getattr(module_object, method), 'default'):
82 call_action = method
83 break
84
85 if call_action:
86 getattr(module_object, call_action)()
87
88 self._request.response.add_header("Content-Type",
89 "text/html; charset=utf-8\n")
90 self._request.response.send()
91
92 class Dispatcher:
93
94 def __init__(self, module_package):
95 self._module_package = module_package
96 self._header = ""
97 self._footer = ""
98 self._failover = ""
99
100 def set_failover(self, failover):
101 self._failover = failover
102
103 def dispatch(self):
104
105 Dispatcher = _PageDispatch(CgiHandler(), self._failover)
106 Dispatcher.dispatch(self._module_package)
107
108 def default(method):
109 """The decorator function that defines default class method.
110
111 A default class method will be called if no other page is specified. A
112 default class method is automatically exposed.
113 """
114
115 method.default = True
116 method.exposed = True
117 return method
118
119 def exposed(method):
120 """The decorator function that defines 'exposed' class methods.
121
122 An exposed class method is a method in a domain module that can be
123 called directly by the browser.
124 """
125
126 method.exposed = True
127 return method

Properties

Name Value
svn:keywords Id

  ViewVC Help
Powered by ViewVC 1.1.20