From f9c25140639e235abd0cd1237cf241b776c29fa1 Mon Sep 17 00:00:00 2001 From: Francesco Lombardo Date: Fri, 8 May 2020 01:22:01 +0200 Subject: [PATCH] version 0.0.1 (#1) * inital version * following Flask Extension Development guideline * fix typo --- README.md | 17 ++++++- flask_python_arango/__init__.py | 69 +++++++++++++++++++++++++++ flask_python_arango/tests/__init__.py | 13 +++++ setup.py | 51 ++++++++++++++++++++ 4 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 flask_python_arango/__init__.py create mode 100644 flask_python_arango/tests/__init__.py create mode 100644 setup.py diff --git a/README.md b/README.md index 91d186e..bf72746 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,17 @@ -# flask-python-arango +# Flask-Python-Arango + python-arango support for Flask applications + +## Installation + +Install the extension with using pip, or easy_install. + +.. code:: bash + + $ pip install -U flask-python-arango + +## Usage + +## Contributing + +Please create an issue on [Github](https://github.com/zvfvrv/flask-python-arango). diff --git a/flask_python_arango/__init__.py b/flask_python_arango/__init__.py new file mode 100644 index 0000000..1003c38 --- /dev/null +++ b/flask_python_arango/__init__.py @@ -0,0 +1,69 @@ +# Copyright 2020 Francesco Lombardo + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +__all__ = ("FlaskArango") + +from arango import ArangoClient +from flask import current_app, _app_ctx_stack + + +class FlaskArango(object): + + """Manages ArangoDB connections for your Flask app. + FlaskArango objects provide access to ArangoDB MongoDB server via the :attr:`db` + attribute. You must either pass the :class:`~flask.Flask` + app to the constructor, or call :meth:`init_app`. + """ + + def __init__(self, app=None): + if app is not None: + self.init_app(app) + + def init_app(self, app): + self.app = app + app.teardown_appcontext(self.teardown) + + def connect(self): + + host = self.app.config.get("ARANGODB_HOST", None) + db_name = self.app.config.get("ARANGODB_DB", None) + db_username = self.app.config.get("ARANGODB_USERNAME", None) + db_password = self.app.config.get("ARANGODB_PSW", None) + + if host is None: + raise ValueError( + "You must set the ARANGO_HOST Flask config variable", + ) + if db_name is None: + raise ValueError( + "You must set the ARANGODB_DB Flask config variable", + ) + # Initialize the client for ArangoDB. + client = ArangoClient(hosts=host) + # Connect to database. + return client.db( + db_name, username=db_username, password=db_password) + + def teardown(self, exception): + ctx = _app_ctx_stack.top + if hasattr(ctx, 'arango_db'): + del ctx.arango_db + + @property + def connection(self): + ctx = _app_ctx_stack.top + if ctx is not None: + if not hasattr(ctx, 'arango_db'): + ctx.arango_db = self.connect() + return ctx.arango_db diff --git a/flask_python_arango/tests/__init__.py b/flask_python_arango/tests/__init__.py new file mode 100644 index 0000000..3c98830 --- /dev/null +++ b/flask_python_arango/tests/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2020 Francesco Lombardo + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..db67cbb --- /dev/null +++ b/setup.py @@ -0,0 +1,51 @@ +""" +Flask-Python-Arango +------------- + +ArangoDB support for Flask applications. + +Flask-Python-Arango is pip-installable: + + $ pip install Flask-Python-Arango + +Source code is hosted on `GitHub `. +Contributions are welcome! +""" + +from setuptools import find_packages, setup + +with open("README.md", "r") as fh: + long_description = fh.read() + +setup( + name="Flask-Python-Arango", + version="0.0.1", + url="https://github.com/zvfvrv/flask-python-arango", + download_url="https://github.com/zvfvrv/flask-python-arango/tags", + license="Apache-2.0", + author="Francesco Lombardo", + author_email="franclombardo@gmail.com", + description="Python ArangoDB support for Flask applications", + long_description=long_description, + zip_safe=False, + platforms="any", + packages=find_packages(), + install_requires=[ + "Flask>=0.12", + "python-arango>=5.4.0", + "six", + ], + classifiers=[ + "Environment :: Web Environment", + "Framework :: Flask", + "Intended Audience :: Developers", + "Operating System :: OS Independent", + "Programming Language :: Python :: 3.5", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python", + "Topic :: Internet :: WWW/HTTP :: Dynamic Content", + "Topic :: Software Development :: Libraries :: Python Modules" + ], + +) \ No newline at end of file