Issue Details (XML | Word | Printable)

Key: MODPYTHON-73
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Unassigned
Reporter: Graham Dumpleton
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
mod_python

Using objects to create an explicit hierarchy.

Created: 13/Aug/05 10:43 AM   Updated: 05/Mar/06 02:20 PM
Return to search
Component/s: publisher
Affects Version/s: 3.2.7
Fix Version/s: 3.2.7

Time Tracking:
Not Specified

File Attachments:
  Size
Text File Licensed for inclusion in ASF works util.diff.txt 2005-08-13 10:44 AM Graham Dumpleton 2 kB

Resolution Date: 01/Sep/05 06:05 PM


 Description  « Hide
Cut and paste of idea presented on mailing list. See:

  http://www.mail-archive.com/python-dev@httpd.apache.org/msg00294.html

Have a strange patch here for consideration.

In CherryPy, one can manually construct the page hierarchy by writing:

  cpg.root.onepage = OnePage()
  cpg.root.otherpage = OtherPage()

  cpg.root.some = Page()
  cpg.root.some.page = Page()

The closest equivalent to this in mod_python is the publisher handler,
whereby a URL will be mapped to attributes and member functions of a
class. One generally though has to create an actual class to encapsulate
all the bits together.

One can to a degree with publisher create a mapping without creating a
proper class by using:

  class _Mapping:
    pass

  def _method1():
    return "_method1"

  def _method2():
    return "_method2"

  object = _Mapping()
  object.onepage = _method1
  object.otherpage = _method2

What isn't possible though without creating a real class is have a
normal function which is called when the dummy mapping object itself
is the target. Ie., following does not work:

  object.__call__ = _method1

This is because util.apply_fs_data() assumes that __call__() is always
an object method.

I know this is sort of an abuse of __call__(), but it does actually
work in Python itself, just not in mod_python when URLs are mapped to
object.

>>> class A:
... pass
...
>>> def _method():
... return "method"
...
>>> a=A()
>>> a.__call__ = _method
>>>
>>> a()
'method'

Anyway, I have attached a patch which would allow this sort of thing to
actually work within mod_python.

I feel it could be a useful way of quickly constructing special object
hierarchies from other functions, objects and attributes without having
to actually create real classes.

For example:

def _method():
  return "method"

class _Mapping:
  pass

_subdir1 = _Mapping()
_subdir1.__call__ = _method # .../root/subdir1
_subdir1.page1 = _method # .../root/subdir1/page1
_subdir1.page2 = _method # .../root/subdir1/page2

root = _Mapping()
root.__call__ = _method # .../root
root.page1 = _method # .../root/page1
root.subdir1 = _subdir1


 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Repository Revision Date User Message
ASF #265674 Thu Sep 01 08:54:47 UTC 2005 nlehuen Graham's patch for MODPYTHON-73
Files Changed
MODIFY /httpd/mod_python/trunk/lib/python/mod_python/util.py

Repository Revision Date User Message
ASF #265677 Thu Sep 01 09:05:17 UTC 2005 nlehuen Added unit tests for MODPYTHON-73
Files Changed
MODIFY /httpd/mod_python/trunk/test/htdocs/tests.py
MODIFY /httpd/mod_python/trunk/test/test.py
MODIFY /httpd/mod_python/trunk/src/include/mpversion.h

Repository Revision Date User Message
ASF #265736 Thu Sep 01 14:05:43 UTC 2005 nlehuen Added a new unit test to match Graham's test in MODPYTHON-73.
Files Changed
MODIFY /httpd/mod_python/trunk/test/htdocs/tests.py
MODIFY /httpd/mod_python/trunk/test/test.py