[Zope] Re: including arbitrary files in zope products

Josef Meile jmeile at hotmail.com
Fri Sep 3 04:32:59 EDT 2004


Ops, I realiced that the function called is missing. You have
to call it on the initialize method like this:

_createFileInFolder(appContext,absolutePath,'yourFile.jar',
                     '/your/zope/path',title='utils')


> Josef Meile wrote:
> 
>> 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 at yahoogroups.com, Raja Subramanian <rsubr at 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 at 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)


More information about the Zope mailing list