Exemple #1
0
    def convert_item_to_flask_url(self, ctx, item, filepath=None):
        """Given a relative reference like `foo/bar.css`, returns
        the Flask static url. By doing so it takes into account
        blueprints, i.e. in the aformentioned example,
        ``foo`` may reference a blueprint.

        If an absolute path is given via ``filepath``, it will be
        used instead. This is needed because ``item`` may be a
        glob instruction that was resolved to multiple files.

        If app.config("FLASK_ASSETS_USE_S3") exists and is True
        then we import the url_for function from flask.ext.s3,
        otherwise we import url_for from flask directly.

        If app.config("FLASK_ASSETS_USE_CDN") exists and is True
        then we import the url_for function from flask.
        """
        if ctx.environment._app.config.get("FLASK_ASSETS_USE_S3"):
            try:
                from flask.ext.s3 import url_for
            except ImportError as e:
                print(
                    "You must have Flask S3 to use FLASK_ASSETS_USE_S3 option")
                raise e
        elif ctx.environment._app.config.get("FLASK_ASSETS_USE_CDN"):
            try:
                from flask.ext.cdn import url_for
            except ImportError as e:
                print(
                    "You must have Flask CDN to use FLASK_ASSETS_USE_CDN option"
                )
                raise e
        else:
            from flask import url_for

        directory, rel_path, endpoint = self.split_prefix(ctx, item)

        if filepath is not None:
            filename = filepath[len(directory) + 1:]
        else:
            filename = rel_path

        flask_ctx = None
        if not _request_ctx_stack.top:
            flask_ctx = ctx.environment._app.test_request_context()
            flask_ctx.push()
        try:
            return url_for(endpoint, filename=filename)
        finally:
            if flask_ctx:
                flask_ctx.pop()
Exemple #2
0
    def convert_item_to_flask_url(self, ctx, item, filepath=None):
        """Given a relative reference like `foo/bar.css`, returns
        the Flask static url. By doing so it takes into account
        blueprints, i.e. in the aformentioned example,
        ``foo`` may reference a blueprint.

        If an absolute path is given via ``filepath``, it will be
        used instead. This is needed because ``item`` may be a
        glob instruction that was resolved to multiple files.

        If app.config("FLASK_ASSETS_USE_S3") exists and is True
        then we import the url_for function from flask.ext.s3,
        otherwise we import url_for from flask directly.

        If app.config("FLASK_ASSETS_USE_CDN") exists and is True
        then we import the url_for function from flask.
        """
        if ctx.environment._app.config.get("FLASK_ASSETS_USE_S3"):
            try:
                from flask.ext.s3 import url_for
            except ImportError as e:
                print("You must have Flask S3 to use FLASK_ASSETS_USE_S3 option")
                raise e
        elif ctx.environment._app.config.get("FLASK_ASSETS_USE_CDN"):
            try:
                from flask.ext.cdn import url_for
            except ImportError as e:
                print("You must have Flask CDN to use FLASK_ASSETS_USE_CDN option")
                raise e
        else:
            from flask import url_for

        directory, rel_path, endpoint = self.split_prefix(ctx, item)

        if filepath is not None:
            filename = filepath[len(directory)+1:]
        else:
            filename = rel_path

        flask_ctx = None
        if not _request_ctx_stack.top:
            flask_ctx = ctx.environment._app.test_request_context()
            flask_ctx.push()
        try:
            return url_for(endpoint, filename=filename)
        finally:
            if flask_ctx:
                flask_ctx.pop()
Exemple #3
0
            def get_url(item):
                if app.config.get('FUNNEL_USE_S3'):
                    try:
                        from flask.ext.s3 import url_for
                    except ImportError:
                        from flask import url_for
                else:
                    from flask import url_for

                if item.startswith(('//', 'http://', 'https://')):
                    return item
                item = item.split('?', 1)
                url = url_for('static', filename=item[0])
                if item[1]:
                    url += '?' + item[1]
                return url
            def get_url(item):
                if app.config.get('FUNNEL_USE_S3'):
                    try:
                        from flask.ext.s3 import url_for
                    except ImportError:
                        from flask import url_for
                else:
                    from flask import url_for

                if item.startswith(('//', 'http://', 'https://')):
                    return item
                item = item.split('?', 1)
                url = url_for('static', filename=item[0])
                if item[1]:
                    url += '?' + item[1]
                return url
Exemple #5
0
    def convert_item_to_flask_url(self, ctx, item, filepath=None):
        """Given a relative reference like `foo/bar.css`, returns
        the Flask static url. By doing so it takes into account
        blueprints, i.e. in the aformentioned example,
        ``foo`` may reference a blueprint.

        If an absolute path is given via ``filepath``, it will be
        used instead. This is needed because ``item`` may be a
        glob instruction that was resolved to multiple files.

        If app.config("FLASK_ASSETS_USE_S3") exists and is True
        then we import the url_for function from flask.ext.s3,
        otherwise we import url_for from flask directly.

        If app.config("FLASK_ASSETS_USE_CDN") exists and is True
        then we import the url_for function from flask.
        """
        if ctx.environment._app.config.get("FLASK_ASSETS_USE_S3"):
            try:
                from flask.ext.s3 import url_for
            except ImportError as e:
                print(
                    "You must have Flask S3 to use FLASK_ASSETS_USE_S3 option")
                raise e
        elif ctx.environment._app.config.get("FLASK_ASSETS_USE_CDN"):
            try:
                from flask.ext.cdn import url_for
            except ImportError as e:
                print(
                    "You must have Flask CDN to use FLASK_ASSETS_USE_CDN option"
                )
                raise e
        else:
            from flask import url_for

        directory, rel_path, endpoint = self.split_prefix(ctx, item)

        if filepath is not None:
            filename = filepath[len(directory) + 1:]
        else:
            filename = rel_path

        flask_ctx = None
        if not _request_ctx_stack.top:
            flask_ctx = ctx.environment._app.test_request_context()
            flask_ctx.push()
        try:
            url = url_for(endpoint, filename=filename)
            # In some cases, url will be an absolute url with a scheme and hostname.
            # (for example, when using werkzeug's host matching).
            # In general, url_for() will return a http url. During assets build, we
            # we don't know yet if the assets will be served over http, https or both.
            # Let's use // instead. url_for takes a _scheme argument, but only together
            # with external=True, which we do not want to force every time. Further,
            # this _scheme argument is not able to render // - it always forces a colon.
            if url and url.startswith('http:'):
                url = url[5:]
            return url
        finally:
            if flask_ctx:
                flask_ctx.pop()