From 90fd21125b11f1d3ac87492d068a544b66d6f117 Mon Sep 17 00:00:00 2001 From: Jiri Kuncar Date: Fri, 24 Jun 2016 13:15:21 +0200 Subject: [PATCH] cli: support for Flask>=0.11 Signed-off-by: Jiri Kuncar --- example/app.py | 34 +++++++++++++++++++++++++++++++--- invenio_db/__init__.py | 5 +++-- invenio_db/cli.py | 8 ++++++-- tests/conftest.py | 12 +++++++++--- tests/test_db.py | 2 +- 5 files changed, 50 insertions(+), 11 deletions(-) diff --git a/example/app.py b/example/app.py index a5042f8..4b34803 100644 --- a/example/app.py +++ b/example/app.py @@ -1,8 +1,32 @@ +# -*- coding: utf-8 -*- +# +# This file is part of Invenio. +# Copyright (C) 2015, 2016 CERN. +# +# Invenio is free software; you can redistribute it +# and/or modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# Invenio is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Invenio; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, +# MA 02111-1307, USA. +# +# In applying this license, CERN does not +# waive the privileges and immunities granted to it by virtue of its status +# as an Intergovernmental Organization or submit itself to any jurisdiction. + +"""Simple example application for Invenio-DB package.""" + import os from flask import Flask -from flask_cli import FlaskCLI -from flask_sqlalchemy import SQLAlchemy from invenio_db import InvenioDB @@ -10,5 +34,9 @@ app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get( 'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db' ) -FlaskCLI(app) + +if not hasattr(app, 'cli'): + from flask_cli import FlaskCLI + FlaskCLI(app) + InvenioDB(app) diff --git a/invenio_db/__init__.py b/invenio_db/__init__.py index 872c390..b954d0c 100644 --- a/invenio_db/__init__.py +++ b/invenio_db/__init__.py @@ -28,9 +28,10 @@ Flask 1.0+ or alternatively use the Flask-CLI extension): >>> from flask import Flask - >>> from flask_cli import FlaskCLI >>> app = Flask('myapp') - >>> cli = FlaskCLI(app) + >>> if not hasattr(app, 'cli'): + ... from flask_cli import FlaskCLI + ... cli = FlaskCLI(app) Next, initialize your extension: diff --git a/invenio_db/cli.py b/invenio_db/cli.py index 8d3c0f3..1f3e4c6 100644 --- a/invenio_db/cli.py +++ b/invenio_db/cli.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2015 CERN. +# Copyright (C) 2015, 2016 CERN. # # Invenio is free software; you can redistribute it # and/or modify it under the terms of the GNU General Public License as @@ -31,11 +31,15 @@ import click from click import _termui_impl from flask import current_app -from flask_cli import with_appcontext from sqlalchemy_utils.functions import create_database, database_exists, \ drop_database from werkzeug.local import LocalProxy +try: + from flask.cli import with_appcontext +except ImportError: # pragma: no cover + from flask_cli import with_appcontext + _db = LocalProxy(lambda: current_app.extensions['sqlalchemy'].db) # Fix Python 3 compatibility issue in click diff --git a/tests/conftest.py b/tests/conftest.py index c7e67d7..59de399 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2015 CERN. +# Copyright (C) 2015, 2016 CERN. # # Invenio is free software; you can redistribute it # and/or modify it under the terms of the GNU General Public License as @@ -31,7 +31,11 @@ import pytest from flask import Flask -from flask_cli import FlaskCLI, ScriptInfo + +try: + from flask.cli import ScriptInfo +except ImportError: + from flask_cli import ScriptInfo @pytest.fixture() @@ -52,7 +56,9 @@ def app(): SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db') ) - FlaskCLI(app) + if not hasattr(app, 'cli'): + from flask_cli import FlaskCLI + FlaskCLI(app) return app diff --git a/tests/test_db.py b/tests/test_db.py index 2dfc628..ef84fe7 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -29,7 +29,7 @@ import pytest import sqlalchemy as sa from click.testing import CliRunner -from flask_cli import ScriptInfo +from conftest import ScriptInfo from mock import patch from pkg_resources import EntryPoint from sqlalchemy.exc import IntegrityError