def ref_from_string(string, id): if len(string) < 1024: return SWDataValue(id, value=encode_datavalue(string)) else: output_ctx = make_local_output(id) filename, _ = output_ctx.get_filename_or_fd() with open(filename, "w") as fp: fp.write(string) output_ctx.close() return output_ctx.get_completed_ref()
def get_completed_ref(self): if not self.closed: raise Exception("FileOutputContext for ref %s must be closed before it is realised as a concrete reference" % self.refid) if self.direct_write_filename is not None or self.direct_write_fd is not None: return SW2_CompletedReference(self.refid) completed_file = producer_filename(self.refid) if self.current_size < 1024: with open(completed_file, "r") as fp: return SWDataValue(self.refid, encode_datavalue(fp.read())) else: return SW2_ConcreteReference(self.refid, size_hint=self.current_size, location_hints=[get_own_netloc()])
def get_completed_ref(self): if not self.closed: raise Exception( "FileOutputContext for ref %s must be closed before it is realised as a concrete reference" % self.refid) if self.direct_write_filename is not None or self.direct_write_fd is not None: return SW2_CompletedReference(self.refid) completed_file = producer_filename(self.refid) if self.current_size < 1024: with open(completed_file, "r") as fp: return SWDataValue(self.refid, encode_datavalue(fp.read())) else: return SW2_ConcreteReference(self.refid, size_hint=self.current_size, location_hints=[get_own_netloc()])
def write_output(self, index, write_callback): with MaybeFile(open_callback=lambda: self.open_output(index)) as fp: write_callback(fp) if fp.real_fp is not None: ret = self.ciel_runtime.synchronous_request( "close_output", { "index": index, "size": fp.bytes_written }) else: ret = self.ciel_runtime.synchronous_request( "publish_string", { "index": index, "str": encode_datavalue(fp.str) }) return ret["ref"]
def to_safe_dict(self): if self.str is not None: return {"strdata": encode_datavalue(self.str)} else: return {"filename": self.filename}