Ejemplo n.º 1
0
def image_for ( value, level = 3, cache = True ):
    """ Converts a specified value to an ImageResource if possible.
    """
    global image_resource_cache

    from facets.ui.pyface.image_resource import ImageResource

    if not isinstance( value, basestring ):
        if isinstance( value, tuple ) and (len( value ) == 2):
            return ImageResource( width = value[0], height = value[1] )

        return value

    key             = value
    is_facets_image = (value[:1] == '@')
    if not is_facets_image:
        search_path = get_resource_path( level )
        key         = '%s[%s]' % ( value, search_path )

    result = image_resource_cache.get( key ) if cache else None
    if result is None:
        col = value.rfind( '?' )
        if col >= 0:
            encoded = value[ col + 1: ]
            value   = value[ : col ]
            result  = image_resource_cache.get( value ) if cache else None

        if result is None:
            if is_facets_image:
                try:
                    from facets.ui.image import ImageLibrary

                    result = ImageLibrary().image_resource( value )
                except:
                    import traceback
                    traceback.print_exc()
                    result = None
            else:
                result = ImageResource( value, search_path = [ search_path ] )

        if result is not None:
            result.name = value
            if col >= 0:
                from facets.extra.helper.image import HLSADerivedImage

                if cache:
                    image_resource_cache[ value ] = result

                result = HLSADerivedImage( base_image = result,
                                           encoded    = encoded )

        if cache:
            image_resource_cache[ key ] = result

    return result
Ejemplo n.º 2
0
    def _set_file_name ( self, file_name ):
        if dirname( file_name ) == '':
            # If no path was specified for the toolbox file, try to find one by
            # searching the directories in the caller's call stack:
            try:
                for level in xrange( 2, 6 ):
                    path = join( get_resource_path( level ), file_name )
                    if isfile( path ):
                        file_name = path

                        break
            except:
                pass

        old, self._file_name = self._file_name, file_name
        if old != file_name:
            self.facet_property_set( 'file_name', old, file_name )
Ejemplo n.º 3
0
    def add_volume(self, file_name=None):
        """ If **file_name** is a file, it adds an image volume specified by
            **file_name** to the image library. If **file_name** is a
            directory, it adds all image libraries contained in the directory
            to the image library. If **file_name** is omitted, all image
            libraries located in the *images* directory contained in the same
            directory as the caller are added.
        """
        # If no file name was specified, derive a path from the caller's
        # source code location:
        if file_name is None:
            file_name = join(get_resource_path(2), "images")

        if isfile(file_name):
            # Load an image volume from the specified file:
            volume = self._add_volume(file_name)
            if volume is None:
                raise FacetError("'%s' is not a valid image volume." % file_name)

            if volume.name in self.catalog:
                self._duplicate_volume(volume.name)

            self.catalog[volume.name] = volume
            self.volumes.append(volume)

        elif isdir(file_name):
            # Load all image volumes from the specified path:
            catalog = self.catalog
            volumes = self._add_path(file_name)
            for volume in volumes:
                if volume.name in catalog:
                    self._duplicate_volume(volume.name)

                catalog[volume.name] = volume

            self.volumes.extend(volumes)
        else:
            # Handle an unrecognized argument:
            raise FacetError(
                ("The add method argument must be None or a file or directory " "path, but '%s' was specified.")
                % file_name
            )
Ejemplo n.º 4
0
    def _volumes_default(self):
        result = []

        # Check for and add the 'application' image library:
        app_library = join(dirname(abspath(sys.argv[0])), "library")
        if isdir(app_library):
            result.extend(self._add_path(app_library))

        # Get all volumes in the standard Facets library directory:
        result.extend(self._add_path(join(get_resource_path(1), "..", "library")))

        # Check to see if there is an environment variable specifying a list
        # of paths containing image libraries:
        paths = facets_env.images
        if paths is not None:
            # Add all image volumes found in each path in the environment
            # variable:
            for path in paths.split(pathsep):
                result.extend(self._add_path(path))

        # Return the list of default volumes found:
        return result
Ejemplo n.º 5
0
    def add_path(self, volume_name, path=None):
        """ Adds the directory specified by **path** as a *virtual* volume
            called **volume_name**. All image files contained within path
            define the contents of the volume. If **path** is None, the
            *images* contained in the 'images' subdirectory of the same
            directory as the caller are is used as the path for the *virtual*
            volume..
        """
        # Make sure we don't already have a volume with that name:
        if volume_name in self.catalog:
            raise FacetError("The volume name '%s' is already in the image library." % volume_name)

        # If no path specified, derive one from the caller's source code
        # location:
        if path is None:
            path = join(get_resource_path(2), "images")

        # Make sure that the specified path is a directory:
        if not isdir(path):
            raise FacetError("The image volume path '%s' does not exist." % path)

        # Create the ImageVolume to describe the path's contents:
        image_volume_path = join(path, "image_volume.py")
        if exists(image_volume_path):
            volume = get_python_value(read_file(image_volume_path), "volume")
        else:
            volume = ImageVolume()

        # Set up the rest of the volume information:
        volume.set(name=volume_name, path=path, is_zip_file=False)

        # Try to bring the volume information up to date if necessary:
        volume.check_save()

        # Add the new volume to the library:
        self.catalog[volume_name] = volume
        self.volumes.append(volume)