# 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

def list(indent, dir, recurse, showprops):
    for item in dir.getChildren():
        typ = item.getProperties()['cmis:objectTypeId'][5:]
        print('%s%s (%s)' % (indent, item.name, typ))
        if showprops:
            props = item.getProperties()
            for p in props:
                print('%s- %s = %s' % (indent, p, props[p]))
        if recurse and typ == 'folder':
            list(indent + '  ', item, recurse, showprops)

def test(url, usr, pwd, reponam, recurse, showprops):
    client = CmisClient(url, usr, pwd)
    repo = client.getRepository(reponam)
    repo._cmisClient = client # hack to work around a known bug
    print(repo.getRepositoryInfo()['repositoryName'])
    print(repo.getRepositoryInfo()['repositoryDescription'])
    print(repo.getRepositoryInfo()['productName'])
    print(repo.getRepositoryInfo()['productVersion'])
    list('', repo.getRootFolder(), recurse, showprops)

test('http://localhost:8080/chemistry/atom', 'test', 'test', 'test', True,True)
test('http://localhost:8080/opencms/cmisatom', 'Admin', 'admin', 'cmis-online', False, False)
