Random ...
January 2018
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Tags ...

python+zope++:: Adding objects with xmlrpc
Posted at 06.Dec,2005 15:31  Comments 1 / Trackbacks 0 / Like this post!
Technorati tag(s):

One of the many ways to add objects to Zope is via xmlrpc. This is scriptable, and fits well in a crontab.

For point and click, there's ftp and webdav, bt that's for another day.

I often found that I need to add objects from non-Zope land to Zope. It maybe normal files, or images, or some other things.

Recently, I needed to get files via ftp and load it into zope weekly. Here's the python script to get files from ftp servers.[1]

Once the files are downloaded, I need to add it into Zope via xmlrpc. For this, we need the patched xmlrpclib (with BasicAuth) so that we can be authenticated.

Here's the python script to add files to Zope:

  import xmlrpclib

  s = xmlrpclib.Server('http://some/url',xmlrpclib.BasicAuthTransport('usr','pwd'))
  for file in filelist:
    s.addMyFile(file, open(file,'r').read())

And in Zope, create a Script (Python) with id addMyFile with the following:

  ob = context.folder #older where we want to add the files
  ob.manage_addDTMLMethod(id=id, file=file)

Where can we get the methods for Folder? There are many ways to get it:

This shows how easy it is to script Zope. We can do all the things that we can do via ZMI.

..[1]This is posted for peer review. I hope to gain enlightenment from those who knows better than me; particularly is it pythonic? what's the difference between sys.exit(1) and sys.exit(2), and the use of getopt:

  import ftplib, sys, getopt, os

  def usage():
    print """Syntax to run:
    %s -h -u -p -d
       -h host
       -u username
       -p password
       -d target dir to store files
    """ % sys.argv[0]

  def processOpts(optlist):
    for opt, arg in optlist:
        if opt == '-h':
        if opt == '-u':
            user = arg
        if opt == '-p':
            passwd = arg
        if opt == '-d':
            targetdir = arg
    print 'host %s user %s pass %s dir %s' % (host, user, passwd, targetdir)
    return (host, user, passwd, targetdir)

  def setupConn(host, user, passwd, targetdir):
    """setup connection with ftp server, change local dir"""
    #change dir
    #connect to host
    h.login(user, passwd)
    return h

  def getSortedFileList(h):
    """get file listing from ftpserver and sort.  can be done in main"""
    return filelist[-7:]

  def getTextFiles(h, file, targetdir):
     """get text file"""
     outfile = open(targetdir+'/'+file,'a')
     #write to targetdir/filename
     h.retrlines('RETR' + ' '+ file,lambda s, w=outfile.write: w(s+"\n")) 

  def getBinaryFiles(h, file, targetdir):
    """get binary file"""
    outfile = open(targetdir+'/'+file,'ab')
    h.retrbinary('RETR' + file, outfile.write)

  def uploadTextFiles(h, file):
    """upload text files"""
    h.storelines("STOR" + file, open(file))

  def uploadBinaryFiles(h, file):
    """upload binary file"""
    h.storebinary("STOR" + file, open(file,'rb'), 1024)

  if __name__ == "__main__":
        optlist, args = getopt.getopt(sys.argv[1:], 'h:u:p:d:')
        #print optlist, type(len(optlist)), args, sys.argv, len(sys.argv)
        if len(optlist) > 3:
            host, user, passwd, targetdir = processOpts(optlist)
            h = setupConn(host, user, passwd, targetdir)
            filelist = getSortedFileList(h)
            for file in filelist:
                wantedfiles = getTextFiles(h, file, targetdir)
    except getopt.GetoptError:

.. technorati python | technorati zope

Bookmark and Share

Is this entry helpful? Comments/Donate/Click some google ads.  
Trackback is http://myzope.kedai.com.my/blogs/kedai/57/tbping 


I write tutorial.
Look it.

It`s very useful for russian users :)
And also usefol for LJ. :)

Guest  at 23.Dec,2005 04:38
Post a comment