Skip to content

Commit

Permalink
docs: add sphinx documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
giuppep committed May 21, 2023
1 parent 20aa847 commit 4d21711
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 19 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,6 @@ cython_debug/
#.idea/


.vscode/
.vscode/

.DS_Store
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
90 changes: 90 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
import alabaster

project = 'flask-pg-session'
copyright = '2023, Giuseppe Papallo'
author = 'Giuseppe Papallo'
release = '0.1.0'


# -- 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.napoleon",
"sphinx.ext.viewcode",
"alabaster",
"myst_parser"
]
autosummary_generate = True # Turn on sphinx.ext.autosummary
autodoc_default_options = {
"show-inheritance": False,
# "members": True,
"member-order": "bysource",
# "special-members": "__init__",
# "undoc-members": True,
"exclude-members": "__weakref__",
}
autoclass_content = "init"

# 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 = []


# -- 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 = "alabaster"
html_theme_path = [alabaster.get_path()]

html_sidebars = {
"**": [
"about.html",
"navigation.html",
"relations.html",
"searchbox.html",
# "donate.html",
]
}

html_theme_options = {
"code_font_family": '"SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace',
"description": "A PostgreSQL-based server-side session extension for Flask.",
"code_font_size": "0.8em",
"fixed_sidebar": True,
"github_banner": False,
"github_button": True,
"github_type": "star",
"github_user": "giuppep",
"github_repo": "flask-pg-session",
"extra_nav_links": {
"Flask": "https://flask.palletsprojects.com/",
"Flask-Session": "https://flasksession.readthedocs.io/en/latest/"
},
}

# 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"]

source_suffix = {
'.rst': 'restructuredtext',
'.md': 'markdown',
}
58 changes: 58 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Welcome to flask-pg-session's documentation!
============================================

.. automodule:: flask_pg_session
:noindex:

.. contents:: Table of contents
:local:
:backlinks: entry
:depth: 2

Installation
************

You can install the ``FlaskPgSession`` extension with ``pip``:

.. code-block:: bash
pip install flask-pg-session
Usage
*****


Usage is pretty straightforward: just import ``FlaskPgSession`` and intialise it with
your ``Flask`` app:

.. code-block:: python
from flask import Flask
from flask_pg_session import FlaskPgSession
app = Flask("my-app")
# You can either pass the app as an argument to the constructor
FlaskPgSession(app)
# or initialise it separately
session = FlaskPgSession()
session.init_app(app)
API & Configuration
*******************


.. module:: flask_pg_session.session

.. autoclass:: FlaskPgSession
:members:



License
*******

``FlaskPgSession`` is open source and released under the MIT License.
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
10 changes: 10 additions & 0 deletions flask_pg_session/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
"""``FlaskPgSession`` is a `Flask <https://flask.palletsprojects.com/>`_ extensions \
that implements server-side session support and stores the data in a PostgreSQL table.
It is inspired by `Flask-Session <https://flasksession.readthedocs.io/en/latest/>`_,
but it's focused only on integration with PostgreSQL and therefore has fewer
dependencies.
It is intended for people that are already using Flask with PostgreSQL and do not want
to add another dependency to their app just for storing sessions (e.g. Redis).
"""
from .session import FlaskPgSession

__all__ = ["FlaskPgSession"]
38 changes: 20 additions & 18 deletions flask_pg_session/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,27 +280,24 @@ def save_session(
# We use this thin wrapper class to match the initialisation pattern of most Flask
# extensions.
class FlaskPgSession:
"""Flask extension for server-side sessions stored in PostgreSQL.
def __init__(self, app: Flask | None) -> None:
"""Flask extension for server-side sessions stored in PostgreSQL.
The following configuration options are supported:
- `SQLALCHEMY_DATABASE_URI`: The URI of the PostgreSQL database to use.
- `SESSION_PG_TABLE`: The name of the table to store sessions in. Defaults to
`flask_sessions`.
- `SESSION_PG_SCHEMA`: The name of the schema to store sessions in. Defaults to
`public`.
- `SESSION_KEY_PREFIX`: The prefix to use for session IDs. Defaults to "".
- `SESSION_USE_SIGNER`: Whether to sign session IDs. Defaults to False.
- `SESSION_PERMANENT`: Whether to set the `permanent` flag on sessions. Defaults to
True.
- `SESSION_AUTODELETE_EXPIRED`: Whether to automatically delete expired sessions.
Defaults to True.
- `SESSION_PG_MAX_DB_CONN`: The maximum number of database connections to use.
Defaults to 100.
"""

def __init__(self, app: Flask | None) -> None:
"""Initialise the FlaskPgSession extension.
- ``SQLALCHEMY_DATABASE_URI``: The URI of the PostgreSQL database to use.
- ``SESSION_PG_TABLE``: The name of the table to store sessions in. Defaults to
``"flask_sessions"``.
- ``SESSION_PG_SCHEMA``: The name of the schema to store sessions in. Defaults
to ``"public"``.
- ``SESSION_KEY_PREFIX``: The prefix to use for session IDs. Defaults to ``""``.
- ``SESSION_USE_SIGNER``: Whether to sign session IDs. Defaults to ``False``.
- ``SESSION_PERMANENT``: Whether to set the `permanent` flag on sessions.
Defaults to ``True``.
- ``SESSION_AUTODELETE_EXPIRED``: Whether to automatically delete expired
sessions. Defaults to ``True``.
- ``SESSION_PG_MAX_DB_CONN``: The maximum number of database connections to use.
Defaults to ``100``.
Args:
app: The Flask application to initialise the extension with.
Expand All @@ -312,6 +309,11 @@ def __init__(self, app: Flask | None) -> None:
assert self._session is not None

def init_app(self, app: Flask) -> _FlaskPgSession:
"""Initialise the extension.
Args:
app: The Flask application to initialise the extension with.
"""
self._session = _FlaskPgSession(
app.config["SQLALCHEMY_DATABASE_URI"],
table_name=app.config.get("SESSION_PG_TABLE", DEFAULT_TABLE_NAME),
Expand Down

0 comments on commit 4d21711

Please sign in to comment.