I’ve started a new project on Google Code called cmislib. It is an interoperable client library for CMIS in Python that uses the Restful AtomPub Binding of a CMIS provider to perform CRUD and query functions on the repository.
I created it for a couple of reasons. First, it’s been bugging me that, unlike our Drupal Alfresco integration, our Django Alfresco integration does not use CMIS. After talking it over with one of our clients we decided it would make more sense to create a more general purpose CMIS API for Python that Django (and any other Python app) could leverage, rather than build CMIS support directly into the Django Alfresco integration.
Second, around the time I was putting together the Getting Started with CMIS tutorial, it struck me that there needed to be an API that didn’t have a lot of dependencies and was very easy to use. Otherwise, it’s too easy to get lost in the weeds and miss the whole point of CMIS: Easily working with rich content repositories, regardless of the underlying implementation.
Even if you’ve never worked with Python before, it is super easy to get started with cmislib. The install is less than 3 steps and the API should feel very natural to anyone that’s worked with a content repository before. Check it out.
- If you don’t have Python installed already, do so. I’ve only tested on Python 2.6 so unless you’re looking to help test, stick with that.
- If you don’t have setuptools installed already, do so. It’s a nice tool to use for installing Python packages.
- Once setuptools is installed, type easy_install cmislib
That’s all there is to it. Now you’re ready to connect to your favorite CMIS-compliant repository.
There’s nothing in cmislib that is specific to any particular vendor. Once you give it your CMIS provider’s service URL and some credentials, it figures out where to go from there. But I haven’t tested with anything other than Alfresco yet, and this thing is still hot out of the oven. If you want to help test it against other CMIS 1.0cd04 repositories I’d love the help.
Anyway, let’s look at some examples using Alfresco’s public CMIS repository.
- From the command-line, start the Python shell by typing python then hit enter.
Python 2.6.3 (r263:75183, Oct 22 2009, 20:01:16)
GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
- Import the CmisClient and Repository classes:
>>> from cmislib.model import CmisClient, Repository
- Point the CmisClient at the repository’s service URL
>>> client = CmisClient('http://cmis.alfresco.com/s/cmis', 'admin', 'admin')
- Get the default repository for the service
>>> repo = client.getDefaultRepository()
- Get the repository’s properties. This for-loop spits out everything cmislib knows about the repo.
>>> info = repo.getRepositoryInfo()
>>> for k,v in info.items():
... print "%s:%s" % (k,v)
cmisSpecificationTitle:Version 1.0 Committee Draft 04
productVersion:3.2.0 (r2 2440)
productName:Alfresco Repository (Community)
Once you’ve got the Repository object you can start working with folders.
- Create a new folder in the root. You should name yours something unique.
>>> root = repo.getRootFolder()
>>> someFolder = root.createFolder('someFolder')
- Then, you can create some content:
>>> someFile = open('test.txt', 'r')
>>> someDoc = someFolder.createDocument('Test Document', contentFile=someFile)
- And, if you want, you can dump the properties of the newly-created document (this is a partial list):
>>> props = someDoc.getProperties()
>>> for k,v in props.items():
... print '%s:%s' % (k,v)
- You can also use cmislib to run CMIS queries. Let’s find the doc we just created with a full-text search. (Note that I’m currently seeing a problem with Alfresco in which the CMIS service returns one less result than what’s really there):
>>> results = repo.query("select * from cmis:document where contains('test')")
>>> for result in results:
... print result.getName()
example test script.js
- Alternatively, you can also get objects by their object ID or their path, like this:
>>> someDoc = repo.getObjectByPath('/someFolder/Test Document')
Set Python loose on your CMIS repository
These are just a few examples meant to give you a feel for the API. There are several other things you can do with cmislib. The package comes with documentation so look there for more info. If you find any problems and you want to pitch in, you can check out the source from Google Code and create issues there as well.
Give this a try and let me know what you think.
[UPDATE: I had the wrong URL for the Alfresco-hosted CMIS service. It’s fixed now.]