Ejemplo n.º 1
0
def convert_image(value, level=3):
    """ Converts a specified value to an ImageResource if possible.
    """
    if not isinstance(value, str):
        return value

    key = value
    is_pyface_image = value.startswith("@")
    if not is_pyface_image:
        search_path = get_resource_path(level)
        key = "%s[%s]" % (value, search_path)

    result = image_resource_cache.get(key)
    if result is None:
        if is_pyface_image:
            try:
                from .image.image import ImageLibrary

                result = ImageLibrary.image_resource(value)
            except Exception as exc:
                logger.error("Can't load image resource '%s'." % value)
                logger.exception(exc)
                result = None
        else:
            from pyface.image_resource import ImageResource

            result = ImageResource(value, search_path=[search_path])

        image_resource_cache[key] = result

    return result
Ejemplo n.º 2
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 Traits UI image 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 = environ.get('TRAITS_IMAGES')
        if paths is not None:
            # Determine the correct OS path separator to use:
            separator = ';'
            if system() != 'Windows':
                separator = ':'

            # Add all image volumes found in each path in the environment
            # variable:
            for path in paths.split(separator):
                result.extend(self._add_path(path))

        # Return the list of default volumes found:
        return result
Ejemplo n.º 3
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 Traits UI image 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 = environ.get( 'TRAITS_IMAGES' )
        if paths is not None:
            # Determine the correct OS path separator to use:
            separator = ';'
            if system() != 'Windows':
                separator = ':'

            # Add all image volumes found in each path in the environment
            # variable:
            for path in paths.split( separator ):
                result.extend( self._add_path( path ) )

        # Return the list of default volumes found:
        return result
Ejemplo n.º 4
0
def convert_image(value, level=3):
    """ Converts a specified value to an ImageResource if possible.
    """
    if not isinstance( value, basestring ):
        return value

    key = value
    is_pyface_image = value.startswith('@')
    if not is_pyface_image:
        search_path = get_resource_path(level)
        key = '%s[%s]' % (value, search_path)

    result = image_resource_cache.get(key)
    if result is None:
        if is_pyface_image:
            try:
                from .image.image import ImageLibrary
                result = ImageLibrary.image_resource(value)
            except Exception as exc:
                logger.error("Can't load image resource '%s'." % value)
                logger.exception(exc)
                result = None
        else:
            from pyface.image_resource import ImageResource
            result = ImageResource(value, search_path=[search_path])

        image_resource_cache[key] = result

    return result
Ejemplo n.º 5
0
def convert_image ( value, level = 3 ):
    """ Converts a specified value to an ImageResource if possible.
    """
    global image_resource_cache

    if not isinstance( value, basestring ):
        return value

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

    result = image_resource_cache.get( key )
    if result is None:
        if is_traits_image:
            try:
                from .image.image import ImageLibrary

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

        image_resource_cache[ key ] = result

    return result
def convert_image(value, level=3):
    """ Converts a specified value to an ImageResource if possible.
    """
    global image_resource_cache
    if not isinstance(value, str):
        return value

    key = value
    is_traits_image = (value[:1] == '@')
    if not is_traits_image:
        search_path = get_resource_path(level)
        key = '%s[%s]' % (value, search_path)
    result = image_resource_cache.get(key)

    if result is None:
        if is_traits_image:
            try:
                from .image.image import ImageLibrary
                result = ImageLibrary.image_resource(value)
            except:
                result = None
        else:
            from pyface.image_resource import ImageResource
            result = ImageResource(value, search_path=[search_path])

        image_resource_cache[key] = result

    return result
Ejemplo n.º 7
0
def get_template(name):
    path = get_resource_path(1)
    print path, name
    pathname = os.path.normpath("%s/%s" % (path, name))
    if os.path.exists(pathname):
        with open(pathname, "rb") as fh:
            source = fh.read()
        return source
Ejemplo n.º 8
0
def construct_path(template_dir, name):
    name = name.lstrip("/")
    if not os.path.isabs(template_dir):
        # resource path will point to the omnivore/templates directory
        path = get_resource_path(1)
        log.debug("resource path: %s" % path)
        pathname = os.path.normpath(os.path.join(path, template_dir, name))
    else:
        path = os.path.dirname(__file__)
        pathname = os.path.normpath(os.path.join(path, template_dir, name))
    return pathname
Ejemplo n.º 9
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 TraitError(
                ("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 TraitError(
                "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.trait_set(name=volume_name, path=path, is_zip_file=False)

        # Try to bring the volume information up to date if necessary:
        if volume.time_stamp < time_stamp_for(stat(path)[ST_MTIME]):
            # Note that the save could fail if the volume is read-only, but
            # that's OK, because we're only trying to do the save in case
            # a developer had added or deleted some image files, which would
            # require write access to the volume:
            volume.save()

        # Add the new volume to the library:
        self.catalog[volume_name] = volume
        self.volumes.append(volume)
Ejemplo n.º 10
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 TraitError( ("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 TraitError( "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:
        if volume.time_stamp < time_stamp_for( stat( path )[ ST_MTIME ] ):
            # Note that the save could fail if the volume is read-only, but
            # that's OK, because we're only trying to do the save in case
            # a developer had added or deleted some image files, which would
            # require write access to the volume:
            volume.save()

        # Add the new volume to the library:
        self.catalog[ volume_name ] = volume
        self.volumes.append( volume )
Ejemplo n.º 11
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 TraitError(
                    "'%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 TraitError(
                "The add method argument must be None or a file "
                "or directory path, but '%s' was specified." % file_name
            )
Ejemplo n.º 12
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 TraitError("'%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 TraitError(
                "The add method argument must be None or a file "
                "or directory path, but '%s' was specified." %
                file_name)
Ejemplo n.º 13
0
def get_htmlhelp(name):
    global root_resource_path

    # resource path will point to the omnivore/templates directory
    if root_resource_path is None:
        path = get_resource_path(1)
        root_resource_path = os.path.normpath(os.path.join(path, "../.."))
        log.debug("resource path: %s" % root_resource_path)
    attempts = []
    for subdir in help_dirs:
        pathname = os.path.normpath(
            os.path.join(root_resource_path, subdir, name))
        log.debug("Checking for htmlhelp at %s" % pathname)
        if os.path.exists(pathname):
            log.debug("Found htmlhelp for %s: %s" % (name, pathname))
            return pathname
        attempts.append(pathname)
    else:
        log.debug("No htmlhelp found for %s in %s" % (name, attempts))
        raise MissingDocumentationError(
            "Unable to locate %s help files; installation error?\nThe files should be in one of the following:\n%s\n\nbut were not found."
            % (name,
               "\n  * " + "\n  * ".join([os.path.dirname(d)
                                         for d in attempts])))
Ejemplo n.º 14
0
def resource_path(level=2):
    """Return a resource path calculated from the caller's stack.

    """
    return get_resource_path(level + 1)
Ejemplo n.º 15
0
def resource_path(level = 2):
    """Return a resource path calculated from the caller's stack.

    """
    return get_resource_path(level + 1)