Re: [Zope3-Users] Find the zope3 instance home directory (or the /var directory)
On Nov 6, 2007, at 5:02 AM, Eric Bréhault wrote: Hello, WIth Zope2, when I need to create or read a file in the filesystem, I usually go in /myzopeinstance/var directory and to find it is pretty easy: import os os.environ['CLIENT_HOME'] But in Zope3, CLIENT_HOME or INSTANCE_HOME are no longer provided in os.environ 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 ? Yes. If you want to have a directory like this: - Create it (or arrange for it to be created lazily) - Configure it in zope.conf: directory /location/of/directory - To get this information from Python: import zope.app.appsetup.product var_dir = zope.app.appsetup.product['var']['directory'] As has been pointed out, there is no equivalent of an instance home in Zope 3, because we don't want to impose such a restrictive model, but you can easily provide such a model for yourself if you wish. Jim -- Jim Fulton Zope Corporation ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
Re: [Zope3-Users] Find the zope3 instance home directory (or the /var directory)
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 ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
Re: [Zope3-Users] Find the zope3 instance home directory (or the /var directory)
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 ? -- Anything that, in happening, causes itself to happen again, happens again. --Douglas Adams ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
[Zope3-Users] Find the zope3 instance home directory (or the /var directory)
Hello, WIth Zope2, when I need to create or read a file in the filesystem, I usually go in /myzopeinstance/var directory and to find it is pretty easy: import os os.environ['CLIENT_HOME'] But in Zope3, CLIENT_HOME or INSTANCE_HOME are no longer provided in os.environ 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 ? Eric BREHAULT ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users