Python Manual User Manual

Page of 124
Plugin Aliasing
109
a servlet. In this later case, the name of the resource can remain the same, and no references to the re-
source need to be changed.
To facilitate use of an alias, an optional argument can be supplied to the "
plugin()
" member func-
tion defining the alternate extension the resource should be identified with. If you wanted all servlet
files accessible using a particular instance of a file server object to be accessed using a "
.html
" ex-
tension instead of the "
.py
" extension, the string "
.html
" would be supplied as the optional third
argument to the "
plugin()
" member function.
filesrvr.plugin(".py",netsvc.PythonPlugin(),".html")
filesrvr.hide(".py")
Note that if the "
hide()
" member function isn’t also called with the "
.py
" extension, the servlet file
would still be accessible with a "
.html
" extension, but a request against the "
.py
" extension would
yield the actual Python source code. This would not be an issue if the plugin had at the same time also
been registered for "
.py
" files, but without the alias.
If the "
.py
" file is hidden, if the servlet file was called "
login.py
", it would be accessable as
"
login.html
", but an attempt to use "
login.py
" would result in a HTTP error response indicat-
ing that the file could not be found. If the "
.py
" file isn’t hidden, but the plugin is registered twice,
once without an alias and once with the alias "
.html
", both "
login.py
" and "
login.html
"
would work.
If the servlet files are providing the roles of CGI scripts, it may be desirable for the files to use no ex-
tension at all. That is, the file should be accessed as "
login
" instead of "
login.py
". If this is the
case, rather than "
.html
", an empty string can be provided.
filesrvr.plugin(".py",netsvc.PythonPlugin(),"")
Be aware that the optional argument to "
plugin()
" defining the alias is actually treated as a filename
suffix and not strictly as an extension. What this means is that that argument need not start with "
.
",
but can be any arbitrary string in which the name of a resource ends. This means it is actually possible
to synthesis new resources as long as they derive from an actual file.
One use of this is a plugin which returns a servlet which generates a thumbnail version of an image.
For example, if an image file was originally called "
holiday.gif
", a request against "
holiday-
thumbnail.gif
" could me made to generate a thumbnail image on the fly.
def factory(session,file):
return ThumbnailServlet(session,file)
filesrvr.plugin(".gif",factory,"-thumbnail.gif")