##parameters=command, path, Type=None
from Products.CMFCore.utils import getToolByName
from Products.CMFCore.permissions import View
mtool = getToolByName(context, 'portal_membership')
checkView = lambda o : mtool.checkPermission(View, o)
utool = getToolByName(context, 'portal_url')
portal = utool.getPortalObject()

def replaceXMLEntities(text) :
	for c, ent in (('<', '&lt;'), ('>', '&gt;'), ('&', '&amp;')) :
		text = text.replace(c, ent)
	return text

ob = portal
path = path.replace('\\', '/')
path = path.strip('/')

if path :
	for name in path.split('/') :
		ob = getattr(ob, name)

if command == 'ls' :
	objects = []
	if ob.isPrincipiaFolderish :
		if Type == 'Image' : filter = {'portal_type' : ['Plinn Folder', 'Portfolio', 'Photo']}
		else : filter = {}
		objects = ob.listNearestFolderContents(contentFilter = filter)
		objects = sequence.sort( objects, (('title_or_id', 'nocase', 'asc'),) )

	# xml printing
	print '<ls>'
	for o in objects :
		path = o.getPhysicalPath()
		encodedPath = ''
		partObject = portal
		for name in path[1:] :
			partObject = getattr(partObject, name)
			sep = checkView(partObject) and '/' or '\\'
			encodedPath += sep+name
		
		row = '<row path="%(path)s" folderish="%(folderish)s" icon="%(icon)s" link="%(link)s">%(title)s</row>' % {
			'path' : encodedPath,
			'folderish' : o.isPrincipiaFolderish,
			'icon' : o.getIcon(),
			'title' : replaceXMLEntities(o.title_or_id()),
			'link' : o.absolute_url()
			}
		print row
	print '</ls>'

elif command == 'info':
	#linkFunction = (Type == 'Image') and (lambda o : o.absolute_url() + '/index_html') or (lambda o : o.absolute_url())
	linkFunction = lambda o : o.absolute_url()
	ti = ob.getTypeInfo()
	method_id = ti.queryMethodID('info')
	if not method_id or not hasattr(ob, method_id) :
		meth = lambda:'Not implemented'
	else :
		path = list(ob.getPhysicalPath())
		path.append(method_id)
		app = context.restrictedTraverse('/')
		meth = app.restrictedTraverse(tuple(path))
	
	lines = []
	pr = lines.append
	pr('<info>')
	# print info summary in a CDATA section
	pr('<![CDATA[')
	pr(meth())
	pr(']]>')
	# print the url link in a <link> tag
	pr('<link>')
	pr(linkFunction(ob))
	pr('</link>')
	pr('</info>')
	print ''.join(lines)


context.REQUEST.RESPONSE.setHeader('content-type', 'text/xml; charset=utf-8')
return printed