Example #1
0
def build_css_sprite(sprite_name):
    """
    Create sprite file.

    Parameters:
        sprite_name <str>
    Return:
        bool
    """
    cfg = settings.CSS_BUILDER_SPRITES[sprite_name]
    paths = find_package_files(cfg["files"], settings.CSS_BUILDER_SOURCE)
    images = []
    for path in paths:
        images.append(ImageFile(path))
    if cfg.has_key("orientation"):
        if cfg["orientation"] == "vertically":
            sprite_images, width, height = build_css_sprite_vertically(images)
        elif cfg["orientation"] == "horizontaly":
            sprite_images, width, height = build_css_sprite_horizontaly(images)
        else:
            log("build_css_sprite", "Unrecognized orientation: %s" %\
                cfg["orientation"])
            return False
    else:
        sprite_images, width, height = build_css_sprite_default(images)
    create_css_sprite_file(sprite_name, sprite_images, width, height)
    return True
Example #2
0
def get_sprite_format(sprite_name, images=None):
    """
    Return sprite format

    Parameters:
        sprite_name <str>
        images <list>
    Return:
        <str> or None
    """
    sprite_cfg = settings.CSS_BUILDER_SPRITES.get(sprite_name, None)
    if sprite_cfg == None:
        return None
    if 'format' in sprite_cfg:
        format = sprite_cfg['format']
        if is_image_format(format):
            return format.upper()
        else:
            log('get_sprite_format', 'Unrecognized image format %s in sprite \
%s definition. Available formats: %s' % (format,sprite_name, IMAGE_FORMATS))
            return None
    else:
        # check if all images in sprite have the same type. If this is
        # true use this common type as the sprite format
        if images == None:
            files = find_package_files(
                        settings.CSS_BUILDER_SPRITES[sprite_name]['files'],
                        settings.CSS_BUILDER_SOURCE)
            sprite_format = None
            for f in files:
                format = imghdr.what(f)
                if not is_image_format(format):
                    log('get_sprite_format',
                        'Unrecognized image format %s. Available formats: %s'
                        % (format, IMAGE_FORMATS))
                    return None
                else:
                    format = format.upper()
                    if sprite_format == None:
                        sprite_format = format
                    elif format != sprite_format:
                        # images in different format in sprite
                        return None
            return sprite_format
        else: # TODO
            return None
Example #3
0
def found_css_sprite(path):
    """
    Return sprite which path belongs for

    Parameters:
        path <str> - absolute path to the file
    Return:
        None or <str>
    """
    for sprite in settings.CSS_BUILDER_SPRITES:
        files = find_package_files(
            settings.CSS_BUILDER_SPRITES[sprite]["files"],
            settings.CSS_BUILDER_SOURCE)
        if path in files:
            return sprite
    log("found_css_sprite", "CSS sprite for %s not found" % path)
    return None
Example #4
0
def css_sprite_is_up_to_date(sprite_name):
    """
    Checks if sprite file needs rebuild

    Parameters:
        sprite_name <str>
    Retrun:
        bool
    """
    cfg = settings.CSS_BUILDER_SPRITES[sprite_name]
    current_files = find_package_files(cfg["files"],
                                       settings.CSS_BUILDER_SOURCE)
    sprite_file = os.path.join(get_dest_dir(), sprite_name)

    if not os.path.exists(sprite_file): # sprite files doesn't exist
        return False

    try:
        sprite = Sprite.objects.get(name=sprite_name)
    except Sprite.DoesNotExist:
        return False

    # check if orientation property wasn't changed
    if cfg.get("orientation", "default") != sprite.orientation:
        return False

    sprite_part_files = sprite.images.all()

    for f in sprite_part_files:
        if not os.path.exists(f.path): # file has been removed
            return False
        # some file from css sprite files has been modified
        sprite_m_time = os.path.getmtime(sprite_file)
        if os.path.getmtime(f.path) > sprite_m_time:
            return False
    for f in current_files: # check if new files have been added
        try:
            SpriteImage.objects.get(path = f, sprite = sprite)
        except SpriteImage.DoesNotExist:
            return False
    return True