# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import ast
import configparser
import os
import sys
from importlib.metadata import PackageNotFoundError
from importlib.metadata import version as import_version

from odoo_scripts.config import Configuration

import odoo

# 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.
sys.path.insert(0, os.path.abspath("."))

# -- 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.doctest",
    "sphinx.ext.intersphinx",
    "sphinx.ext.todo",
    "sphinx.ext.coverage",
    "sphinx.ext.graphviz",
    "sphinx.ext.viewcode",
    "sphinxodooautodoc.ext.autodoc",
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
source_suffix = ".rst"

# The master toctree document.
master_doc = "index"

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.

# The full version, including alpha/beta/rc tags.
try:
    # TEMPLATE Update with the name of the project as defined in pyproject.toml
    release = import_version("odoo-addon-icuformat")
    # The short X.Y version.
    version = ".".join(release.split(".")[:2])
except PackageNotFoundError:
    # No version number if not installed
    pass

with open(os.path.join("..", "__manifest__.py"), "r") as f:
    read_data = f.read()
d = ast.literal_eval(read_data)

# General information about the project.
project = d["name"]
copyright = "2021, 2022, 2023 XCG Consulting"
author = d["author"]
module_nospace = project.replace(" ", "")
module_description = d.get("summary", "")
module_lowercase = module_nospace.lower()

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

locale_dirs = ["locale"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ["_build"]

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = "sphinx"

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True


# -- 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 = "default"

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

# Output file base name for HTML help builder.
htmlhelp_basename = "%sdoc" % module_nospace

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    (
        master_doc,
        "%s.tex" % module_nospace,
        "%s Documentation" % project,
        author,
        "manual",
    )
]

# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [(master_doc, module_lowercase, "%s Documentation" % project, [author], 1)]

# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    (
        master_doc,
        module_nospace,
        "%s Documentation" % project,
        author,
        module_nospace,
        module_description,
        "Miscellaneous",
    )
]

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {"https://docs.python.org/3/": None}


# -- Options for sphinx-odoo-autodoc extension ----------------------------

# sphinxodooautodoc_addons : List of addons name to load (if empty, no addon will be
# loaded)
this_module = os.path.basename(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
sphinxodooautodoc_addons = [this_module]
# sphinxodooautodoc_root_path : Path of the Odoo root directory
sphinxodooautodoc_root_path = os.path.dirname(
    os.path.dirname(os.path.abspath(odoo.__file__))
)
# TODO devrait ne rien contenir si tout est bien installé comme voulu.tp
# sphinxodooautodoc_addons_path : List of paths were Odoo addons to load are located
c = None
# find setup file of superproject, if any
directory = os.path.dirname(os.getenv("PWD"))
while not c and directory:
    setup_path = os.path.join(directory, "setup.cfg")
    if os.path.isfile(setup_path):
        c = configparser.ConfigParser()
        c.read(setup_path)
        if c.has_section("odoo_scripts"):
            # reload with odoo_scripts
            c = Configuration(directory)
        else:
            c = None
    if not c:
        if os.path.dirname(directory) != directory:
            directory = os.path.dirname(directory)
        else:
            directory = None

sphinxodooautodoc_addons_path = []

if c:
    addon_dirs = set(os.path.dirname(path) for path in c.modules)

    for line in addon_dirs:
        sphinxodooautodoc_addons_path.append(os.path.join(directory, line))
else:
    # add this directory top dir
    sphinxodooautodoc_addons_path.append(
        os.path.dirname(os.path.dirname(os.getenv("PWD")))
    )
    other_addons = os.getenv("ODOO_ADDONS_PATH", default=None)
    if other_addons:
        for addon_path in other_addons.split(","):
            sphinxodooautodoc_addons_path.append(addon_path)