[IMP] upgrade-util: link code documentation

Add direct code documentation from upgrade-util repo.
Adapt `extensions/github_link`.

Original commit: 3352d33997

Part-of: odoo/documentation#8836
Co-authored-by: Victor Feyens <vfe@odoo.com>
This commit is contained in:
Alvaro Fuentes 2024-04-15 11:30:21 +02:00 committed by Victor Feyens
parent 62f6502d61
commit 26fd5f3192
3 changed files with 47 additions and 27 deletions

View File

@ -9,6 +9,7 @@
- Python dependencies listed in the file `requirements.txt`. - Python dependencies listed in the file `requirements.txt`.
- Make - Make
- A local copy of the [odoo/odoo repository](https://github.com/odoo/odoo) (optional) - A local copy of the [odoo/odoo repository](https://github.com/odoo/odoo) (optional)
- A local copy of the [odoo/upgrade-util repository](https://github.com/odoo/upgrade-util) (optional)
### Instructions ### Instructions
@ -18,8 +19,9 @@
3. See [this guide](https://www.odoo.com/documentation/latest/contributing/documentation.html) 3. See [this guide](https://www.odoo.com/documentation/latest/contributing/documentation.html)
for more detailed instructions. for more detailed instructions.
Optional: place your local copy of the `odoo/odoo` repository in the parent directory or in the root Optional: place your local copy of the `odoo/odoo` and `odoo/upgrade-util` repositories in
directory of the documentation to build the latter with the documented Python docstrings. the parent directory or in the root directory of the documentation to build the latter
with the documented Python docstrings.
## Contribute to the documentation ## Contribute to the documentation

21
conf.py
View File

@ -133,6 +133,24 @@ else:
) )
odoo_dir_in_path = True odoo_dir_in_path = True
if odoo_dir_in_path:
upgrade_util_dir = next(filter(Path.exists, [Path('upgrade-util'), Path('../upgrade-util')]), None)
if not upgrade_util_dir:
_logger.warning(
"Could not find Upgrade Utils sources directory in `upgrade_util`.\n"
"The developer documentation will be built but autodoc directives will be skipped.\n"
"In order to fully build the 'Developer' documentation, clone the repository with "
"`git clone https://github.com/odoo/upgrade-util` or create a symbolic link."
)
odoo_dir_in_path = False
else:
_logger.info(
"Found Upgrade Util sources in %(directory)s",
{'directory': upgrade_util_dir.resolve()},
)
from odoo import upgrade
upgrade.__path__.append(str((upgrade_util_dir / 'src').resolve()))
# Mapping between odoo models related to master data and the declaration of the # Mapping between odoo models related to master data and the declaration of the
# data. This is used to point users to available xml_ids when giving values for # data. This is used to point users to available xml_ids when giving values for
# a field with the autodoc_field extension. # a field with the autodoc_field extension.
@ -250,6 +268,9 @@ redirects_dir = 'redirects/'
sphinx_tabs_disable_tab_closing = True sphinx_tabs_disable_tab_closing = True
sphinx_tabs_disable_css_loading = True sphinx_tabs_disable_css_loading = True
# Autodoc ordering
autodoc_member_order = 'bysource'
#=== Options for HTML output ===# #=== Options for HTML output ===#
html_theme = 'odoo_theme' html_theme = 'odoo_theme'

View File

@ -24,6 +24,7 @@ import inspect
import os.path import os.path
import werkzeug import werkzeug
import contextlib
def setup(app): def setup(app):
@ -36,6 +37,7 @@ def setup(app):
# TODO: js? # TODO: js?
if domain != 'py': if domain != 'py':
return None return None
if not (app.config.github_user and app.config.github_project): if not (app.config.github_user and app.config.github_project):
return None return None
@ -53,10 +55,8 @@ def setup(app):
return None return None
# get original from decorated methods # get original from decorated methods
try: with contextlib.suppress(AttributeError):
obj = getattr(obj, '_orig') obj = obj._orig
except AttributeError:
pass
try: try:
obj_source_path = inspect.getsourcefile(obj) obj_source_path = inspect.getsourcefile(obj)
@ -65,14 +65,21 @@ def setup(app):
# obj doesn't have a module, or something # obj doesn't have a module, or something
return None return None
import odoo
# FIXME: make finding project root project-independent # FIXME: make finding project root project-independent
project_root = os.path.join(os.path.dirname(odoo.__file__), '..') if module.startswith('odoo.upgrade.util'):
from odoo.upgrade import util
project = 'upgrade-util'
project_root = os.path.join(os.path.dirname(util.__file__), '../..')
else:
import odoo
project = 'odoo'
project_root = os.path.join(os.path.dirname(odoo.__file__), '..')
return make_github_link( return make_github_link(
app, app,
os.path.relpath(obj_source_path, project_root), project=project,
line, path=os.path.relpath(obj_source_path, project_root),
odoo_repository=True) line=line,
)
app.config.linkcode_resolve = linkcode_resolve app.config.linkcode_resolve = linkcode_resolve
return { return {
@ -80,22 +87,12 @@ def setup(app):
'parallel_write_safe': True 'parallel_write_safe': True
} }
def make_github_link(app, path, line=None, mode="blob", odoo_repository=False): def make_github_link(app, project, path, line=None, mode="blob"):
config = app.config branch = app.config.version or 'master'
if project == 'upgrade-util':
branch = 'master'
user = config.github_user urlpath = f"/{app.config.github_user}/{project}/{mode}/{branch}/{path}"
project = config.github_project
if odoo_repository:
user = 'odoo'
project = 'odoo'
urlpath = "/{user}/{project}/{mode}/{branch}/{path}".format(
user=user,
project=project,
branch=config.version or 'master',
path=path,
mode=mode,
)
return werkzeug.urls.url_unparse(( return werkzeug.urls.url_unparse((
'https', 'https',
'github.com', 'github.com',
@ -116,4 +113,4 @@ def add_doc_link(app, pagename, templatename, context, doctree):
source_suffix = app.config.source_suffix source_suffix = app.config.source_suffix
source_suffix = next(iter(source_suffix)) source_suffix = next(iter(source_suffix))
context['github_link'] = lambda mode='edit': make_github_link( context['github_link'] = lambda mode='edit': make_github_link(
app, f'content/{pagename}{source_suffix}', mode=mode) app, project=app.config.github_project, path=f'content/{pagename}{source_suffix}', mode=mode)