...or as a reference into deeply categorized resources. In this approach, we take a path like this...
/documents/news/2005/article.html
...and we consider documents
,
news
,
and
2005
as directories, and article.html
as a
file-like resource. If we ask for the following path...
/documents/news/2005
...we may decide to provide a listing of files within that directory, or we may decide to refuse such a request. Indeed some kinds of applications insist that such a listing may only be produced with the following path instead:
/documents/news/2005/
Applications of this kind are quite common since the publishing of files on a Web server often just involves exposing parts of a real filesystem to requests through the server.
There are a number of different ways that paths can be interpreted and handled in WebStack applications, including...
We might decide to represent components in these kinds of paths using different resource classes; for example:
documents
news
2005
article.html
another.html
2004
document.html
Consider the above hierarchy;
we would implement such a hierarchy with a
resource object mapped to documents
,
and that resource object
would contain a mapping of years to other resources. Eventually, at the
bottom of the hierarchy, individual resources would represent articles
and be
mapped to names such as article.html
.
WebStack provides the MapResource
class (in the WebStack.Resources.ResourceMap
module) for convenient mapping of path
components to resource objects. See the "ResourceMap - Simple Mappings from Names to Resources" document for a more detailed description of the
MapResource
class.
This class can be used in adapter code to initialise an application as follows:
from WebStack.Resources.ResourceMap import MapResource
from MyApplication import FileResource # import some resource class
article_resource = FileResource(...) # make a resource representing the article
document_resource = FileResource(...) # make a resource representing the document
year_2004_resource = MapResource({"document.html" : document_resource})
year_2005_resource = MapResource({"article.html" : article_resource})
news_resource = MapResource({"2005" : year_2005_resource, "2004" : year_2004_resource})
documents_resource = MapResource({"news" : news_resource})
top_resource = MapResource({"documents" : documents_resource})
Of course, predefining resource objects is not the only way to support such hierarchies. We could inspect paths and act dynamically on the supplied information, either choosing to create resources or choosing to handle such paths in the same resource.