Random ...
 
December 2017
S M T W T F S
          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 ...
Links



python+zope++:: Adding Zope objects with zeo
Posted at 13.Dec,2005 16:12  Comments 0 / Trackbacks 0 / Like this post!
Technorati tag(s):

One of the cool thing about zope is ZEO. We can scale sites by adding stock box with zope configured as zeo clients.

But zeo is not about scaling sites only. The other cool thing about zeo is that we can introspect any zope instance via the python interpreter. And we don't need another box for this setup.

Here's how:

  [kdie@tokey ~]$ ipython
  Python 2.3.5 (#1, May 24 2005, 15:52:33)
  Type "copyright", "credits" or "license" for more information.

  IPython 0.6.15 -- An enhanced Interactive Python.
  ?       -> Introduction to IPython's features.
  %magic  -> Information about IPython's 'magic' % functions.
  help    -> Python's own help system.
  object? -> Details about 'object'. ?object also works, ?? prints more.

  In [1]: import sys

  In [2]: sys.path.insert(1,'/home/kdie/zope28/lib/python')

  In [3]: from Zope2 import configure

  In [4]: configure('/home/kdie/zopehome28/etc/zope.conf')
  Out[4]: <Zope2.Startup.UnixZopeStarter instance at 0xb7c23dcc>

  In [5]: import Zope2

  In [6]: app = Zope2.app()

  In [7]: app.objectIds()
  Out[7]:
   ['acl_users',
   'Control_Panel',
   'temp_folder',
   'session_data_manager',
   'browser_id_manager',
   'error_log',
   'standard_html_header',
   'standard_template.pt',
   'standard_html_footer',
   'standard_error_message',
   'index_html',
   'virtual_hosting',
   'test',
   'js',
   'laman']

After any changes, we need to commit by doing:

  get_transaction().commit()

Recent Zope complains about the above:

  zeoadd.py:23: DeprecationWarning: This will be removed in ZODB 3.6:
   use transaction.get() instead of get_transaction().
   transaction.commit() is a shortcut spelling of   transaction.get().commit(),
   and transaction.abort() of transaction.get().abort().

But the shortcut mentioned resulted in a NameError.

And what better way to script zope than via zeo. Extending on the previous example, I now add files using zeo.[1]

The script will get the SOFTWARE_HOME and INSTANCE_HOME, directories where we store the files, and the target zope folder.

It will then process the options and connect to zope, and add the necessary files.

As usual, it is put here for peer review. Please feel free to comment and right what's wrong.

It seems that either my scripts are excellent, or it's too easy that it deserves no comments. :P

..[1]:

  def connZope():

    from Zope2 import configure
    configure(INSTANCE_HOME+'/etc/zope.conf')
    import Zope2
    app = Zope2.app()
    return app

  #add files
  def addFiles(filename, mypath, app):
    try:
        folder = app.unrestrictedTraverse(str(mypath))
    except AttributeError, e:
        print e, 'cannot get %s' % mypath
        sys.exit(1)
    if folder is not None:
        folder.manage_addDTMLMethod(id=str(filename.split('/')[-1:][0]), file=open(filename,'r').
read())
        get_transaction().commit()

  def authAsUser(username, app):
    UF = app.acl_users
    u = UF.getUser(username)
    from AccessControl.SecurityManagement import newSecurityUser
    newSecurityUser(None, u.__of__UF)
    return u

  def usage():
    print """
    usage:
    %s -s SOFTWARE_HOME -i INSTANCE_HOME -p zope object path -d target directory

    example:
    %s -s /home/zope28 -i /home/zopehome -p test/folder -d /tmp/files

    """ % (sys.argv[0], sys.argv[0])

  def processOpts(optlist):
    for opt, arg in optlist:
        if opt == '-p':
            mypath = arg
        elif opt == '-d':
            targetdir = arg
        elif opt == '-s':
            SOFTWARE_HOME = arg
        elif opt == '-i':
            INSTANCE_HOME = arg
    return (SOFTWARE_HOME, INSTANCE_HOME, mypath, targetdir)

  if __name__ == "__main__":
    import os, sys, getopt

    try:
        optlist, args = getopt.getopt(sys.argv[1:],'s: i: p: d:')
    except getopt.GetoptError, e:
        print e
        usage()
        sys.exit(1)
    if len(optlist) > 3:
        print optlist
        SOFTWARE_HOME, INSTANCE_HOME, mypath, targetdir = processOpts(optlist)
        sys.path.insert(1, SOFTWARE_HOME)
        sys.path.insert(1,INSTANCE_HOME)
        sys.path.insert(1, SOFTWARE_HOME + '/lib/python')
        app = connZope()
        filelist = os.listdir(targetdir)
        print filelist
        for fname in filelist:
            filename = targetdir +'/'+fname
            print filename
            addFiles(filename, mypath, app)
        app._p_jar.close()

        sys.exit(0)
    else:
        print optlist
        usage()
        sys.exit(1)

python | zope


Bookmark and Share

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

Comments
Post a comment