def __init__(self, name, mode="r", codec_id=None): self.name = name self.fp = open(name, mode) self.mode = mode self.objects = atlas.Messages() if mode=="r": self.bridge = Bridge(NegotiationClient(), functions=self) else: if codec_id: self.codec_id = codec_id else: self.codec_id = codecs.get_default_codec().id self.bridge = Bridge(NegotiationServer(codecs=(self.codec_id,)), functions=self) self.other_negotiation = NegotiationClient()
#License along with this library; if not, write to the Free Software #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import test_objects reload(test_objects) from test_objects import * from atlas.codecs import binary1 reload(binary1) from atlas.codecs.binary1 import * from atlas.transport.negotiation import NegotiationClient #import pdb #pdb.set_trace() file_content = open("test.binary1").read() neg = NegotiationClient(["Binary1_beta"]) assert (neg(file_content) == "found") if neg.str != gen_binary1( msg)[2:]: #reason for [2:]: drop list encoding from it print "Binary1 encoding not same!" parse = get_parser() msg2 = parse(gen_binary1(msg)[2:]) file_content = open("test.binary1").read() neg = NegotiationClient(["Binary1_beta"]) assert (neg(file_content) == "found") if neg.str != gen_binary1(msg2)[2:]: print "Binary1 decoding and Binary1 encoding not same!"
def __init__(self): Common.__init__(self) self.bridge = Bridge(NegotiationClient(), functions=self)
class File: """client for reading/writing files: currently only read support""" def __init__(self, name, mode="r", codec_id=None): self.name = name self.fp = open(name, mode) self.mode = mode self.objects = atlas.Messages() if mode == "r": self.bridge = Bridge(NegotiationClient(), functions=self) else: if codec_id: self.codec_id = codec_id else: self.codec_id = codecs.get_default_codec().id self.bridge = Bridge(NegotiationServer(codecs=(self.codec_id, )), functions=self) self.other_negotiation = NegotiationClient() def send_string(self, str): if self.mode == "w": self.fp.write(str) if self.other_negotiation.state: self.other_negotiation(str) self.bridge.process_string( self.other_negotiation.get_send_str()) def operation_received(self, op): self.objects.append(op) def read_object(self): while 1: if self.objects: return self.objects.pop(0) data = self.fp.read(buf_size) if not data: return self.bridge.process_string(data) def write_object(self, obj): self.bridge.process_operation(obj) def check_negotiation_ok(self): if not self.bridge.codec: raise IOError, "no codec negotiated in file %s" % self.name def read_all_as_list(self): objects = atlas.Messages() while 1: obj = self.read_object() if not obj: break objects.append(obj) self.check_negotiation_ok() return objects def read_all_as_dict(self): objects = {} while 1: obj = self.read_object() if not obj: break objects[obj.id] = obj self.check_negotiation_ok() return objects def read_and_analyse_all(self): objects = {} while 1: obj = self.read_object() if not obj: break objects[obj.id] = obj self.check_negotiation_ok() atlas.find_parents_children_objects(objects) return objects def close(self): self.bridge.close() self.fp.close()
class File: """client for reading/writing files: currently only read support""" def __init__(self, name, mode="r", codec_id=None): self.name = name self.fp = open(name, mode) self.mode = mode self.objects = atlas.Messages() if mode=="r": self.bridge = Bridge(NegotiationClient(), functions=self) else: if codec_id: self.codec_id = codec_id else: self.codec_id = codecs.get_default_codec().id self.bridge = Bridge(NegotiationServer(codecs=(self.codec_id,)), functions=self) self.other_negotiation = NegotiationClient() def send_string(self, str): if self.mode=="w": self.fp.write(str) if self.other_negotiation.state: self.other_negotiation(str) self.bridge.process_string(self.other_negotiation.get_send_str()) def operation_received(self, op): self.objects.append(op) def read_object(self): while 1: if self.objects: return self.objects.pop(0) data = self.fp.read(buf_size) if not data: return self.bridge.process_string(data) def write_object(self, obj): self.bridge.process_operation(obj) def check_negotiation_ok(self): if not self.bridge.codec: raise IOError, "no codec negotiated in file %s" % self.name def read_all_as_list(self): objects = atlas.Messages() while 1: obj = self.read_object() if not obj: break objects.append(obj) self.check_negotiation_ok() return objects def read_all_as_dict(self): objects = {} while 1: obj = self.read_object() if not obj: break objects[obj.id] = obj self.check_negotiation_ok() return objects def read_and_analyse_all(self): objects = {} while 1: obj = self.read_object() if not obj: break objects[obj.id] = obj self.check_negotiation_ok() atlas.find_parents_children_objects(objects) return objects def close(self): self.bridge.close() self.fp.close()
assert ( not res_lst ) #if 2 test messages are some day codec inside 5 bytes: will fail falsely lst.append(obj) assert (str(lst) == res) test_stream_decode(s1, res1) test_stream_decode(s2, res2) test_stream_decode(s3, res3) ############################################################ # test from file co.set_stream_mode() file_name = "test." + file_extension file_content = open(file_name).read() neg1 = NegotiationClient([codec_id]) assert (neg1(file_content) == "found") str1 = co.encode(msg) + co.encoder.close() if neg1.str != str1: print codec_id + " encoding not same!" msg2 = co.decode(str1) file_content = open(file_name).read() neg2 = NegotiationClient([codec_id]) assert (neg2(file_content) == "found") str2 = co.encode(msg2) + co.encoder.close() if neg2.str != str2: print "%(codec_id)s decoding and %(codec_id)s encoding not same!" % locals( ) assert (co.decoder.eos())