[Zope3-Users] Find the zope3 instance home directory (or the /var directory)

Jim Washington jwashin at vt.edu
Wed Nov 7 07:58:07 EST 2007


Stefan H. Holek wrote:
> I don't think there is any way whatsoever in Zope 3. There is no
> "instance" home to begin with, and the os.pardir hacks don't work
> either because you can't really know where your package is installed.
>
> Stefan
>
>
> On 6. Nov 2007, at 11:02, Eric Br�hault wrote:
>
>> I had a look to different source code, and apparently the solution is
>> to use
>> the path of the current file to get the instance home, so we have
>> code like
>> this:
>> os.path.normpath(
>>             os.path.join(os.path.dirname(__file__),
>>                          os.pardir, os.pardir, os.pardir, os.pardir))
>>
>> Is there any easier (and cleaner) way to do it ?
>
I have found useful another trick related to __file__ .

If the file system location you are looking for is near code you are
using or can import, python's "inspect" may be your friend.

>>> import inspect

So, if your object is, for example, a view method and you need the
directory where another method is defined for your view, you can

>>> sourcefilepath = inspect.getsourcefile(self.otherMethod)

Here's another example for clarification.  It works, but is not
particularly useful:

Let's say we want to know the file system location where
Decimal.normalize is defined.

>>> from inspect import getsourcefile
>>> from decimal import Decimal

We can get the location directly:

>>> getsourcefile(Decimal.normalize)
'/usr/lib64/python2.5/decimal.py'

Or, if we just want the location of the class:
>>> getsourcefile(Decimal)
'/usr/lib64/python2.5/decimal.py'

or, we can get the location from an instance:

>>> d = Decimal('0.12')
>>> getsourcefile(d.normalize)
'/usr/lib64/python2.5/decimal.py'

>From there, standard os.path functions can get you locations relative to
the location given.

HTH,

-Jim Washington




More information about the Zope3-users mailing list