[Welcome] [TitleIndex] [WordIndex

Extended Directory

Thanks to the flexible structure of Quixote 2; easily, we are able to make our own Directory.

Let me present ExtDirectory that will bring you more flexibility.

In few words:

How it works

ExtDirectory search (from left to right) within the url until he find a python object/method. Once found, he will assign all remaining elements of the url (components and paramters) to the python object/method.

That means that you can add paramters to you standard Python methods or (for objects) to your _q_lookup method.

Thanks to Python, we can imagine to have optional components. For that look at the example here under : TestDir._q_lookup() *

The code

Requirements: Quixote-2.0

   1 from quixote.directory import Directory
   2 from quixote.errors import TraversalError
   3 
   4 class ExtDirectory(Directory):
   5     def _q_traverse(self, path):
   6         assert len(path) > 0
   7         component = path[0]
   8         path = path[1:]
   9         name = self._q_translate(component)
  10         if name is not None:
  11             obj = getattr(self, name)
  12             if not hasattr(obj,'_q_exports'):
  13                 if path: return obj(*path)
  14                 else:    return obj()
  15             else:
  16                 return obj._q_traverse(path)
  17         else:
  18             obj = self._q_lookup(component,*path)
  19             if obj: 
  20                 return obj
  21         if obj is None:
  22             raise TraversalError(private_msg=('directory %r has no component '
  23                                               '%r' % (self, component)))
  24 

Some examples

   1 import quixote
   2 
   3 from quixote.publish import Publisher
   4 from quixote.server.simple_server import run
   5 
   6 from quixote.directory import Directory
   7 from quixote.errors import TraversalError
   8 
   9 """
  10 I've try it the following url
  11 http://localhost:8080/other/
  12 http://localhost:8080/other/hello
  13 http://localhost:8080/other/hello?msg=hello
  14 http://localhost:8080/other/test2/
  15 http://localhost:8080/other/test2/1/2
  16 http://localhost:8080/other/test2/1/2/
  17 http://localhost:8080/other/test2/1/2/3
  18 http://localhost:8080/other/test2/test
  19 http://localhost:8080/other/test2/test/1
  20 http://localhost:8080/other/test2/test/1?msg=hello
  21 http://localhost:8080/other/test2/test?msg=hello
  22 
  23 all works and provide expected result
  24 
  25 """
  26 
  27 
  28 class ExtDirectory(Directory):
  29     def _q_traverse(self, path):
  30         assert len(path) > 0
  31         component = path[0]
  32         path = path[1:]
  33         name = self._q_translate(component)
  34         if name is not None:
  35             obj = getattr(self, name)
  36             if not hasattr(obj,'_q_exports'):
  37                 if path: return obj(*path)
  38                 else:    return obj()
  39             else:
  40                 return obj._q_traverse(path)
  41         else:
  42             obj = self._q_lookup(component,*path)
  43             if obj: 
  44                 return obj
  45         if obj is None:
  46             raise TraversalError(private_msg=('directory %r has no component '
  47                                               '%r' % (self, component)))
  48 
  49 
  50 class RootDir(ExtDirectory):
  51    pass
  52         
  53 class TestDir(ExtDirectory):
  54    _q_exports= ['test']
  55    def test(self,var1=None):
  56        msg=quixote.get_field('msg',None)
  57        return "test ok",var1,msg
  58    def _q_lookup(self,var1,var2=None,var3=None):
  59        return "_q_lookup",var1,var2,var3
  60 class OtherDir(ExtDirectory):
  61    _q_exports=['','hello']
  62    def _q_index(self):
  63        return "other index"
  64    def hello(self):
  65        msg=quixote.get_field('msg',None)
  66        return "other hello",msg
  67 
  68 def create_publisher():
  69    root=RootDir()
  70    root._q_exports.append('other')
  71    root.other=OtherDir()
  72    root.other._q_exports.append('test2')
  73    root.other.test2=TestDir()
  74    pub=Publisher(root)
  75    return pub
  76 
  77 if __name__=="__main__":
  78    run(create_publisher,host='localhost',port=8080)
  79 

Discussion

Feel free to post your adaptation/comments/...

Or to contact me at: vincent_delft (at) yahoo.com.

Thanks


2010-09-22 22:14