[Zope] objectTree

Oleg Broytmann phd2@email.com
Mon, 8 Nov 1999 20:19:27 +0300 (MSK)


Hi!

   While hacking load_site.py for my needs I transformed it to a program to
traverse object tree. Here is the version that just prints the tree on
the screen:

----------
import sys, getopt, os, string


defIndent = 3 # I love 3 spaces indents


def main():
    user, password = 'superuser', '123'
    opts, args = getopt.getopt(sys.argv[1:], 'p:u:i:')

    for o, v in opts:
        if o == '-p':
            d, f = os.path.split(v)
            if f == 'ZPublisher': sys.path.insert(0, d)
            else: sys.path.insert(0, v)

        elif o == '-u':
            v = string.split(v, ':')
            user, password = v[0], string.join(v[1:], ':')

        elif o == '-i':
            global defIndent
            defIndent = string.atoi(v)

    if not args:
        print "Usage: %s [-p pathToZPublisher] [-u user:passwd] [-i indent] url" % sys.argv[0]
        sys.exit(1)

    url=args[0]

    import ZPublisher.Client
    global ServerError
    ServerError=ZPublisher.Client.ServerError
    object=ZPublisher.Client.Object(url, username=user, password=password)

    print url
    showTree(object, 0)


def call(f, *args, **kw):
    # Call a function ignoring redirect bci errors.
    try:
        return apply(f, args, kw)

    except ServerError, v:
        if str(v)[:1] != '3':
            raise sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]


def showTree(object, indent):
   try:
      for id in eval(call(object.objectIds)[1]):
         _showTree(object, id, indent+defIndent)
   except:
      return


def _showTree(object, id, indent):
   print "%s%s" % (' ' * indent, id)
   object = object.__class__(object.url+'/'+id,
      username=object.username,
      password=object.password)
   showTree(object, indent)


if __name__=='__main__':
   main()
----------

Oleg.
---- 
   Oleg Broytmann     National Research Surgery Centre     phd2@email.com
           Programmers don't die, they just GOSUB without RETURN.