Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor with API #2

Merged
merged 99 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
f3eef38
Clean up the README file and license/copyright notices
pkhalaj Apr 5, 2024
559b20f
Preliminary implementation of the API and SDK.
pkhalaj May 5, 2024
f92b25c
Commit
pkhalaj May 6, 2024
b4e9784
Commit
pkhalaj May 6, 2024
3d81e5d
Fix Sphinx
pkhalaj May 6, 2024
5622ad5
Fix Sphinx
pkhalaj May 6, 2024
5109c96
Fix Sphinx
pkhalaj May 6, 2024
621cb6b
Fix Sphinx
pkhalaj May 6, 2024
ae93435
Fix Sphinx
pkhalaj May 6, 2024
d15b0e8
Fix Sphinx
pkhalaj May 6, 2024
4baba93
Fix Sphinx
pkhalaj May 6, 2024
04b9fa1
Refactor error handling.
pkhalaj May 13, 2024
29a3b45
Better errors handling
pkhalaj May 15, 2024
8522453
Change imports to absolute
mraspaud May 15, 2024
55edaa4
Switch to pyproject.toml
mraspaud May 15, 2024
83aa075
Add github actions
mraspaud May 15, 2024
cfdaa82
Fix ruff stuff (WIP).
pkhalaj May 15, 2024
5482c29
Try fixing ci
mraspaud May 15, 2024
4591f64
Fix ci
mraspaud May 15, 2024
959ff5d
Add ci.yml
mraspaud May 15, 2024
67e8dac
Merge branch 'master' into add_ci
mraspaud May 15, 2024
1b5c61a
Fix package
mraspaud May 15, 2024
6f36e15
Fix mongodb installation
mraspaud May 15, 2024
e3af5bf
Add pytest cov to ci installation
mraspaud May 15, 2024
a0bc628
Merge branch 'add_ci' into feature/refactor-with-api
mraspaud May 15, 2024
93dd84d
Fix more ruff stuff (WIP).
pkhalaj May 15, 2024
a48f963
Just run ci on 3.12
mraspaud May 15, 2024
351926d
Add pre-commit-ci config
mraspaud May 15, 2024
9bc3f85
Add pre-commit-ci
mraspaud May 15, 2024
86d20f5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
9d74b51
Remove ruff from ci in favour of pre-commit
mraspaud May 15, 2024
16fefaf
Run pytest on all test files
mraspaud May 15, 2024
99b7800
Make ruff happy.
pkhalaj May 15, 2024
e209a29
Make ruff even happier.
pkhalaj May 15, 2024
2b36906
Commit.
pkhalaj May 15, 2024
3513776
Terminate mongodb instead of killing it
mraspaud May 15, 2024
7336f0d
Refactor tests into a separate directory.
pkhalaj May 15, 2024
f6c5a72
Add a cli db test
mraspaud May 15, 2024
4453f64
Sphinx
pkhalaj May 15, 2024
42a9eba
Docstrings!
pkhalaj May 16, 2024
43f9b07
Hard code some API configs.
pkhalaj May 16, 2024
ab855bf
Hard code some API configs.
pkhalaj May 16, 2024
386cf7b
Hard code some API configs.
pkhalaj May 16, 2024
abf0354
More docstrings!
pkhalaj May 16, 2024
9ef3340
More docstrings!
pkhalaj May 16, 2024
c75d270
More docstrings!
pkhalaj May 16, 2024
c5008a5
Add a command line interface function
mraspaud May 16, 2024
56a5ab6
More docstrings!
pkhalaj May 16, 2024
ddd5e09
More docstrings!
pkhalaj May 16, 2024
d50824e
More docstrings!
pkhalaj May 16, 2024
bec9f64
More docstrings!
pkhalaj May 16, 2024
7b5f6a5
More docstrings!
pkhalaj May 16, 2024
592e72a
More docstrings!
pkhalaj May 17, 2024
5221399
Convert make.sh to an actual Makefile.
pkhalaj May 17, 2024
60f8145
Add tests for pipelines.
pkhalaj May 17, 2024
9c13ac2
More docstrings!
pkhalaj May 17, 2024
ceebf5f
More docstrings!
pkhalaj May 17, 2024
d7782d5
More docstrings!
pkhalaj May 17, 2024
ac304b1
More docstrings!
pkhalaj May 17, 2024
00aa2f5
More docstrings!
pkhalaj May 17, 2024
c4a03c3
More docstrings!
pkhalaj May 17, 2024
c1508bb
More docstrings!
pkhalaj May 17, 2024
80a985c
More docstrings!
pkhalaj May 17, 2024
b1a7d7d
More docstrings!
pkhalaj May 17, 2024
a06acab
More logging!
pkhalaj May 17, 2024
540a58c
Update README.rst
pkhalaj May 17, 2024
332e977
More logging
pkhalaj May 17, 2024
e9fbb44
More docstrings.
pkhalaj May 19, 2024
2d166ab
More docstrings.
pkhalaj May 19, 2024
d1494a5
More docstrings.
pkhalaj May 19, 2024
8748ff9
Add script entrypoint
mraspaud May 20, 2024
e206d65
CI/CD.
pkhalaj May 20, 2024
f84a761
Refactor test_db tests.
pkhalaj May 20, 2024
047f232
Refactor.
pkhalaj May 20, 2024
cd5eac3
CI/CD.
pkhalaj May 20, 2024
c1b726b
Refactor.
pkhalaj May 20, 2024
b756c12
Clean up.
pkhalaj May 20, 2024
c1d141c
Add model for SubscriberConfig.
pkhalaj May 20, 2024
4c28ebd
Refactor.
pkhalaj May 21, 2024
4927ebc
Address PR comments.
pkhalaj May 21, 2024
3a0098f
Address PR comments.
pkhalaj May 21, 2024
71ad57d
Address PR comments.
pkhalaj May 21, 2024
b5fba56
Address PR comments.
pkhalaj May 21, 2024
22c38b4
Address PR comments.
pkhalaj May 21, 2024
9bae659
Address PR comments.
pkhalaj May 21, 2024
425fe9b
Address PR comments.
pkhalaj May 21, 2024
ba3c1ae
Address PR comments.
pkhalaj May 21, 2024
6e292a6
Address PR comments.
pkhalaj May 21, 2024
a1bc1be
Address PR comments.
pkhalaj May 21, 2024
f451754
Address PR comments.
pkhalaj May 21, 2024
9fc425c
Address PR comments.
pkhalaj May 21, 2024
cbcbe59
Address PR comments.
pkhalaj May 22, 2024
16610e4
CI/CD.
pkhalaj May 22, 2024
7f6081d
Mock sphinx autodoc imports.
pkhalaj May 22, 2024
b36dbd2
Mock sphinx autodoc imports.
pkhalaj May 22, 2024
5d09360
Args.
pkhalaj May 22, 2024
12f1a0b
Fix a bug with auto-generated docs.
pkhalaj May 22, 2024
a936c8e
pytest warnings.
pkhalaj May 22, 2024
167b588
pytest warnings.
pkhalaj May 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,15 @@ coverage.xml
*.pot

# Sphinx documentation
docs/_build/
docs/build/
*.rst
!index.rst
*.doctree
*.pickle

# the actual config file [HAS TO BE ALWAYS EXCLUDED!]
config.yaml
config.yml

# temp log and storage for the test database
__temp*
23 changes: 23 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# .readthedocs.yaml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the OS, Python version and other tools you might need
build:
os: ubuntu-22.04
tools:
python: "3.12"

# Build documentation in the "docs/" directory with Sphinx
sphinx:
configuration: docs/source/conf.py

# Optional but recommended, declare the Python requirements required
# to build your documentation
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
python:
install:
- requirements: docs/requirements.txt
37 changes: 34 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
doobie
======

Database interface for pytroll
The database interface of `Pytroll <https://pytroll.github.io/>`_


Copyright (C)
2012, 2014, 2015, 2024

Martin Raspaud, Pouria Khalaj, Esben S. Nielsen, Adam Dybbroe, Kristian Rune Larsen


Authors
- Martin Raspaud <[email protected]>
- Pouria Khalaj <[email protected]>
- Esben S. Nielsen <[email protected]>
- Adam Dybbroe <[email protected]>
- Kristian Rune Larsen <[email protected]>


License
This program, i.e. **pytroll-db**, is part of `Pytroll <https://pytroll.github.io/>`_.

**pytroll-db** is free software: you can redistribute it and/or modify
it under the terms of the `GNU General Public License <https://www.gnu.org/licenses/gpl-3.0.en.html>`_
as published by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.


Disclaimer
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 General Public License for more details. You should have
received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

30 changes: 6 additions & 24 deletions bin/pytroll-mongo.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2019 Martin Raspaud

# Author(s):

# Martin Raspaud <[email protected]>

# This program 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 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 General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from posttroll.subscriber import Subscribe
import logging
from threading import Thread
import yaml
import os
from threading import Thread

import yaml
from posttroll.subscriber import Subscribe
from pymongo import MongoClient

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -118,8 +100,8 @@ def setup_logging(cmd_args):
help="Log config file to use instead of the standard logging.")
parser.add_argument("-v", "--verbose", dest="verbosity", action="count", default=0,
help="Verbosity (between 1 and 2 occurrences with more leading to more "
"verbose logging). WARN=0, INFO=1, "
"DEBUG=2. This is overridden by the log config file if specified.")
"verbose logging). WARN=0, INFO=1, "
"DEBUG=2. This is overridden by the log config file if specified.")
cmd_args = parser.parse_args()

logger = logging.getLogger("mongo_recorder")
Expand Down
2 changes: 2 additions & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sphinx==7.2.6
sphinx-rtd-theme==2.0.0
69 changes: 69 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys

from sphinx.ext import apidoc

sys.path.insert(0, os.path.abspath('../../trolldb'))

# -- Project information -----------------------------------------------------

project = 'Pytroll-db'
copyright = '2024, Pytroll'
author = 'Pouria Khalaj'

# The full version, including alpha/beta/rc tags
release = '0.1'

# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.viewcode',
'sphinx.ext.napoleon',
'sphinx.ext.duration',
'sphinx.ext.doctest',
'sphinx.ext.autosummary',
'sphinx.ext.intersphinx',
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ["*tests/*"]
include_patterns = ["**"]

# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
# html_static_path = ['_static']


root_doc = "index"

output_dir = os.path.join('.')
module_dir = os.path.abspath('../../trolldb')
apidoc.main(['-f', '-o', output_dir, module_dir])
17 changes: 17 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Welcome to Pytroll documentation!
===========================================

.. toctree::
:maxdepth: 2
:caption: Contents:

modules



Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
File renamed without changes.
101 changes: 101 additions & 0 deletions trolldb/api/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""
The module which includes the main functionalities of the API package. This is the main module which is supposed to be
imported by the users of the package.

Note:
Functions in this module are decorated with
`pydantic.validate_call <https://docs.pydantic.dev/latest/api/validate_call/#pydantic.validate_call_decorator.validate_call>`_
so that their arguments can be validated using the corresponding type hints, when calling the function at runtime.

Note:
The following applies to the :obj:`trolldb.api` package and all its subpackages/modules.

To avoid redundant documentation and inconsistencies, only non-FastAPI components are documented via the docstrings.
For the documentation related to the FastAPI components, check out the auto-generated documentation by FastAPI.
Assuming that the API server is running on `<http://localhost:8000>`_ (example) the auto-generated documentation can
be accessed via either `<http://localhost:8000/redoc>`_ or `<http://localhost:8000/docs>`_.

Read more at `FastAPI automatics docs <https://fastapi.tiangolo.com/features/#automatic-docs>`_.
"""

import asyncio
import time
from contextlib import contextmanager
from multiprocessing import Process

import uvicorn
from fastapi import FastAPI
from pydantic import FilePath, validate_call

from api.routes import api_router
from config.config import AppConfig, parse, Timeout
from database.mongodb import mongodb_context


@validate_call
def run_server(config: AppConfig | FilePath, **kwargs) -> None:
"""
Runs the API server with all the routes and connection to the database. It first creates a FastAPI
application and runs it using `uvicorn <https://www.uvicorn.org/>`_ which is
ASGI (Asynchronous Server Gateway Interface) compliant. This function runs the event loop using
`asyncio <https://docs.python.org/3/library/asyncio.html>`_ and does not yield!

Args:
config:
The configuration of the application which includes both the server and database configurations. In case of
a :class:`FilePath`, it should be a valid path to an existing config file which will parsed as a ``.YAML``
file.

**kwargs:
The keyword arguments are the same as those accepted by the
`FastAPI class <https://fastapi.tiangolo.com/reference/fastapi/#fastapi.FastAPI>`_ and are directly passed
to it. These keyword arguments will be first concatenated with the configurations of the API server which
are read from the ``config`` argument. The keyword arguments which are passed
explicitly to the function take precedence over ``config``.
"""

config = parse(config)
app = FastAPI(**(config.api_server._asdict() | kwargs))
app.include_router(api_router)

async def _serve():
"""
An auxiliary coroutine to be used in the asynchronous execution of the FastAPI application.
"""
async with mongodb_context(config.database):
await uvicorn.Server(
config=uvicorn.Config(
host=config.api_server.url.host,
port=config.api_server.url.port,
app=app
)
).serve()

asyncio.run(_serve())


@contextmanager
@validate_call
def server_process_context(config: AppConfig | FilePath, startup_time: Timeout = 2000):
"""
A synchronous context manager to run the API server in a separate process (non-blocking) using the
`multiprocessing <https://docs.python.org/3/library/multiprocessing.html>`_ package. The main use case is envisaged
to be in testing environments.

Args:
config:
Same as ``config`` argument for :func:`run_server`.

startup_time:
The overall time that is expected for the server and the database connections to be established before
actual requests can be sent to the server. For testing purposes ensure that this is sufficiently large so
that the tests will not time out.
"""
config = parse(config)
process = Process(target=run_server, args=(config,))
process.start()
try:
time.sleep(startup_time / 1000) # `time.sleep()` expects an argument in seconds, hence the division by 1000.
yield process
finally:
process.terminate()
Empty file added trolldb/api/errors/__init__.py
Empty file.
Loading