Author: | Dave Kuhlman |
---|---|
Address: | dkuhlman@rexx.com http://www.rexx.com/~dkuhlman |
Revision: | 1.0a |
Date: | August 14, 2005 |
Copyright: | (C) Copyright 2005 Nuxeo SARL (http://nuxeo.com). This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
Abstract
This document describes the CPSRemoteController product. This product enables any XML-RPC clients (which includes python scripts, Java programs, etc.) to remotely control a CPS site and its content.
Thanks to Nuxeo and the developers there for CPSRemoteController. This document would not be possible without that implementation. Significant portions of the documentation on individual methods was copied from the CPSRemoteController module. Thanks for those very helpful comments. Also, it is likely that any errors in this document have been introduced by me and should not be attributed to the implementors of CPSRemoteController.
CPSRemoteController provides a way to run Python scripts, outside of the CPS environment that manipulate a CPS portal, including the users, groups, documents, and other objects at that site.
Why you might want to use CPSRemoteController -- Some possible of benefits:
Extensions to CPSRemoteController -- In addition, you may be able to implement additional methods not currently supported by CPSRemoteController. We'll see how to do that in section Adding New Methods to CPSRemoteController.
By the time you read this, CPSRemoteController may already be included in the CPS distribution. If not, it is available through SVN at http://svn.nuxeo.org/trac/pub.
There is already some documentation on CPSRemoteController:
Brief comments on how to make calls into CPSRemoteController are in Products/CPSRemoteController/RemoteControllerTool.py.
Comments on each method that is exposed are the method's implementation. Although you can read the in-line documentation in the source code, it may make for more convenient reading if you generate HTML documentation for the methods. I used happydoc. Generate the documentation for CPSRemoteController using something like the following:
cd my_cps_site/Products/CPSRemoteController happydoc RemoteControllerTool.py
By default, happydoc places the generated files in a directory named doc. happydoc may already be installed on your machine. If not and if you are on a Debian GNU/Linux machine, you can install happydoc with something like the following:
apt-get install python-happydoc
For help on other platforms, check the happydoc Web site.
Calling a method on CPSRemoteController is fairly easy. Here is an example based on the use of the standard python module xmlrpclib that you can use as a template:
from xmlrpclib import ServerProxy proxy = ServerProxy('http://username:password@thrush:8085/mysite/portal_remote_controller') # 1 path_to_doc = 'workspaces/members/username/document-1' doc_def = { # 2 'content': 'Test #1', } comments = 'CPSRemoteController test #1\n' proxy.editDocument(path_to_doc, doc_def, comments) # 3
Explanation:
Note: Use of the ServerProxy class as described here is dependant on the specific implementation library used here, in particular xmlrpclib. There are other python XML-RPC implementations for Python that might be better suited than xmlrpclib, for example see Creating XML-RPC Servers and Clients with Twisted. One deficiency with the xmlrpclib implementation is not be able to send XML-RPC queries through a proxy.
So, here is a client built on Twisted. Note that this implementation requires a replacement to and extension of several classes in twisted.web.xmlrpc from TwistedWeb. The extension supports the use of user IDs and passwords. By the time you read this, that extension may already be in the distribution of TwistedWeb. Here are those replacement classes and a sample client that you can use as a template for your Twisted clients:
#!/usr/bin/env python import sys import base64, urlparse from twisted.web import xmlrpc from twisted.internet import reactor, defer class QueryProtocol(xmlrpc.QueryProtocol): def connectionMade(self): self.sendCommand('POST', self.factory.url) self.sendHeader('User-Agent', 'Twisted/XMLRPClib') self.sendHeader('Host', self.factory.host) if self.factory.authString is not None: cred = base64.encodestring(self.factory.authString) self.sendHeader('Authorization', 'Basic ' + cred[:-1]) self.sendHeader('Content-type', 'text/xml') self.sendHeader('Content-length', str(len(self.factory.payload))) self.endHeaders() self.transport.write(self.factory.payload) class QueryFactory(xmlrpc.QueryFactory): protocol = QueryProtocol def __init__(self, url, host, method, authString=None, *args): self.authString = authString xmlrpc.QueryFactory.__init__(self, url, host, method, *args) class Proxy: """A Proxy for making remote XML-RPC calls. Pass the URL of the remote XML-RPC server to the constructor. Use proxy.callRemote('foobar', *args) to call remote method 'foobar' with *args. """ def __init__(self, url): parts = urlparse.urlparse(url) self.url = urlparse.urlunparse(('', '')+parts[2:]) self.auth = None if self.url == "": self.url = "/" if ':' in parts[1]: if '@' in parts[1]: self.auth, address = parts[1].split('@') else: address = parts[1] self.authHost = None self.host, self.port = address.split(':') if self.auth is not None: self.authHost = '@'.join([self.auth, self.host]) self.port = int(self.port) else: self.host, self.port = parts[1], None self.secure = parts[0] == 'https' def callRemote(self, method, *args): factory = QueryFactory(self.url, self.host, method, self.auth, *args) if self.secure: from twisted.internet import ssl reactor.connectSSL(self.host, self.port or 443, factory, ssl.ClientContextFactory()) else: reactor.connectTCP(self.host, self.port or 80, factory) return factory.deferred class Test: def printValue(self, value): value.sort() print 'Items:' for item in value: print ' %s' % item def printError(self, error): print 'error', error def getData(self, user, password): url = 'http://%s:%s@thrush:8085/cps1/portal_remote_controller' % \ (user, password, ) self.proxy = Proxy(url) arg1 = 'workspaces/members/%s' % user d = self.proxy.callRemote('listContent', arg1) d.addCallbacks(self.printValue, self.printError) return d def test_listContent(): g = Test() user = 'user1' password = 'user1_password' d = g.getData(user, password) user = 'user2' password = 'user2_password' d = g.getData(user, password) reactor.callLater(1, reactor.stop) reactor.run() if __name__ == '__main__': test_listContent()
Notes:
More information on the Twisted programming paradigm and the use of deferred objects can be found at Twisted Documentation. For more on the use of XML-RPC with Twisted see Creating XML-RPC Servers and Clients with Twisted.
For documents, CPS takes the document title, performs a conversion on the title, then uses that converted string for the object ID. You must use the document ID, not the title, to perform operations on existing objects.
How to learn the ID of an object -- You can learn the ID of a document, folder, etc in one of the following ways:
The function used to perform the conversion from title to ID is: generateId in my_zope_instance/Products/CPSUtil/id.py. You may want to read the documentation in the source and the source itself for that function if you have questions about this conversion process.
Although the behavior of generateId can be modified by parameters, here are a few rules that my version of CPS seems to be following:
This section describes each of the methods exposed and supported by CPSRemoteController.
The code examples that we give are implement on top of xmlrpclib.
Prototype:
changeDocumentPosition( self, rpath, step, )
Change the position of the document within its containing folder. For example, this operation changes the order in which documents are displayed when you click on "Folder contents" in your site. Warning: This method can only be called on ordered folders and would produce errors if called, for example, on BTreeFolders.
Parameters:
Exceptions:
Prototype:
createDocument( self, portal_type, doc_def, folder_rpath, position=-1, comments="", )
Create document with the given portal_type with data from the given data dictionary.
The method returns the rpath of the created document.
Parameters:
portal_type is the type of document to be created. In your CPS portal, click on the New action to get a list of document types that can be created in a particular folder. You can learn more about these document types in my_cps_site/portal_schemas in the ZMI.
doc_def (a dictionary) contains values to be inserted in the new object. The keys in the dictionary are the names of the properties in the new object and the values are the values assigned for each property. The following properties are always valid:
You can learn about additional properties specific to each document type by looking in my_cps_site/portal_schemas in the ZMI, then clicking on a specific document definition.
folder_rpath (a string) is the path to the folder in which the document is to be created. An example is "workspaces/members/a_user_name".
position (an integer) is optional. It is used to specify the position of the new document within exiting documents in the folder. A value of zero places the new document at the top. A value of -1 (the default) places the document after all existing documents.
comments (a string) supplies optional comments.
Exceptions:
Examples -- These examples were copied from the in-line documentation, then reformatted and modified:
from xmlrpclib import ServerProxy p = ServerProxy('http://manager:xxxxx@myserver.net:8080/cps/portal_remote_controller') doc_def = {'Title': "The report from Monday meeting", 'Description': "Another boring report" } p.createDocument('File', doc_def, 'workspaces') doc_def = {'Title': "The company hires", 'Description': "The company goes well and hires" } p.createDocument('News Item', doc_def, 'workspaces') doc_def = {'Title': "The report from Monday meeting", 'Description': "Another boring report" } p.createDocument('File', doc_def, 'workspaces') doc_def = {'Title': "The company hires", 'Description': "The company goes well and hires" } p.createDocument('News Item', doc_def, 'workspaces', 0) from xmlrpclib import ServerProxy, Binary f = open('MyImage.png', 'r') binary = Binary(f.read()) f.close() doc_def = {'Title': "The report from Monday meeting", 'Description': "Another boring report", 'file_name': "MyImage.png", 'file': binary, } p.createDocument('File', doc_def, 'workspaces') doc_def = {'Title': "The company hires", 'Description': "The company goes well and hires" } p.createDocument('News Item', doc_def, 'workspaces', 2) from xmlrpclib import ServerProxy, Binary f = open('MyImage.png', 'r') binary = Binary(f.read()) f.close() doc_def = {'Title': "The report from Monday meeting", 'Description': "Another boring report", 'file_name': "MyImage.png", 'file_key': 'file_zip', 'file': binary, } p.createDocument('File', doc_def, 'workspaces') doc_def = {'Title': "The company hires", 'Description': "The company goes well and hires" } p.createDocument('News Item', doc_def, 'workspaces', 2)
And, here is one additional example. This one creates an object of type Document, adds some content in the document, and specifies the content format:
# # Create new document in the user's private directory. # def test_createDocument(user, title, description): constr = 'http://%s:%s%s@thrush:8085/cps1/portal_remote_controller' % \ (user, user, user, ) proxy = ServerProxy(constr) content_template = '''\ Content: - Title: %s - Description: %s ''' content = content_template % (title, description, ) doc_def = {'Title': title, 'Description': description, 'content': content, 'content_format': 'stx', } doc_rpath = 'workspaces/members/%s' % user result = proxy.createDocument('Document', doc_def, doc_rpath) print 'result: "%s"' % result
Explanation:
We specify the values of the content and content_format properties. Your question at this point might be: (1) How did we learn the names/IDs of the properties for this document type? And, (2) how do we find out what values these properties can take. Here are a few guides to help you find out:
Hopefully, you will be able to do similar investigative work to learn about the properties of other object types.
Prototype:
deleteDocument(self, rpath)
Delete the document with the given rpath.
Parameters:
Exceptions
Example:
# # Delete a document. # def test_deleteDocument(user, title, description): constr = 'http://%s:%s%s@thrush:8085/cps1/portal_remote_controller' % \ (user, user, user, ) proxy = ServerProxy(constr) doc_rpath = 'workspaces/members/%s/%s' % (user, title, ) print 'deleting -- doc_rpath: %s' % doc_rpath proxy.deleteDocument(doc_rpath)
Prototype:
deleteDocuments(self, rpaths)
Delete the documents corresponding to the given rpaths.
Parameters:
Example:
# Delete a set of documents. # The documents are specified as titles = 'doc1:doc2:doc3 ...' # def test_deleteDocuments(user, titles, description): constr = 'http://%s:%s%s@thrush:8085/cps1/portal_remote_controller' % \ (user, user, user, ) proxy = ServerProxy(constr) doc_rpaths = [] title_list = titles.split(':') for title in title_list: rpath = 'workspaces/members/%s/%s' % (user, title, ) doc_rpaths.append(rpath) print 'deleting -- doc_rpaths: %s' % doc_rpaths proxy.deleteDocuments(doc_rpaths)
deleteDocumentsInDirectory(self, rpath)
Delete the documents located in directory corresponding to the given rpath.
Parameters:
Exceptions:
Prototype:
editDocument( self, rpath, doc_def={}, comments="", )
Modify the specified document with data from the given data dictionary.
Parameters:
Exceptions:
Example:
# Edit/modify the content of an existing document in the # user's private directory. # def test_editDocument(user, title, description): constr = 'http://%s:%s%s@thrush:8085/cps1/portal_remote_controller' % \ (user, user, user, ) proxy = ServerProxy(constr) content_template = '''\ This is edited content. Content: - Title: %s - Description: %s ''' content = content_template % (title, description, ) doc_def = {'Title': title, 'Description': description, 'content': content, 'content_format': 'stx', } doc_rpath = 'workspaces/members/%s/%s' % (user, title, ) position = 1 comment = 'Comment for %s' % title print 'editing document -- doc_rpath: %s doc_def: %s' % \ (doc_rpath, doc_def, ) result = proxy.editDocument(doc_rpath, doc_def, comment) print 'result: "%s"' % result
Prototype:
editOrCreateDocument( self, rpath, portal_type, doc_def, position=-1, comments="", )
Create or edit a document with the given portal_type with data from the given data dictionary.
The method returns the rpath of the created or edited document.
Parameters -- Same as for createDocument.
Exceptions:
Prototype:
getDocumentHistory(self, rpath)
Return the document history.
Parameters:
Example:
# Get document history. # def test_getDocumentHistory(user, rpath): constr = 'http://%s:%s%s@thrush:8085/cps1/portal_remote_controller' % \ (user, user, user, ) proxy = ServerProxy(constr) print 'getting doc history -- user: %s rpath: %s' % \ (user, rpath, ) history_simplified = proxy.getDocumentHistory(rpath) print 'history:' for action, time in history_simplified.items(): print ' action: %s time: %s' % (action, time, )
Prototype:
getDocumentState(self, rpath)
Return the workflow state of the document specified by the given relative path.
Parameters:
Prototype:
getOriginalDocument(self, rpath)
Return the path to the original document that was used to publish the document specified by the given path.
Parameters:
Prototype:
getPublishedDocuments(self, rpath)
Return a list of rpaths of documents which are published versions of the document specified by the given path.
Parameters:
Prototype:
isDocumentLocked(self, rpath)
Return whether the document is locked (in the WebDAV sense) or not.
Parameters:
Example -- See lockDocument.
Prototype:
listContent(self, rpath)
Return a list of documents contained in the folder specified by the given relative path.
Parameters:
Example:
from xmlrpclib import ServerProxy def test(): proxy = ServerProxy('http://some_user:xxxxx@thrush:8085/cps1/portal_remote_controller') workspaces = proxy.listContent('workspaces') folder_contents = proxy.listContent('workspaces/members/some_user') print 'folder_contents:' for count, item in enumerate(folder_contents): print ' %d. %s' % (count, item, ) test()
Prototype:
deleteDocumentLocks(self, rpath)
Delete all the locks owned by a user on the specified document.
Calling this method should be avoided but might be useful when a client application crashes and loses all the user locks.
Parameters:
Exceptions:
Prototype:
lockDocument(self, rpath)
Lock the document and return the associated lock token or return False if some problem occurs.
Parameters:
Example:
from xmlrpclib import ServerProxy def test(): proxy = ServerProxy('http://some_user:xxxxx@thrush:8085/cps1/portal_remote_controller') rpath = 'workspaces/members/some_user/document-105' result = proxy.isDocumentLocked(rpath) print '1. result: %s' % result lock = proxy.lockDocument(rpath) result = proxy.isDocumentLocked(rpath) print '2. result: %s' % result proxy.unlockDocument(rpath, lock) result = proxy.isDocumentLocked(rpath) print '3. result: %s' % result test()
Exceptions:
Prototype:
unlockDocument(self, rpath, lock_token)
Un-lock the document and return True if the operation succeeds, else return False.
Parameters:
Exceptions:
Example -- See lockDocument.
Prototype:
acceptDocument( self, rpath, comments="", )
Approve the document specified by the given relative path. This method performs the same operation as the Accept action under Object actions.
Parameters:
As of this writing, CPSRemoteController does not expose a rejectDocument method. If you need that functionality, see section rejectDocument.
Exceptions:
Prototype:
publishDocument( self, doc_rpath, rpaths_to_publish, wait_for_approval=False, comments="", )
Publish the document specified by the given relative path.
Parameters:
Prototype:
unpublishDocument(self, rpath, comments="")
Unpublish the document specified by the given relative path.
Parameters:
Prototype:
checkPermission( self, rpath, permission, )
Check the given permission for the current user on the given context.
Parameters:
Prototype:
getLocalRoles(self, username, rpath)
Return the roles of the given user local to the specified context.
N.B.: This method doesn't know how to deal with blocked roles.
Parameters:
Prototype:
getRoles(self, username)
Return the roles of the given user.
Parameters:
Example:
# Change document position. # def test_getRoles(username): constr = 'http://%s:%s%s@thrush:8085/cps1/portal_remote_controller' % \ (username, username, username, ) proxy = ServerProxy(constr) print 'getting user roles -- user: %s' % username roles = proxy.getRoles(username) print 'roles: %s' % roles
For our example, we will implement a method that will create a new user.
Preparing for upgrades to CPSRemoteController -- We will want to preserve our added methods when CPSRemoteController is upgraded. Therefore, we'd like to put our additional methods in a separate class and in a separate module. Unfortunately, I have not figured out how to do that. I've tried both (1) placing additional methods in a sub-class of RemoteControllerTool and (2) adding methods in a new class registered as a tool with CPSRemoteController. Neither of these approaches worked. If you know how to do something like this, please send me an email, explaining your solution.
And so, in the examples below, where we add a new method, we will simply add it at the end of class RemoteControllerTool and mark them off clearly with comments.
IANALB (I am not a lawyer but ...) -- Since you are extending CPSRemoteController and since that code is covered by the GNU General Public License, you will need to preserve and honor that license in your extensions.
The first example is trivial. It involves simply modifying the existing implementation of getDocumentHistory so that it returns a little more information.
Here is the old, existing implementation:
security.declareProtected(View, 'getDocumentHistory') def getDocumentHistory(self, rpath): """Return the document history. """ proxy = self.restrictedTraverse(rpath) history = proxy.getContentInfo(proxy=proxy, level=3)['history'] LOG(glog_key, DEBUG, "history = %s" % history) # A simplified value of the history so that it can be transported over # XML-RPC. history_simplified = {} for event in history: history_simplified[event['action']] = event['time_str'] LOG(glog_key, DEBUG, "history_simplified = %s" % history_simplified) return history_simplified
And, here is the new, extended implementation:
# # Start additional methods # security.declareProtected(View, 'getComplexDocumentHistory') def getComplexDocumentHistory(self, rpath): """Return the document history. """ proxy = self.restrictedTraverse(rpath) history = proxy.getContentInfo(proxy=proxy, level=3)['history'] LOG(glog_key, DEBUG, "history = %s" % history) # A simplified value of the history so that it can be transported over # XML-RPC. history_simplified = {} history_complex = [] # [1] for event in history: # [2] history_simplified[event['action']] = event['time_str'] history_complex.append((event['action'], event['time_str'], )) LOG(glog_key, DEBUG, "history_simplified = %s" % history_simplified) return history_simplified, history_complex # [3] # # End additional methods #
Notes:
We could further modify getComplexDocumentHistory so that it returns additional information from the event object. A hint -- The event object is a dictionary that contains the following keys:
This is another simple example. We merely copy the acceptDocument method, then change "accept" to "reject". Here is our new method:
# # Start additional methods # security.declareProtected(View, 'rejectDocument') def rejectDocument(self, rpath, comments=""): """Approve the document specified by the given relative path. rpath is of the form "sections/doc1" or "sections/folder/doc2". """ wftool = self.portal_workflow proxy = self.restrictedTraverse(rpath) if not _checkPermission(ModifyPortalContent, proxy): raise Unauthorized("You need the ModifyPortalContent permission.") context = proxy workflow_action = 'reject' allowed_transitions = wftool.getAllowedPublishingTransitions(context) LOG(glog_key, DEBUG, "allowed_transitions = %s" % str(allowed_transitions)) wftool.doActionFor(context, workflow_action, comment=comments) # # End additional methods #
Suppose that you want to add a number of users to your portal. Further, suppose that you have information about these users (e.g. login ID, first name, last name, email address) in a file. Adding users to a CPS site by hand is laborious. So, perhaps a RemoteController method could help.
Here is an implementation:
# # Start additional methods # security.declareProtected(View, 'addUser') def addUser(self, userId, userPassword, userRoles=None, email='', firstName='', lastName=''): """Add a new user to the portal. By default, the new user will have a Member role. """ mtool = getToolByName(self, 'portal_membership') if not userRoles: userRoles = ('Member', ) userDomains = [] mtool.addMember(userId, userPassword, userRoles, userDomains) member = mtool.getMemberById(userId) if member is None or not hasattr(aq_base(member), 'getMemberId'): raise ValueError("Cannot add member '%s'" % userId) memberProperties = { 'email': email, 'givenName': firstName, 'sn': lastName, } member.setMemberProperties(memberProperties) # # End additional methods #
Notes: