Exemple #1
0
def base_link(ext, *sources, **options):
    from pylons import config
    from pylons.decorators.cache import beaker_cache

    combined = options.pop('combined', False)
    minified = options.pop('minified', False)
    beaker_options = options.pop('beaker_kwargs', False)
    fs_root = config.get('pylons.paths').get('static_files')

    if not (config.get('debug', False) or options.get('builtins', False)):
        if beaker_options:
            beaker_kwargs.update(beaker_options)

        if combined:
            sources = beaker_cache(**beaker_kwargs)(combine_sources)(
                list(sources), ext, fs_root)

        if minified:
            sources = beaker_cache(**beaker_kwargs)(minify_sources)(
                list(sources), '.min.' + ext, fs_root)

    if 'js' in ext:
        return __javascript_link(*sources, **options)
    if 'css' in ext:
        return __stylesheet_link(*sources, **options)
Exemple #2
0
def base_link(ext, *sources, **options):
    """Base function that glues all logic together.

    It parses options and calls :func:`process_sources`.

    :param ext: js or css helper
    :param sources: a list of source files. Can be a dicts with keys:
            file='/script.js':              a path to your file
            minify=False|'strip'|'minify':  should this file be minified,
                                            stripped or left as it is?
            dest='/script.min.js':          minified file destination (needed if
                                            files are not combined)
        Alternatively you can provide strings in which case they will be treated
        as
            {file='<your string>', minify=False}
        In this case the files will not be minified and 'combined' must be
        provided
    :param combined: the combined file name if the files need to be combined.
        Otherwise they have to have 'dest' keys
    :param beaker_kwargs: Beaker options to pass to caching decorators
    :param timestamp: append timestamp to links, eg. test.js?t=123012343
    :type ext: string
    :type sources: list of strings or dicts or any combination
    :type combined: string
    :type beaker_kwargs: dict
    :type timestamp: bool
    :returns: HTML source code
    
    .. versionadded:: 0.3.1
        `beaker_kwargs` parameter

    .. versionadded:: 0.3.2
        `combined_filename` parameter

    .. versionadded:: 0.3.5
        `timestamp` parameter
    """
    combined = options.pop('combined', False)
    timestamp = options.pop('timestamp', False)
    beaker_options = options.pop('beaker_kwargs', False)
    fs_root = config.get('pylons.paths').get('static_files')

    sources = list(sources)
    if not (config.get('debug', False) or options.get('builtins', False)):
        if beaker_options:
            beaker_kwargs.update(beaker_options)

        # use beaker_cache to cache the returned sources
        sources = beaker_cache(**beaker_kwargs)(process_sources)(
            sources, ext, fs_root, combined, timestamp)
    else:
        for i in range(len(sources)):
            if isinstance(sources[i], dict):
                sources[i] = sources[i]['file']

    if 'js' in ext:
        return __javascript_link(*sources, **options)
    if 'css' in ext:
        return __stylesheet_link(*sources, **options)
Exemple #3
0
def base_link(ext, *sources, **options):
    from pylons import config
    from pylons.decorators.cache import beaker_cache

    combined = options.pop('combined', False)
    minified = options.pop('minified', False)
    beaker_options = options.pop('beaker_kwargs', False)
    fs_root = config.get('pylons.paths').get('static_files')

    if not (config.get('debug', False) or options.get('builtins', False)):
        if beaker_options:
            beaker_kwargs.update(beaker_options)

        if combined:
            sources = beaker_cache(**beaker_kwargs)(combine_sources)(list(sources), ext, fs_root)

        if minified:
            sources = beaker_cache(**beaker_kwargs)(minify_sources)(list(sources), '.min.' + ext, fs_root)

    if 'js' in ext:
        return __javascript_link(*sources, **options)
    if 'css' in ext:
        return __stylesheet_link(*sources, **options)
def base_link(ext, *sources, **options):
    """Base function that glues all logic together.

    It parses options and calls :func:`minify_sources` or :func:`combine_sources`
    if apropriate.

    :param ext: js or css helper
    :param sources: paths to your files
    :param combined: if True combines sources into one file
    :param minified: if True minifies javascript or css files
    :param beaker_kwargs: Beaker options to pass to caching decorators
    :param combined_filename: filename that will be used when combining files
    :param timestamp: append `time.time` timestamp to file, eg. test.js?t=123012343
    :param strip_prefix: prefix to be stripped from `sources` URL list
    :type ext: string
    :type sources: string
    :type combined_filename: keyword arg
    :type combined: keyword arg
    :type minified: keyword arg
    :type beaker_kwargs: dict
    :type timestamp: bool
    :type strip_prefix: string
    :returns: HTML source code

    .. versionadded:: 0.3.1
        `beaker_kwargs` parameter

    .. versionadded:: 0.3.2
        `combined_filename` parameter

    .. versionadded:: 0.3.5
        `timestamp` parameter

    .. versionadded:: 0.3.6
        `strip_prefix` parameter
    """
    filename = options.pop('combined_filename', False)
    combined = options.pop('combined', False)
    minified = options.pop('minified', False)
    timestamp = options.pop('timestamp', False)
    strip_prefix = options.pop('strip_prefix', False)
    beaker_options = options.pop('beaker_kwargs', False)
    fs_root = config.get('pylons.paths').get('static_files')
    stripped_sources = []

    if filename and not combined:
        raise ValueError("combined_filename=True specifies filename for"
            " combined=True parameter which is not set.")

    if not (config.get('debug', False) or options.get('builtins', False)):
        if beaker_options:
            beaker_kwargs.update(beaker_options)

        if strip_prefix:
            stripped_sources = [source.replace(strip_prefix, '', 1)
                                for source in sources
                                if source.startswith(strip_prefix)]
            if stripped_sources:
                sources = stripped_sources

        if combined:
            # use beaker_cache decorator to cache the return value
            sources = beaker_cache(**beaker_kwargs)\
                (combine_sources)(list(sources), ext, fs_root,
                    filename, timestamp)

        if minified:
            # use beaker_cache decorator to cache the return value
            sources = beaker_cache(**beaker_kwargs)\
                (minify_sources)(list(sources), '.min.' + ext, fs_root, timestamp)

        if stripped_sources:
            sources = [strip_prefix + source for source in sources]

        sources = [url(source) for source in sources]

    if 'js' in ext:
        return __javascript_link(*sources, **options)
    if 'css' in ext:
        return __stylesheet_link(*sources, **options)