if self.dirty is not None: return self.dirty # Loaded but not modified if self.timestamp is not None: return self.timestamp # Not yet loaded, check the FS return self.database.get_handler_mtime(self.key) def to_str(self): return self.data def set_data(self, data): self.set_changed() self.data = data def to_text(self): raise NotImplementedError def is_empty(self): raise NotImplementedError register_handler_class(File)
self.encoding = 'utf-8' def _load_state_from_file(self, file): data = file.read() self.encoding = guess_encoding(data) self.data = unicode(data, self.encoding) ######################################################################### # API ######################################################################### def get_encoding(self): return self.encoding def to_str(self, encoding='utf-8'): return self.data.encode(encoding) def to_text(self): return unicode(self.to_str(), 'utf-8') def is_empty(self): return self.to_text().strip() == u"" register_handler_class(TextFile)
class TGZFile(TARFile): class_mimetypes = ['application/x-tgz'] class_extension = 'tgz' class_mode = 'r:gz' class TBZ2File(TARFile): class_mimetypes = ['application/x-tbz2'] class_extension = 'tbz2' class_mode = 'r:bz2' class GzipFile(File): class_mimetypes = ['application/x-gzip'] class_extension = 'gz' class Bzip2File(File): class_mimetypes = ['application/x-bzip2'] class_extension = 'bz2' # Register for cls in [ZIPFile, TARFile, TGZFile, TBZ2File, GzipFile, Bzip2File]: register_handler_class(cls)
def _get_format(self, handle): return 'PNG' def _scale_down(self, handle, ratio): xsize, ysize = self.size if ratio >= 1.0: # Convert surface = ImageSurface(FORMAT_ARGB32, xsize, ysize) ctx = Context(surface) else: # Scale xsize, ysize = int(xsize * ratio), int(ysize * ratio) surface = ImageSurface(FORMAT_ARGB32, xsize, ysize) ctx = Context(surface) ctx.scale(ratio, ratio) # Render handle.render_cairo(ctx) # Transform to a PIL image for further manipulation size = (xsize, ysize) im = frombuffer('RGBA', size, surface.get_data(), 'raw', 'BGRA', 0, 1) surface.finish() return im, xsize, ysize register_handler_class(Image) register_handler_class(SVGFile)
for name in self.get_handler_names(): handler = self.get_handler(name) if type(handler) is Folder: for x in handler.traverse(): yield x else: yield handler def traverse2(self, context=None): if context is None: context = Context() yield self, context if context.skip is True: context.skip = False else: for name in self.get_handler_names(): handler = self.get_handler(name) if type(handler) is Folder: for x, context in handler.traverse2(context): yield x, context else: yield handler, context if context.skip is True: context.skip = False # Register register_handler_class(Folder)
if self.dirty is not None: return self.dirty # Loaded but not modified if self.timestamp is not None: return self.timestamp # Not yet loaded, check the FS return self.database.fs.get_mtime(self.key) def to_str(self): return self.data def set_data(self, data): self.set_changed() self.data = data def to_text(self): raise NotImplementedError def is_empty(self): raise NotImplementedError register_handler_class(File)
class_mode = 'r:gz' class TBZ2File(TARFile): class_mimetypes = ['application/x-tbz2'] class_extension = 'tbz2' class_mode = 'r:bz2' class GzipFile(File): class_mimetypes = ['application/x-gzip'] class_extension = 'gz' class Bzip2File(File): class_mimetypes = ['application/x-bzip2'] class_extension = 'bz2' # Register for cls in [ZIPFile, TARFile, TGZFile, TBZ2File, GzipFile, Bzip2File]: register_handler_class(cls)
return 'PNG' def _scale_down(self, handle, ratio): xsize, ysize = self.size if ratio >= 1.0: # Convert surface = ImageSurface(FORMAT_ARGB32, xsize, ysize) ctx = Context(surface) else: # Scale xsize, ysize = int(xsize * ratio), int(ysize * ratio) surface = ImageSurface(FORMAT_ARGB32, xsize, ysize) ctx = Context(surface) ctx.scale(ratio, ratio) # Render handle.render_cairo(ctx) # Transform to a PIL image for further manipulation size = (xsize, ysize) im = frombuffer('RGBA', size, surface.get_data(), 'raw', 'BGRA', 0, 1) surface.finish() return im, xsize, ysize register_handler_class(Image) register_handler_class(SVGFile)
f = StringIO(data) try: im = PILImage.open(f).convert("RGBA") except IOError: return None, None # Create the thumbnail if needed state_width, state_height = self.size if state_width > width or state_height > height: # TODO Improve the quality of the thumbnails by cropping? # The only problem would be the loss of information. try: im.thumbnail((width, height), PILImage.ANTIALIAS) except IOError: # PIL does not support interlaced PNG files, raises IOError return None, None else: thumbnail = StringIO() im.save(thumbnail, format.upper(), quality=80) data = thumbnail.getvalue() thumbnail.close() else: data = self.to_str() # Store in the cache and return thumbnails[key] = data, format.lower() return data, format.lower() register_handler_class(Image)