Пример #1
0
def getCollection(collectionId, dbSysInst, folderOnly = False):
    '''
    The basic algrithom for this function is:
    1. Find the original collection for the collectionId represented collection
    2. Update the collectionId represented collection if possible
    3. return the latest collection snapshot for the original collection
    So we can always get the latest snapshot for the original collection
    '''
    #If the collection is an uuid, for example: uuid://xxxx-xxxxxxxx-xxxx-xxxx-xxxx,
    #then get the local cache for that object. If no local cache for the object, return the collection directly
    if objTools.isUuid(collectionId) or objTools.isUfsFs(collectionId):
        #it is uuid://xxxxx or is ufsFs://xxxx
        id = objTools.getUuid(collectionId)
        #It's a UUID
        try:
            import localLibs.cache.localFileSystemCache as localFileSystemCache
            try:
                cacheSys = localFileSystemCache.localFileSystemCache(dbSysInst)
                cachedPath = cacheSys.getCached(id)
            except KeyError:
                cachedPath = None
        except ImportError:
            cachedPath = None
        if cachedPath is None:
            #No local path exist, return the collection directly
            return collectionBase.collectionBase(collectionId, dbSysInst.getCollectionDb())
    elif objTools.isUfsUrl(collectionId):
        #It is not uuid://xxx and not ufsFs:xxx, but in other ufs url format like xxxx://xxxxxx
        return getCollectionByProtocol(collectionId, dbSysInst)
    else:
        #The collectionID is not in uuid://xxxx-xxxx-xxxx format. Treat it as a path.
        cachedPath = collectionId
    #Either it is an ID that pointed to a local filesystem collection or treat it as a directory
    try:
        #print 'create new cache'
        import localLibSys
        import localLibs.collection.fileSystem.fileSystemCollection as fileSystemCollection
        import cachedCollection
        try:
            f = fileSystemCollection.fileSystemCollection(cachedPath, folderOnly)
        except fileSystemCollection.pathNotExist:
            return collectionBase.collectionBase(collectionId, dbSysInst.getCollectionDb())
        except fileSystemCollection.pathIsNotCollection:
            #print 'pathIsNotCollection, it seems to be a file', collectionId.encode('gbk', 'replace')
            return collectionInterface.emptyCollection()
        c = cachedCollection.simpleCacheCollection(dbSysInst, f)
        #print '----------------------------------'
        #print c
        return c
    except ImportError:
        pass
    raise undefinedCollection
Пример #2
0
def updateCollectionFromJson(jsonCollectionStr, htmlGen, dbInst):
    r = json.loads(jsonCollectionStr)
    for k in r:
        if k == "add":
            data = r[k]
            for i in data:
                c = collectionBase.collectionBase(i, dbInst.getCollectionDb())
                try:
                    htmlGen.write(u"adding:"+r[i]+u"<br>")
                except:
                    pass
                if type(data[i]) == list:
                    for j in data[i]:
                        c.append(j)
                else:
                    c.append(data[i])
        elif k == "del":
            data = r[k]
            for i in data:
                c = collectionBase.collectionBase(i, dbInst.getCollectionDb())
                try:
                    htmlGen.write(u"removing:"+r[i]+u"<br>")
                except:
                    pass
                if type(data[i]) == list:
                    for j in data[i]:
                        c.remove(j)
                else:
                    c.remove(data[i])
                    
        
        elif k == "update":
            data = r[k]
            for i in data:
                c = collectionBase.collectionBase(i, dbInst.getCollectionDb())
                try:
                    htmlGen.write(u"removing:"+r[i]+u"<br>")
                except:
                    pass
                l = data[i]
                if type(l) == unicode:
                    l = [l]
                c.update(l)
Пример #3
0
def importCollectionFromJson(jsonCollectionStr, htmlGen):
    r = json.loads(jsonCollectionStr)
    dbInst = dbSys.dbSysSmart()
    for i in r:
        c = collectionBase.collectionBase(i, dbInst.getCollectionDb())
        try:
            htmlGen.write(r[i]+u"<br>")
        except:
            pass
        if type(r[i]) == list:
            for j in r[i]:
                c.append(j)
        else:
            c.append(r[i])