def clear_temporary(self): """ Retains both sender and recipient. """ self.filedataname = None self.filedata = None self.btprox = None self.gsm = None self.gps = None self.gps_gui = None if os.path.isfile(ut.to_str(test_filedata)): self.filedataname = ut.basename(test_filedata) self.filedata = read_file(test_filedata) self.update_timestamp("all")
def rename_picfile(self): if self.card.picfile is not None: dirname = ut.dirname(self.card.picfile) old_basename = ut.basename(self.card.picfile) new_basename = appuifw.query(u"Filename:", "text", old_basename) if new_basename is not None: new_picfile = dirname + "\\" + new_basename # Better check since on some platforms the target is # silently clobbered. if os.path.exists(ut.to_str(new_picfile)): appuifw.note(u"File by that name already exists", "error") return try: os.rename(ut.to_str(self.card.picfile), ut.to_str(new_picfile)) except: appuifw.note(u"Failed to rename photo", "error") return self.card.set_picfile(new_picfile)
def refresh_metadata(self): metadata = self.metadata = {} if self.filedata: metadata["data filename"] = self.filedataname if self.picfile: metadata["photo filename"] = ut.basename(self.picfile) metadata["sender"] = self.sender metadata["receiver"] = self.recipient metadata["status"] = self.mood if self.gps is not None: metadata["gps"] = self.gps if self.btprox: metadata["bt scan"] = self.btprox if self.gsm is not None: (country_code, network_code, area_code, cell_id) = self.gsm metadata["gsm"] = {"country code" : country_code, "network code" : network_code, "area code" : area_code, "cell id" : cell_id} metadata["time"] = self.time
def set_picfile(self, picfile): cell = self.cells["picfile"] cell.text = (picfile is None) and u"[ADD]" or ut.basename(picfile) self.refresh_cells(cell)
def serialize_card(card): """ We might consider making card sending more memory friendly by building the serialized form as it is required. In particular, reading the photo from a file in small chunks without storing the whole thing in memory would be useful. We could use a generator function, possibly, so that the sending function would repeatedly invoke the generator as it should require more data to send. """ host, port, path = dest_info lf = "\r\n" boundary = "-----AaB03xeql7ds" hb = "--" + boundary pbegin = hb + lf psep = lf + pbegin pend = lf + hb + "--" + lf parts = [] metaparthead = "Content-Disposition: form-data; name=\"metadata\"; filename=\"postcard-metadata.json\"\r\nContent-Type: application/json; charset=UTF-8\r\n" metapartbody = simplejson.dumps(filter_nan(card.metadata)) metapart = lf.join([metaparthead, metapartbody]) parts.append(metapart) if card.filedata is not None: filedatapartbody = card.filedata filedataparthead = "Content-Disposition: form-data; name=\"filedata\"; filename=%s\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n" % filename_str_encode(card.filedataname) filedatapart = lf.join([filedataparthead, filedatapartbody]) parts.append(filedatapart) if card.picfile is not None: #print repr(card.picfile) picpartbody = read_file(card.picfile) #print repr(picpartbody) picparthead = "Content-Disposition: form-data; name=\"picture\"; filename=%s\r\nContent-Type: image/jpeg\r\nContent-Transfer-Encoding: binary\r\n" % filename_str_encode(ut.basename(card.picfile)) picpart = lf.join([picparthead, picpartbody]) parts.append(picpart) uppartbody = "Upload" upparthead = "Content-Disposition: form-data; name=\"upload\"\r\n" uppart = lf.join([upparthead, uppartbody]) parts.append(uppart) body = "".join([pbegin, psep.join(parts), pend]) body_len = len(body) header = "POST %s HTTP/1.1\r\nHost: %s:%d\r\nConnection: close\r\nContent-type: multipart/form-data, boundary=%s\r\nContent-Length: %d\r\n" % (path, host, port, boundary, body_len) request = "".join([header, "\r\n", body]) return request