Skip to content

Commit

Permalink
Drop Flask-Script legacy support
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoblake committed Nov 22, 2023
1 parent 2f7a1e7 commit d44f622
Show file tree
Hide file tree
Showing 7 changed files with 1 addition and 207 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
2.1.1 (Unreleased)
- Drop Flask-Script legacy support

2.1.0 (2023-10-22)
- Drop Python 2.x support.
Expand Down
24 changes: 0 additions & 24 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -231,30 +231,6 @@ when executing ``flask`` command in your shell:
.. _click: https://click.pocoo.org/docs/latest/


Legacy support
~~~~~~~~~~~~~~

If you have `Flask-Script`_ installed, then a command will be available
as ``flask_assets.ManageAssets``:

.. code-block:: python
from flask_assets import ManageAssets
manager = Manager(app)
manager.add_command("assets", ManageAssets(assets_env))
You can explicitly pass the ``assets_env`` when adding the command as above.
Alternatively, ``ManageAssets`` will import the ``current_app`` from Flask and
use the ``jinja_env``.

The command allows you to do things like rebuilding bundles from the
command line. See the list of
:ref:`available subcommands <webassets:script-commands>`.


.. _Flask-Script: http://pypi.python.org/pypi/Flask-Script


Using in Google App Engine
~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
1 change: 0 additions & 1 deletion requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pytest
Flask-Script
webassets
PyYAML
pyScss
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ exceptiongroup==1.1.3
# via pytest
flask==3.0.0
# via flask-script
flask-script==2.0.6
# via -r requirements.in
iniconfig==2.0.0
# via pytest
itsdangerous==2.1.2
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,5 @@ def parse_version(asignee):
],
tests_require=[
'pytest',
'flask-script'
],
)
91 changes: 0 additions & 91 deletions src/flask_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,97 +370,6 @@ def from_module(self, path):
for name in bundles:
self.register(name, bundles[name])

try:
import flask_script as script
except ImportError:
pass
else:
import argparse
from webassets.script import GenericArgparseImplementation, CommandError

class FlaskArgparseInterface(GenericArgparseImplementation):
"""Subclass the CLI implementation to add a --parse-templates option."""

def _construct_parser(self, *a, **kw):
super(FlaskArgparseInterface, self).\
_construct_parser(*a, **kw)
self.parser.add_argument(
'--jinja-extension', default='*.html',
help='specify the glob pattern for Jinja extensions (default: *.html)')
self.parser.add_argument(
'--parse-templates', action='store_true',
help='search project templates to find bundles')

def _setup_assets_env(self, ns, log):
env = super(FlaskArgparseInterface, self)._setup_assets_env(ns, log)
if env is not None:
if ns.parse_templates:
log.info('Searching templates...')
# Note that we exclude container bundles. By their very nature,
# they are guaranteed to have been created by solely referencing
# other bundles which are already registered.
env.add(*[b for b in self.load_from_templates(env, ns.jinja_extension)
if not b.is_container])

if not len(env):
raise CommandError(
'No asset bundles were found. '
'If you are defining assets directly within '
'your templates, you want to use the '
'--parse-templates option.')
return env

def load_from_templates(self, env, jinja_extension):
from webassets.ext.jinja2 import Jinja2Loader, AssetsExtension
from flask import current_app as app

# Use the application's Jinja environment to parse
jinja2_env = app.jinja_env

# Get the template directories of app and blueprints
template_dirs = [path.join(app.root_path, app.template_folder)]
for blueprint in app.blueprints.values():
if blueprint.template_folder is None:
continue
template_dirs.append(
path.join(blueprint.root_path, blueprint.template_folder))

return Jinja2Loader(env, template_dirs, [jinja2_env], jinja_ext=jinja_extension).\
load_bundles()

class ManageAssets(script.Command):
"""Manage assets."""
capture_all_args = True

def __init__(self, assets_env=None, impl=FlaskArgparseInterface,
log=None):
self.env = assets_env
self.implementation = impl
self.log = log

def run(self, args):
"""Runs the management script.
If ``self.env`` is not defined, it will import it from
``current_app``.
"""

if not self.env:
from flask import current_app
self.env = current_app.jinja_env.assets_environment

# Determine 'prog' - something like for example
# "./manage.py assets", to be shown in the help string.
# While we don't know the command name we are registered with
# in Flask-Assets, we are lucky to be able to rely on the
# name being in argv[1].
import sys, os.path
prog = '%s %s' % (os.path.basename(sys.argv[0]), sys.argv[1])

impl = self.implementation(self.env, prog=prog, log=self.log)
return impl.main(args)

__all__ = __all__ + ('ManageAssets',)


try:
import click
Expand Down
88 changes: 0 additions & 88 deletions tests/test_script.py

This file was deleted.

0 comments on commit d44f622

Please sign in to comment.