def deserialize(data): '''From dictionary to object Publish Pack''' pp = PublishRequest() pp.parent = BlockVersion.deserialize( data[PublishRequest.SERIAL_TRACKED_KEY]) pp.parent_time = data[PublishRequest.SERIAL_PARENT_DATETIME] pp.tag = VersionTag.deserialize(data[PublishRequest.SERIAL_TAG_KEY]) pp.msg = data[PublishRequest.SERIAL_MSG_KEY] # Backward client compatibility pp.versiontag = data.get(PublishRequest.SERIAL_VTAG_KEY, None) pp.deptable = BlockVersionTable.deserialize( data[PublishRequest.SERIAL_DEP_TABLE]) pp.cells = ListDeserializer(CellDeserializer(BlockCellName)).\ deserialize(data[PublishRequest.SERIAL_CELLS_KEY]) pp.deleted = ListDeserializer(CellName).\ deserialize(data[PublishRequest.SERIAL_DELETED_KEY]) pp.renames = Renames.deserialize( data[PublishRequest.SERIAL_RENAMES_KEY]) pp.contents = DictDeserializer(CellName, ContentDeserializer(BlockCellName)).\ deserialize(data[PublishRequest.SERIAL_CONTENTS_KEY]) pp.contents_ids = DictDeserializer(CellName, ID).\ deserialize(data[PublishRequest.SERIAL_CONTENTS_ID_KEY]) # Backward client compatibility pp.origin = OriginInfo.deserialize( data.get(PublishRequest.SERIAL_ORIGIN_INFO, None)) return pp
def diff(self, base, other): data = Serializer().build(("base", base), ("other", other)) values_deserializer = ResourceDeserializer( CellDeserializer(BlockCellName), ContentDeserializer(BlockCellName)) deserializer = ChangesDeserializer(CellName, values_deserializer) return self.bson_jwt_call('diff', data=data, deserializer=deserializer)
def deserialize(data): d = DictDeserializer( BlockVersion, DictDeserializer( CellName, ResourceDeserializer( CellDeserializer(ID), ContentDeserializer(ID)))).deserialize(data) result = ReferencedResources() result.update(d) return result
def _read_referenced_resources(self, query, id_type): statement = query ret = ReferencedResources() rs = statement.fetchall() cell_des = CellDeserializer(id_type) content_des = ContentDeserializer(id_type) for r in rs: try: v = Reference.deserialize(decode_serialized_value(r[0])) scontent = decode_serialized_value(r[2]) if r[2] else None res = Resource(cell_des.deserialize(decode_serialized_value(r[1])), content_des.deserialize(scontent)) cell_name = v.ref ret[v.block_version][cell_name] = res # logger.debug("Local found: %s/%s" % (str(v.block_version), str(cell_name))) except Exception as e: tb = traceback.format_exc() logger.error("Error while reading resources %s" % str(e)) logger.debug(tb) return ret
def deserialize(data): try: names = ListDeserializer(BlockCellName).deserialize( data[Closure.SERIAL_NAMES]) d = ResourceDeserializer(CellDeserializer(ID), ContentDeserializer(ID)) resources = ListDeserializer(d).deserialize( data[Closure.SERIAL_RESOURCES]) versions = ListDeserializer(BlockVersion).deserialize( data[Closure.SERIAL_VERSIONS]) return Closure(dict(zip(names, zip(resources, versions)))) except Exception as e: raise BiiSerializationException(e)
def test_simple_published_cell(self): # Simple publish resource simpleresource = SimpleCell("user/block/path/to/file.h") simpleresource.ID = ID() simpleresource.root = ID() # Include inside "a" the "kls" attribute s = simpleresource.serialize() self.assertEqual( s[Deserializer.POLIMORPHIC_KEYWORD], ClassTypedSerializer.getValue(simpleresource.__class__)) ob = CellDeserializer(ID).deserialize(s) self.assert_bii_equal(simpleresource, ob)
def test_simple_edition_cell_with_container(self): virtual_cell = BlockCellName('user/block/path/file.h') simpleresource = SimpleCell("user/block/path/to/file.h") simpleresource.ID = BlockCellName("user/block/path/to/file.h") simpleresource.root = ID() simpleresource.dependencies.add_implicit( BlockCellName("user/block/path/file.h")) simpleresource.container = virtual_cell s = simpleresource.serialize() self.assertEqual(s[Deserializer.POLIMORPHIC_KEYWORD], ClassTypedSerializer.getValue(SimpleCell)) ob = CellDeserializer(BlockCellName).deserialize(s) self.assert_bii_equal(ob, simpleresource)
def testSerialize(self): serial = self.r.serialize() d = CellDeserializer(BlockCellName) deserialized = d.deserialize(serial) self.assertEquals(self.r, deserialized)
class MongoServerStore(MongoStore, GenericServerStore): deserializer = { GenericServerStore.PUBLISHED_CELL_ST: CellDeserializer(ID), GenericServerStore.PUBLISHED_CONTENT_ST: ContentDeserializer(ID), GenericServerStore.BLOCK_ST: Block, GenericServerStore.USER_ST: User, GenericServerStore.COUNTERS_ST: None, GenericServerStore.BLOCK_PERMISSIONS_ST: ElementPermissions, GenericServerStore.USER_SUBSCRIPTION_ST: UserSubscription } def __init__(self, connection, databasename=None): ''' connection: MongoClient, can be get from MongoStore.makeConnection ''' MongoStore.__init__(self, connection, databasename) def read_user_by_email(self, email): '''Reads user by email''' dbcol = self.db[GenericServerStore.USER_ST] doc = dbcol.find_one({User.SERIAL_EMAIL: email}) return User.deserialize(doc) if doc else None def read_user_by_oauth_token(self, provider, token): '''Reads user by github or google token''' cols = { "google": User.SERIAL_OAUTH_GOOGLE_TOKEN, "github": User.SERIAL_OAUTH_GITHUB_TOKEN } dbcol = self.db[GenericServerStore.USER_ST] doc = dbcol.find_one({cols[provider]: token}) return User.deserialize(doc) if doc else None def read_user_subscription_by_customer_id(self, customer_id): dbcol = self.db[GenericServerStore.USER_SUBSCRIPTION_ST] doc = dbcol.find_one( {UserSubscription.SERIAL_CUSTOMER_ID_KEY: customer_id}) return UserSubscription.deserialize(doc) if doc else None def exists_user_id_ignoring_case(self, brl_user): '''Check if user already exists with a case insensitive pattern''' import re dbcol = self.db[GenericServerStore.USER_ST] doc = dbcol.find_one({ User.SERIAL_ID_KEY: re.compile('^' + re.escape(brl_user) + '$', re.IGNORECASE) }) return User.deserialize(doc) if doc else None def generate_user_id(self): counters = self.db["counters"] updated = counters.find_and_modify(query={'_id': 'users'}, update={"$inc": { 'seq': 1 }}, upsert=True, new=True) return ID((updated['seq'], )) def getDeserializer(self, collection): '''Mapping our collections''' return MongoServerStore.deserializer[collection] def getDeserializerMulti(self, collection): # For read_multi keys '''Mapping our collections''' return { GenericServerStore.PUBLISHED_CELL_ST: ID, GenericServerStore.PUBLISHED_CONTENT_ST: ID, GenericServerStore.BLOCK_ST: BRLBlock, GenericServerStore.BLOCK_PERMISSIONS_ST: BRLBlock, }[collection] def check_transaction(self, brl_hive): dbcol = self.db[MongoStore.HIVE_TRANSACTIONS] transaction = dbcol.find_one({'_id': str(brl_hive)}) if transaction is not None: raise BiiPendingTransactionException( "Cannot read hive %s, try again later" % transaction) ############ Get content size ################ def read_content_sizes(self, content_ids): dbcol = self.db[GenericServerStore.PUBLISHED_CONTENT_ST] ids = [a.serialize() for a in content_ids] projection = {"l.sz": 1} cursor = dbcol.find({"_id": {"$in": ids}}, projection) result = {ID.deserialize(doc["_id"]): doc["l"]["sz"] for doc in cursor} return result ########### Get published blocks info ############ def read_published_blocks_info(self): """Gets the blocks brl's with the last publish date in a tuple. The method returns a generator, blocks can be a lot and we don't want it all in memory """ dbcol = self.db[GenericServerStore.BLOCK_ST] brls = dbcol.find({}, {"_id": 1}) # Iterator in results for ret_brl_block in brls: # Its an iterator too brl_block = BRLBlock(ret_brl_block["_id"]) the_block = self.read_block(brl_block) last_delta = the_block.last_delta if last_delta: last_pub_date = last_delta.datetime yield (brl_block, last_pub_date)
def test_virtual_cell(self): v = VirtualCell(BlockCellName('user/block/virtual.h'), "code", {"win", "nix"}) s = v.serialize() v2 = CellDeserializer(BlockCellName).deserialize(s) self.assertEqual(v, v2)