[Zope] [ANN] zshell: The Zope Shell

Jerome Alet alet@unice.fr
Fri, 4 May 2001 11:55:32 +0200 (MET DST)


On Fri, 4 May 2001, Jose Soares wrote:

> Where on can find Zope Shell version 0.001 ?
> at http://www.zope.org/Members/sf/zopeshell
> there's only...

Sorry, it's not the same product.

zshell 0.001 is just the script below, but expect a much much more
powerful version in minutes.

I'll announce it there.

bye,

Jerome

> >   ------------------------------------------------------------------------
> > ## Script (Python) "zshell"
> > ##bind container=container
> > ##bind context=context
> > ##bind namespace=
> > ##bind script=script
> > ##bind subpath=traverse_subpath
> > ##parameters=
> > ##title=The Zope Shell
> > ##
> > # zshell - (c) 2001 Jerome Alet
> > #
> > # You're welcome to redistribute this software under the
> > # terms of the GNU General Public Licence version 2.0
> > # or, at your option, any higher version.
> > #
> > # You can read the complete GNU GPL in the file COPYING
> > # which should come along with this software, or visit
> > # the Free Software Foundation's WEB site http://www.fsf.org
> > #
> > # author: Jerome Alet - <alet@unice.fr>
> > #
> > #
> > # This is the Zope shell, an attempt at making the most common
> > # Unix shell commands available from within Zope.
> > #
> > # As of May 3rd 2001, version 0.001, the following commands
> > # are recognized:
> > #
> > #       cd, rm, mv, cp, ls
> > #
> > # * cd accepts a single path as an argument
> > # * rm accepts many path arguments
> > # * mv and cp last argument is the destination folder, every previous
> > #   argument is an object id in the current folder
> > # * ls accepts many meta types as its arguments
> > #
> > # The future:
> > #
> > # in the future I'd be interested in adding more functionnalities
> > # as well as making it a complete Zope product, splitting it
> > # in one method per command, and making the accepted syntax much more
> > # match Unix shell's one.
> > # adding support for a find command is also high on my priority list
> > #
> > # Feel free to send me any feedback about this software at: alet@unice.fr
> > #
> >
> > import string
> >
> > def toObject(curdir, path) :
> >         import string
> >         path = string.strip(path)
> >         if path[0] == '/' :
> >                 path = path[1:]
> >                 while hasattr(curdir, 'aq_parent') and (curdir.aq_parent is not None):
> >                         curdir = curdir.aq_parent
> >
> >         components = filter(None, string.split(path, '/'))
> >         for component in components :
> >                 if component == '.' :
> >                         continue
> >                 elif component == '..' :
> >                         if hasattr(curdir, 'aq_parent') and (curdir.aq_parent is not None):
> >                                 curdir = curdir.aq_parent
> >                 elif hasattr(curdir, component) :
> >                         curdir = curdir[component]
> >                 else :
> >                         return None     # Error
> >         return curdir
> >
> > print context.standard_html_header.document_src()
> >
> > if context.REQUEST.has_key("commands") :
> >         commands = filter(None, context.REQUEST["commands"])
> > else :
> >         commands = []
> >
> > print '<p><form action="zshell" method="POST">'
> > print 'Enter your commands below:<br>'
> > print '<textarea rows="10" cols="50" wrap="physical" name="commands:lines">%s</textarea>' % string.join(commands, '\n')
> > print '<br><input type="submit" name="run" value="Run !" />'
> > print '</form></p>'
> >
> > if commands :
> >         print '<hr noshade="noshade" width="33%" /><p>Results:'
> >         current = context
> >         for command in commands :
> >                 cmd = command[:2]
> >                 print '<br><b>%s:</b> ' % cmd
> >                 if cmd == 'cd' :
> >                         path = command[3:]
> >                         newdir = toObject(current, path)
> >                         if (newdir is not None) and hasattr(newdir, 'meta_type') and (newdir.meta_type == 'Folder') :
> >                                 current = newdir
> >                                 print 'New folder is <em><b>/%s</b></em>' % newdir.absolute_url(relative=1)
> >                         else :
> >                                 print 'Incorrect path <em><b>%s</b></em>' % path
> >                                 print '[%s]' % repr(newdir)
> >                 elif (cmd == 'mv') or (cmd == 'cp') :
> >                         args = string.split(command[3:])
> >                         if len(args) < 2 :
> >                                 print 'Incorrect number of arguments'
> >                         else :
> >                                 dst = args[-1]
> >                                 srcs = args[:-1]
> >                                 objids = []
> >                                 for src in srcs :
> >                                         if '/' in src :
> >                                                 print 'Paths in source objects are not allowed at this time: <em><b>%s</b></em>' % src
> >                                         else :
> >                                                 objids.append(src)
> >                                 dsto = toObject(current, dst)
> >                                 if dsto is None :
> >                                         print 'Incorrect destination argument <em><b>%s</b></em>' % dst
> >                                         continue
> >                                 if dsto.meta_type != 'Folder' :
> >                                         print 'Destination <em><b>%s</b></em> is not a folderish object' % dst
> >                                         continue
> >                                 if cmd == 'cp' :
> >                                         clipboard = current.manage_copyObjects(ids = objids)
> >                                         action = 'copied'
> >                                 else :
> >                                         clipboard = current.manage_cutObjects(ids = objids)
> >                                         action = 'moved'
> >                                 dsto.manage_pasteObjects(cb_copy_data = clipboard)
> >                                 for oid in objids :
> >                                         print '<br>%s %s' % (oid, action)
> >                 elif cmd == 'rm' :
> >                         objpaths = filter(None, string.split(string.strip(command[3:])))
> >                         for objpath in objpaths :
> >                                 object = toObject(current, objpath)
> >                                 if object is None :
> >                                         print 'Incorrect path <em><b>%s</b></em>' % objpath
> >                                 else :
> >                                         if hasattr(object, 'aq_parent') :
> >                                                 object.aq_parent.manage_delObjects(ids = [object.getId()])
> >                                                 print "<br>/%s removed" % object.absolute_url(relative=1)
> >                 elif cmd == 'ls' :
> >                         metatypes = filter(None, string.split(string.strip(command[3:])))
> >                         if metatypes :
> >                                 objects = current.objectValues(metatypes)
> >                         else :
> >                                 objects = current.objectValues()
> >                         if objects :
> >                                 print '<table border="1"><tr><th>Id</th><th>Title</th><th>MetaType</th></tr>'
> >                                 for object in objects :
> >                                         title = object.title or '&nbsp;'
> >                                         print '<tr><td>%s</td><td>%s</td><td>%s</td></tr>' % (object.getId(), title, object.meta_type)
> >                                 print '</table>'
> >                 else :
> >                         print 'Unknown command <em><b>%s</b></em>' % command
> >         print '</p>'
> >
> > print context.standard_html_footer.document_src()
> > return printed
>