示例#1
0
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()
示例#2
0
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()