def _Convert(self, profile): source = self.files.get(profile.source) if not source or not source.tempfile: # convert only if tempfile return False, () if not source: return False, [_(u"Image not found: ") + profile.source] p = DvPath() p.SetUniqueTempFileName() p.SetExtension(profile.extension) destPath = str(p) try: try: source.file.seek(0) except: pass try: iObj = Image.open(source) except IOError: # no file to be converted return False, () iObj = iObj.convert("RGB") # resize size = [profile.width, profile.height] if size[0] != 0 or size[1] != 0: if size[0] == 0: size[0] = size[1] elif size[1] == 0: size[1] = size[0] x, y = iObj.size if x > size[0]: y = y * size[0] / x; x = size[0] if y > size[1]: x = x * size[1] / y; y = size[1] size = x, y iObj = iObj.resize(size, Image.ANTIALIAS) iObj.save(destPath, profile.format) try: source.file.seek(0) except: pass # file meta data imgFile = open(destPath) filename = DvPath(profile.dest+"_"+source.filename) filename.SetExtension(profile.extension) file = File(filekey=profile.dest, filename=str(filename), file=imgFile, size=p.GetSize(), path=destPath, extension=profile.extension, tempfile=True) self.files.set(profile.dest, file) finally: # clean temp file p.Delete() return True, []
def test_tempfilename(self): p = DvPath("file.txt") p.SetUniqueTempFileName() self.assert_(p.GetExtension() == "txt", p.GetExtension())