PackagePath.py :  » Web-Frameworks » Aquarium » aquarium-2.3 » aquarium » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Aquarium 
Aquarium » aquarium 2.3 » aquarium » PackagePath.py
'''Document the package search path.

Aquarium uses a rarely used feature of Python packages that allows multiple
physical directories to map to a single package directory.  In this way,
Aquarium can have its own ``screen`` directory (for instance), and the app can
have its own ``screen`` directory.  Both Aquarium and the app form directory
hierarchies that are separate, but overlaid upon each other.  A path is used to
search the corresponding directory in each hiearchy for a given module.  For
instance, the module reference ``aquarium.navigation.TopNav`` might result in a
search for::

        $PYTHON_LIBS/aquarium/navigation/TopNav.py
        $APP_LIBS/site-packages/navigation/TopNav.py
        $CHEETAH_CACHE/navigation/TopNav.py

The package path must be set once, very early in the life of the application.
Specifically, it must be set before importing, instantiating, and executing
Aquarium or even AquariumProperties (the package path is needed to *find*
AquariumProperties!).  The package path is a Python global.  Because CGI
executes a new copy of Python for every page hit, in CGI, you must set the
package path at the beginning of every page hit.  For other environments, you
normally set the package path at the same time you are initializing other parts
of your application.  The package path is determined by one of the following:

* If ``__main__.packagePath`` is set to a tuple of paths, then it is
  used.  It is common to set ``__main__.packagePath`` in the ``index.py`` of a
  CGI::

    # In Python.
    import __main__
    __main__.packagePath = ("/.../site-packages",)

* Otherwise, if the environmental variable ``AQUARIUM_PACKAGE_PATH`` is set to
  a colon separated set of paths, then it is used::

    # In Bash.
    export AQUARIUM_PACKAGE_PATH=/.../site-packages

* Otherwise, if there is a directory named "site-packages" in the current
  directory, ``("site-packages",)`` is used.

* Otherwise, an empty tuple is used.  Only Aquarium modules will be accessible.
  This is helpful when using the Python shell to surf documentation.

Unfortunately, all this magic comes at a modest price.  Whenever you create a
new directory that Aquarium doesn't know about (e.g. ``screen/foobar``), you
must create an ``__init__.py`` in that directory that looks like this::

    """Setup the package search path."""
    packageType = "screen/foobar" # <- You must update this part.

    from __main__ import packagePath
    from os import path
    __path__ = map(lambda (x): path.join(x, packageType), packagePath) + \\
        __path__

Concerning the Python shell, as long as you can reach the aquarium package
(all the various ways to play with ``PYTHONPATH`` are out of scope for this
document), you can use the Python shell to read all of the Aquarium
documentation.  To read the documentation for your own application's Aquarium
classes, set the ``AQUARIUM_PACKAGE_PATH`` before starting the shell::

    $ env AQUARIUM_PACKAGE_PATH=demo/seamstress_exchange/site-packages \\
      python
    >>> import aquarium.screen.MessageList
    >>> help(aquarium.screen.MessageList)

Unfortunately, this won't work for templates unless you precompile them and put
the directory they are compiled into in your ``AQUARIUM_PACKAGE_PATH``.  You'll
need to precompile the base classes too::

    $ (cd aquarium;
       cheetah-compile --idir . \\
                       --odir /var/cache/cheetah/seamstress_exchange \\
        `find . -name '*.tmpl'`)
    $ (cd demo/seamstress_exchange/site-packages
       cheetah-compile --idir . \\
                       --odir /var/cache/cheetah/seamstress_exchange \\
        `find . -name '*.tmpl'`)
    $ env AQUARIUM_PACKAGE_PATH=demo/seamstress_exchange/site-packages:/var/cache/cheetah/seamstress_exchange python
    >>> import aquarium.screen.MessageListView
    >>> help(aquarium.screen.MessageListView)

'''

__docformat__ = "restructuredtext"

# Created: Mon Apr  5 14:46:48 PDT 2004
# Author: Shannon -jj Behrens
# Email: jjinux@users.sourceforge.net
#
# Copyright (c) Shannon -jj Behrens.  All rights reserved.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.