def _transform(self, vfs_object): """ Transforms the given source represented as an VFS instance and returns an file-like instance with the configured transformation applied. :param vfs_object: Source VFS object to read the image from :return: (object) File-like instance with the configured transformation applied :since: v0.1.00 """ _return = CacheFile() image_class = ImageImplementation.get_class() if (image_class is None): raise IOException( "Image media implementation does not support transformation") image = image_class() if (not image.is_supported("transformation")): raise IOException( "Image media implementation does not support transformation") if (not image.open_url(vfs_object.get_url())): raise IOException( "Image media implementation failed to open the original VFS object" ) image.set_mimetype(self.transformation_data['mimetype']) image.set_resize_mode(self.transformation_data['resize_mode']) image.set_size(self.transformation_data['width'], self.transformation_data['height']) colormap = image_class.get_colormap_for_depth( self.transformation_data['mimetype'], self.transformation_data['depth']) if (colormap is not None): image.set_colormap(colormap) image.transform() _return.set_data_attributes( time_cached=vfs_object.get_time_updated(), resource=self.get_url(), ) _return.write(image.read()) _return.save() _return.seek(0) return _return
def _open_wrapped_resource(self): """ Opens the wrapped resource once needed. @TODO: Nocache? :since: v0.1.00 """ cache_file = None vfs_object = Implementation.load_vfs_url(self.original_media_vfs_url, True) try: cache_file = CacheFile.load_resource(self.get_url()) if ((not vfs_object.is_valid()) or (vfs_object.is_supported("time_updated") and (not cache_file.is_up_to_date( vfs_object.get_time_updated())))): cache_file.delete() cache_file = None # except NothingMatchedException: pass if (not vfs_object.is_valid()): raise IOException("Failed to load the original VFS object") if (cache_file is None): cache_file = self._transform(vfs_object) self._set_wrapped_resource(cache_file)
def open(self, vfs_url, readonly=False): """ Opens a VFS object. :param vfs_url: VFS URL :param readonly: Open object in readonly mode :since: v0.1.00 """ if (self._wrapped_resource is not None): raise IOException( "Can't create new VFS object on already opened instance") vfs_url_data = urlsplit(vfs_url) transformation_data = Object.get_transformation_data( vfs_url_data.query) if ("mimetype" not in transformation_data or "width" not in transformation_data or "height" not in transformation_data): raise ValueException( "VFS URL given does not contain the required transformation data" ) transformation_data['depth'] = int(transformation_data.get( "depth", 32)) transformation_data['height'] = int(transformation_data['height']) transformation_data['resize_mode'] = int( transformation_data.get("resize_mode", AbstractImage.RESIZE_SCALED_FIT)) transformation_data['width'] = int(transformation_data['width']) mimetype_definition = MimeType.get_instance().get( mimetype=transformation_data['mimetype']) if (mimetype_definition is None or mimetype_definition['class'] != "image"): raise IOException("VFS object does not correspond to an image") self.original_media_vfs_url = unquote(vfs_url_data.path[1:]) self.transformation_data = transformation_data
def get_implementing_instance(self): """ Returns the implementing instance. :return: (mixed) Implementing instance :since: v0.1.00 """ if (self._wrapped_resource is None): raise IOException("VFS object not opened") return self._wrapped_resource
def get_name(self): """ Returns the name of this VFS object. :return: (str) VFS object name :since: v0.1.00 """ if (self._wrapped_resource is None): raise IOException("VFS object not opened") return self._wrapped_resource.get_id()
def get_time_created(self): """ Returns the UNIX timestamp this object was created. :return: (int) UNIX timestamp this object was created :since: v0.1.00 """ if (self._wrapped_resource is None): raise IOException("VFS object not opened") return self._wrapped_resource.get_data_attributes( "time_stored")['time_stored']
def get_type(self): """ Returns the type of this object. :return: (int) Object type :since: v0.1.00 """ # pylint: disable=no-member if (self._wrapped_resource is None): raise IOException("VFS object not opened") return Object.TYPE_FILE
def get_mimetype(self): """ Returns the mime type of this VFS object. :return: (str) VFS object mime type :since: v0.1.00 """ if (self._wrapped_resource is None): raise IOException("VFS object not opened") mimetype_definition = MimeType.get_instance().get( mimetype=self.transformation_data['mimetype']) return ("application/octet-stream" if (mimetype_definition is None) else mimetype_definition['type'])
def get_url(self): """ Returns the URL of this VFS object. :return: (str) VFS URL :since: v0.1.00 """ if (self.original_media_vfs_url is None): raise IOException("VFS object not opened") query_string = Object.get_transformation_query_string( self.transformation_data) return ("{0}:///{1}?{2}".format( self.get_implementing_scheme(), quote(self.original_media_vfs_url, "/"), query_string))