# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
from cmislib.model import CmisClient
from cmislib.exceptions import ObjectNotFoundException
from cmislib.exceptions import NameConstraintViolationException

def dump(repo, path):
    try:
        f = repo.getObjectByPath(path)
        props = f.getProperties()
        for p in props:
            print('%s = %s' % (p, props[p]))
        stm = f.getContentStream()
        print(stm.read())
        stm.close()
    except ObjectNotFoundException:
        print(path + ' does not exist')

client = CmisClient('http://localhost:8080/chemistry/atom', 'test', 'test')
repo = client.getRepository('test')
repo._cmisClient = client # hack to work around a known bug
# read
dump(repo, '/RW/demo.txt')
# create
s = 'A\r\nBB\r\nCCC\r\n'
dir = repo.getObjectByPath('/RW');
try:
    dir.createDocumentFromString('demo.txt', contentType='text/plain', contentString=s) # broken i Python 3.x and cmslib3
except NameConstraintViolationException:
    print('demo.txt did already exist')
# read
dump(repo, '/RW/demo.txt');
# delete
repo.getObjectByPath('/RW/demo.txt').delete()
# read
dump(repo, '/RW/demo.txt')
