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)

Or, if we just want the location of the class:
>>> getsourcefile(Decimal)

or, we can get the location from an instance:

>>> d = Decimal('0.12')
>>> getsourcefile(d.normalize)

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


-Jim Washington

Zope3-users mailing list

Reply via email to