From 75631026e2a16b86bf21dbf28ad6656184e3338b Mon Sep 17 00:00:00 2001 From: Felix Delattre <fd@gfz-potsdam.de> Date: Wed, 14 Jul 2021 13:42:09 +0000 Subject: [PATCH 1/2] Added basic project structure and CI --- .gitignore | 16 +++++++++++++++ .gitlab-ci.yml | 33 ++++++++++++++++++++++++++++++ .pre-commit-config.yaml | 31 ++++++++++++++++++++++++++++ Makefile | 14 +++++++++++++ README.rst | 38 +++++++++++++++++++++++++++++++--- obmdataapi/__init__.py | 35 ++++++++++++++++++++++++++++++++ setup.py | 45 +++++++++++++++++++++++++++++++++++++++++ tests/__init__.py | 17 ++++++++++++++++ tests/test_base.py | 25 +++++++++++++++++++++++ 9 files changed, 251 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 .gitlab-ci.yml create mode 100644 .pre-commit-config.yaml create mode 100644 Makefile create mode 100644 obmdataapi/__init__.py create mode 100644 setup.py create mode 100644 tests/__init__.py create mode 100644 tests/test_base.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7d3b5be --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +*.pyc +*.log +*.pkl +*.egg-info +Pipfile +Pipfile.lock + +__pycache__ +.pytest_cache +.cache +.vscode +.idea + +build +dist +venv diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..740cd69 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,33 @@ +image: python:3-buster + +# Make pip cache the installed dependencies +variables: + PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip" +cache: + paths: + - .cache/pip + - venv/ + +stages: + - test + +before_script: + - python3 -V + - pip3 install virtualenv --quiet + - virtualenv venv --quiet + - source venv/bin/activate + - pip3 install . --quiet + - pip3 install .[tests] --quiet + +linters: + stage: test + interruptible: true + script: + - pip3 install .[linters] --quiet + - make check + +tests: + stage: test + interruptible: true + script: + - pytest tests diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..c698993 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,31 @@ +fail_fast: false +repos: + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.3.0 + hooks: + - id: check-yaml + - id: end-of-file-fixer + - id: check-builtin-literals + - id: trailing-whitespace + + - repo: https://github.com/psf/black + rev: 20.8b1 + hooks: + - id: black + args: [ --line-length=96 ] + + - repo: https://github.com/pycqa/flake8 + rev: 3.7.9 + hooks: + - id: flake8 + args: [ --max-line-length=96 ] + + - repo: local + hooks: + - id: pylint + name: pylint + entry: pylint + language: system + args: [ --disable=E0611, -E, -j4 ] + types: [ python ] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0fd7826 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +check: + pre-commit run --all-files + +install: + pip install .[tests] + pip install .[linters] + pip install -e . + pre-commit install + +format: + pre-commit run --all-files black + +clean: + rm -r -f docs/build diff --git a/README.rst b/README.rst index ac1c073..1447f3b 100644 --- a/README.rst +++ b/README.rst @@ -1,6 +1,38 @@ -======================== -openbuildingmap-data-api -======================== +=============================== +OpenBuildingMap Data API Server +=============================== + + +|Build status| + .. |Build status| image:: https://git.gfz-potsdam.de/dynamicexposure/openbuildingmap/data-api-server/badges/master/pipeline.svg + :target: https://git.gfz-potsdam.de/dynamicexposure/openbuildingmap/data-api-server/-/commits/master + +Idea +---- + +Provide an API for data access around the OpenBuildingMap. + +Requirements +------------ + + * python >= 3.7 + +Installation +------------ + + pip3 install . + +Contributing +------------ + +Development and linting dependencies can be installed with: + + pip3 install .[tests] + pip3 install .[linters] + +We use `black` and `flake8` for consistent code style and `pylint` for general linting. Project linting comes pre-defined in `.pre-commit` to be used in `https://pre-commit.com/`_. + + pre-commit install Copyright and copyleft ---------------------- diff --git a/obmdataapi/__init__.py b/obmdataapi/__init__.py new file mode 100644 index 0000000..0f17d10 --- /dev/null +++ b/obmdataapi/__init__.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2021: +# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or (at +# your option) any later version. +# +# This program 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 Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see http://www.gnu.org/licenses/. + +import logging +import sys + +logger = logging.getLogger() +logger.setLevel(logging.DEBUG) +logger.addHandler(logging.StreamHandler(sys.stdout)) + + +def main(): + + logger.info("obmapidata has started") + + sys.exit() + + +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..d053595 --- /dev/null +++ b/setup.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +# Copyright (c) 2021 +# +# * Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +from setuptools import setup, find_packages + +tests_require = [ + "pytest", +] + +linters_require = ["pre-commit", "pylint"] + +setup( + name="obmdataapi", + version="0.0.1", + description="OpenBuildingMap Data API Server", + long_description="Provide an API for data access around the OpenBuildingMap.", + url="https://git.gfz-potsdam.de/dynamicexposure/openbuildingmap/data-api-server", + license="AGPLv3+", + keywords="API", + author="Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ", + tests_require=tests_require, + extras_require={ + "tests": tests_require, + "linters": linters_require, + }, + packages=find_packages(), + include_package_data=True, + python_requires=">=3.7", +) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..6502050 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2021: +# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or (at +# your option) any later version. +# +# This program 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 Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see http://www.gnu.org/licenses/. diff --git a/tests/test_base.py b/tests/test_base.py new file mode 100644 index 0000000..86c801e --- /dev/null +++ b/tests/test_base.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2021: +# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or (at +# your option) any later version. +# +# This program 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 Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see http://www.gnu.org/licenses/. + +import logging + +logger = logging.getLogger() + + +def test_func(): + pass -- GitLab From a63b7b6ee90b15f68ef4d1df1f0ebfb9c7a14053 Mon Sep 17 00:00:00 2001 From: Felix Delattre <fd@gfz-potsdam.de> Date: Wed, 14 Jul 2021 14:21:13 +0000 Subject: [PATCH 2/2] Added very basic flask --- README.rst | 3 +- obmdataapi/__init__.py | 35 +++++++++++++++++++--- setup.py | 4 ++- tests/conftest.py | 35 ++++++++++++++++++++++ tests/{test_base.py => test_obmdataapi.py} | 6 ++-- 5 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 tests/conftest.py rename tests/{test_base.py => test_obmdataapi.py} (86%) diff --git a/README.rst b/README.rst index 1447f3b..b7a747c 100644 --- a/README.rst +++ b/README.rst @@ -16,6 +16,7 @@ Requirements ------------ * python >= 3.7 + * Flask >= 2.0.1 Installation ------------ @@ -30,7 +31,7 @@ Development and linting dependencies can be installed with: pip3 install .[tests] pip3 install .[linters] -We use `black` and `flake8` for consistent code style and `pylint` for general linting. Project linting comes pre-defined in `.pre-commit` to be used in `https://pre-commit.com/`_. +We use `black` and `flake8` for consistent code style and `pylint` for general linting. Project linting comes pre-defined in `.pre-commit` to be used in `pre-commit <https://pre-commit.com/>`_. pre-commit install diff --git a/obmdataapi/__init__.py b/obmdataapi/__init__.py index 0f17d10..ec46e44 100644 --- a/obmdataapi/__init__.py +++ b/obmdataapi/__init__.py @@ -17,19 +17,46 @@ # along with this program. If not, see http://www.gnu.org/licenses/. import logging +import os import sys +from flask import Flask + logger = logging.getLogger() logger.setLevel(logging.DEBUG) logger.addHandler(logging.StreamHandler(sys.stdout)) +app = Flask(__name__) + -def main(): +def create_app(test_config=None): + """Create and configure an instance of the Flask application.""" logger.info("obmapidata has started") - sys.exit() + app = Flask(__name__, instance_relative_config=True) + app.config.from_mapping( + # a default secret that should be overridden by instance config + SECRET_KEY="dev" + ) + + if test_config is None: + # load the instance config, if it exists, when not testing + app.config.from_pyfile("config.py", silent=True) + else: + # load the test config if passed in + app.config.update(test_config) + + # ensure the instance folder exists + try: + os.makedirs(app.instance_path) + except OSError: + pass + + @app.route("/") + def hello_world(): + return "<p>Hello, World!</p>" + app.add_url_rule("/", endpoint="index") -if __name__ == "__main__": - main() + return app diff --git a/setup.py b/setup.py index d053595..f2f2bea 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -# Copyright (c) 2021 +# Copyright (c) 2020-2021 # # * Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ # @@ -20,6 +20,7 @@ from setuptools import setup, find_packages tests_require = [ + "Flask>=2.0.1", "pytest", ] @@ -34,6 +35,7 @@ setup( license="AGPLv3+", keywords="API", author="Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ", + install_requires=["Flask>=2.0.1"], tests_require=tests_require, extras_require={ "tests": tests_require, diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..e371511 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2021: +# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or (at +# your option) any later version. +# +# This program 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 Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see http://www.gnu.org/licenses/. + +import pytest + +from obmdataapi import create_app + + +@pytest.fixture +def app(): + # create the app with common test config + app = create_app({"TESTING": True}) + + yield app + + +@pytest.fixture +def client(app): + """A test client for the app.""" + return app.test_client() diff --git a/tests/test_base.py b/tests/test_obmdataapi.py similarity index 86% rename from tests/test_base.py rename to tests/test_obmdataapi.py index 86c801e..ce3cf57 100644 --- a/tests/test_base.py +++ b/tests/test_obmdataapi.py @@ -21,5 +21,7 @@ import logging logger = logging.getLogger() -def test_func(): - pass +def test_index(client): + response = client.get("/") + assert b"Hello" in response.data + assert b"World" in response.data -- GitLab