def test_deserialize(self): self.doc.add_resource( Resource(path="/example", content="example"), False ) self.doc.freeze() serial = self.doc.serialize() newdoc = Document(self.doc.name) newdoc.deserialize(serial) self.assertEqual(list(newdoc.resources.keys()), ['/example']) self.assertIsInstance(newdoc.resources['/example'], Resource) initial_resources = newdoc._initial.resources self.assertEqual(list(initial_resources.keys()), ['/example']) self.assertIsInstance(initial_resources['/example'], Resource)
def do_dinit(self, args): ''' Initialize DEJE interactivity. This command must be used before any of the other d* commands. It reads from a few of the values in variable storage as initialization parameters: * idcache - EJTP identity cache * identity - location of EJTP identity in cache * docname - Name of the document for network sync * docserialized - serialized version of document The dinit command can be run more than once, but it's a bit of a reset, and may cause data loss in the stateful parts of the protocol. But it's also the only way to update the parameters used by the DEJE code - for example, any changes to the 'idcache' variable after initialization will have no effect. ''' try: params = self.get_vars( 'idcache', 'identity', 'docname', 'docserialized' ) except KeyError as e: self.fail('Need to set variable %r' % e.args[0]) cache = IdentityCache() try: cache.deserialize(params['idcache']) except: self.fail('Could not deserialize data in idcache') try: ident = cache.find_by_location(params['identity']) except KeyError: loc_string = strict(params['identity']).export() self.fail('No identity in cache for ' + loc_string) owner = Owner(ident) owner.identities = cache owner.client.rcv_callback = self.on_ejtp self.write_json = owner.client.write_json owner.client.write_json = self.write_json_wrapped if type(params['docname']) != str: json_str = strict(params['docname']).export() self.fail('Not a valid docname: ' + json_str) doc = Document(params['docname'], owner=owner) try: doc.deserialize(params['docserialized']) except Exception as e: self.fail('Failed to deserialize data:\n%r' % e) doc.signals['enact-event'].connect(self.on_event) doc.debug = self.debug # Wait until everything that could fail has gone right self.interface.owner = owner self.interface.document = doc self.output('DEJE initialized')