Skip to content

Commit

Permalink
docs: generate docs with sphinx and furo (#2)
Browse files Browse the repository at this point in the history
* Add docs

* Create .readthedocs.yaml

* Add requirements

* Add logo

* Add errors to doc

* Configure settings and add custom session

* Add docs URL

* Change dev status
  • Loading branch information
tibue99 authored Jun 6, 2024
1 parent 282a478 commit 4ba1137
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repos:
rev: v1.10.0
hooks:
- id: mypy
args: [--disable-error-code=union-attr]
args: [--disable-error-code=union-attr, --disable-error-code=import]

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
Expand Down
18 changes: 18 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

version: 2

build:
os: ubuntu-22.04
tools:
python: "3.11"

sphinx:
configuration: docs/conf.py
fail_on_warning: false
builder: html

python:
install:
- requirements: docs/requirements.txt
12 changes: 9 additions & 3 deletions cookiebot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ def _stats_dict(data: dict[str, int]) -> dict[date, int]:


class CookieAPI:
"""A class to interact with the Cookie Bot API.
"""A class to interact with the CookieBot API.
Parameters
----------
api_key:
The API key to use. If no key is provided, ``COOKIE_KEY`` is loaded from the environment.
session:
An existing aiohttp session to use.
"""

def __init__(self, api_key: str | None = None):
self._session: aiohttp.ClientSession | None = None
def __init__(self, api_key: str | None = None, session: aiohttp.ClientSession | None = None):
self._session: aiohttp.ClientSession | None = session

if api_key is None:
load_dotenv()
Expand All @@ -48,6 +50,10 @@ async def _setup(self):
self._session = aiohttp.ClientSession()

async def close(self):
"""Close the aiohttp session. When using the async context manager,
this is called automatically.
"""

await self._session.close()

@overload
Expand Down
14 changes: 14 additions & 0 deletions cookiebot/errors.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
class CookieError(Exception):
"""Base exception class for all CookieBot exceptions."""

pass


class InvalidAPIKey(CookieError):
"""Raised when an invalid API key is provided or if no key was found
in the environment variables.
"""

def __init__(self, msg: str | None = None):
super().__init__(msg or "Invalid API key.")


class NotFound(CookieError):
"""Raised when an object is not found."""

pass


class UserNotFound(NotFound):
"""Raised when the given user ID is not found."""

def __init__(self):
super().__init__("Could not find the user ID.")


class GuildNotFound(NotFound):
"""Raised when the given guild ID is not found."""

def __init__(self):
super().__init__("Could not find the guild ID.")


class NoGuildAccess(CookieError):
"""Raised when you do not have access to a guild."""

def __init__(self):
super().__init__("You are not a member of this guild.")
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)
Binary file added docs/_static/favicon.ico
Binary file not shown.
Binary file added docs/_static/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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 os
import sys
from datetime import date

sys.path.insert(0, os.path.abspath("../"))

from cookiebot import __version__

project = "CookieBot"
copyright = f"{date.today().year}, tibue99"
author = "tibue99"
release = __version__

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.napoleon",
"sphinx.ext.intersphinx",
"sphinx_copybutton",
"sphinx_autodoc_typehints",
"myst_parser",
]

simplify_optional_unions = True
autodoc_member_order = "bysource"

intersphinx_mapping = {
"py": ("https://docs.python.org/3", None),
"aio": ("https://docs.aiohttp.org/", None),
}

templates_path = ["_templates"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = "furo"
html_static_path = ["_static"]

html_title = f"<h3 align='center'>{release}</h3>"
html_logo = "_static/logo.png"
html_favicon = "_static/favicon.ico"
6 changes: 6 additions & 0 deletions docs/cookiebot/api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
API
=======================

.. autoclass:: cookiebot.CookieAPI
:members:
:undoc-members:
6 changes: 6 additions & 0 deletions docs/cookiebot/errors.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Errors
=======================

.. automodule:: cookiebot.errors
:members:
:undoc-members:
6 changes: 6 additions & 0 deletions docs/cookiebot/models.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Models
=======================

.. automodule:: cookiebot.models
:members:
:undoc-members:
12 changes: 12 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.. include:: ../README.md
:parser: myst_parser.sphinx_


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

cookiebot/api
cookiebot/models
cookiebot/errors
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
7 changes: 7 additions & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sphinx
furo
sphinx-autodoc-typehints
sphinx-copybutton
myst-parser
aiohttp
python-dotenv
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ authors = [
{ name = "tibue99" }, { name="tobfd" }
]
classifiers = [
"Development Status :: 4 - Beta",
"Development Status :: 5 - Production/Stable",
"License :: OSI Approved :: MIT License",
"Intended Audience :: Developers",
"Operating System :: OS Independent",
Expand All @@ -27,6 +27,7 @@ dependencies = {file = "requirements.txt"}
[project.urls]
GitHub = "https://github.com/tibue99/cookie-api"
CookieBot = "https://cookie-bot.xyz"
Documentation = "https://cookiebot.readthedocs.io"

[build-system]
requires = ["setuptools>=61.0"]
Expand Down

0 comments on commit 4ba1137

Please sign in to comment.