Re: including arbitrary files in zope products
Hi Raja, some few month ago, I was trying to do the same: Store a java applet class on the file system and load it dynamically from each instance of my product, but I didn't succeeded. You can see my attempt on: http://mail.zope.org/pipermail/zope/2004-May/149813.html Anyway, I solved it storing the java applet on the Zope DB when initializing my product. The code is attached to the message. After adding the file to the Zope DB, then you can use its path on the <applet> tag and it should work. Regards, Josef --- In zope@yahoogroups.com, Raja Subramanian <rsubr@p...> wrote:
Hi,
I am facing a basic problem with Zope that I'm unable to solve.
I want to embed a java applet (jar) in my zope product. The jar file lives as "<Product Basedir>/www/foo.jar" on the filesystem. I want to make this available as "<Product Instance Dir>/foo.jar" inside zope.
To include ZPT files, I'm able to do -
from Products.PageTemplates.PageTemplateFile import PageTemplateFile ... class Foo(Folder): ... index_html = PageTemplateFile('zpt/index_html', globals())
and make zpt files available inside zope. How can I do the same for arbitrary files such as java archives and others?
I don't know how to use the "File" object for this purpose. Can someone please help?
Thanks in advance.
- Raja
_______________________________________________ Zope maillist - Zope@z... http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
import Globals, os from OFS.Image import manage_addFile from zLOG import LOG, INFO, BLATHER, \ PROBLEM, WARNING #Get's the physical location of your product PRODUCT_HOME=Globals.package_home(globals()) def initialize(context): #Do the normal initialization stuff here #... #... #... #Get's the ZMI Context appContext=context._ProductContext__app #On my case the java class was stored on #PRODUCT_HOME/java, here you have to change #it for your location absolutePath=os.path.join(PRODUCT_HOME,'java') """ ColorPicker.class is the name of the class (here you can use your jar file) /java is the path where you want to store it on the ZMI """ def _createFileInFolder(context, filePath, fileName, zopePath, title='', binary=0): """Creates a the folder <zopePath> and puts the file <filePath>/<fileName> on it """ pathLen=len(zopePath) #Remove leading slash if (zopePath[-1]=='/') and (pathLen>1): zopePath=zopePath[0:pathLen-1] if zopePath[0]!='/': #The product will only work with relative paths #if the instances can adquire them. _log('The path "%s" is relative.' %zopePath, severity=WARNING, detail='For better results use absolute paths.', product='createFileInFolder') zopePath='/'+zopePath pathParts=os.path.split(zopePath) basePath=pathParts[0] folder=pathParts[1] #This only happens when no slash is given at #the beginning of the path, so the root will #be assumed fileContainer=context.unrestrictedTraverse(basePath,None) if fileContainer==None: _log('The path "%s" doesn\'t exist on the Zope DB.' %basePath, severity=PROBLEM,detail='Create it first.', product='createFileInFolder') return fileFolder=fileContainer #If the given path wasn't the zope root if folder!='': _tryAdd(fileContainer,folder,manage_addFolder,'Utils') fileFolder=getattr(fileContainer,folder) absolutePath=os.path.join(filePath,fileName) if binary: data=file(absolutePath,'rb') else: data=file(absolutePath) _tryAdd(fileFolder,fileName,manage_addFile, title,file=data) def _tryAdd(container, id, addMethod, title='', file=None): """If an object doesn't exist on the Zope DB, then it will be added; otherwise, the method will return 0 """ try: if file!=None: #It is a file object addMethod(container,id,title=title, file=file) else: addMethod(container,id,title=title) except BadRequest: result=0 return 1 def _log(msg, severity=INFO, detail='', error=None, product='', exit=0): """Logs a message to the zope log""" if not NOISY_DEBUG and severity == BLATHER: return if type(msg) is UnicodeType: msg = msg.encode(sys.getdefaultencoding(), 'replace') if type(detail) is UnicodeType: detail = detail.encode(sys.getdefaultencoding(), 'replace') LOG(product, severity, msg, detail, error) if exit: Lifetime.shutdown(0)
Hi, Josef Meile wrote:
some few month ago, I was trying to do the same: Store a java applet class on the file system and load it dynamically from each instance of my product, but I didn't succeeded. You can see my attempt on:
http://mail.zope.org/pipermail/zope/2004-May/149813.html
Anyway, I solved it storing the java applet on the Zope DB when initializing my product. The code is attached to the message.
Many thanks for your help. It's good to know that I'm not alone with this problem ;-) I have the following in my product constructor and it seems to work fine. It pulls in the contents of applet.jar into Zope on product instantiation. # A small dance to import the java client into Zope product_home = Globals.package_home(globals()) fname = os.path.join(product_home, 'www', 'applet.jar') f = open(fname, 'rb') filecontents = f.read() f.close() self.manage_addFile('applet.jar', filecontents, 'Applet') I borrowed the "product_home = Globals..." line from your code. Thanks! But this is not optimal because if I change applet.jar (which happens a lot during development), it is not automatically reflected within Zope. Oh well! I could call the above code whenever I modify the applet and some how hook it into product refresh. Since I have only one jar file and only a couple of product instances, my problem is manageable. But in general, how does one go about embedding arbitrary files - like html/text/pdf/etc - within products and have them automatically refreshed on Zope init/restart/etc? Like everything else in zope, I was imagining that this would be an easy/simple thing to do. Is there some reason why the Zope Gods left out such a feature? - Raja PS: The Zope XML-RPC mechanism and java xmlrpc libs from apache have simpilified my application by an order of magitude. Kudos to the Zope developers.
Raja Subramanian wrote:
But in general, how does one go about embedding arbitrary files - like html/text/pdf/etc - within products and have them automatically refreshed on Zope init/restart/etc? Like everything else in zope, I was imagining that this would be an easy/simple thing to do.
If you are running your zope behind Apache, the easiest is to just have a filesystem based directory there, that you can upload your files to. You can then use The Apache proxy to serve that director from the filesystem. If you are using CMF/Plone I guess you can make a skin and put the Java file in there. I am not certain about that though. I also suspect that some of these products might be of help: http://sourceforge.net/projects/localfs http://zope.org/Members/shh/ExtFile regards Max M
participants (3)
-
Josef Meile -
Max M -
Raja Subramanian