platypush/generate_missing_docs.py

151 lines
3.5 KiB
Python
Raw Permalink Normal View History

2019-12-30 10:16:55 +01:00
import os
2023-04-03 00:44:29 +02:00
from typing import Iterable, Optional
2019-12-30 10:16:55 +01:00
from platypush.backend import Backend
2019-12-30 10:16:55 +01:00
from platypush.context import get_plugin
from platypush.plugins import Plugin
from platypush.utils.manifest import get_manifests
2019-12-30 10:16:55 +01:00
def _get_inspect_plugin():
p = get_plugin('inspect')
assert p, 'Could not load the `inspect` plugin'
return p
2019-12-30 10:16:55 +01:00
def get_all_plugins():
2023-04-03 00:44:29 +02:00
return sorted([mf.component_name for mf in get_manifests(Plugin)])
2019-12-30 10:16:55 +01:00
def get_all_backends():
2023-04-03 00:44:29 +02:00
return sorted([mf.component_name for mf in get_manifests(Backend)])
2019-12-30 10:16:55 +01:00
2019-12-30 18:50:01 +01:00
def get_all_events():
return _get_inspect_plugin().get_all_events().output
2019-12-30 18:50:01 +01:00
def get_all_responses():
return _get_inspect_plugin().get_all_responses().output
2023-04-03 00:44:29 +02:00
def _generate_components_doc(
index_name: str,
package_name: str,
components: Iterable[str],
doc_dir: Optional[str] = None,
):
if not doc_dir:
doc_dir = index_name
index_file = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'docs',
'source',
2023-04-03 00:44:29 +02:00
f'{index_name}.rst',
)
2023-04-03 00:44:29 +02:00
docs_dir = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'docs',
'source',
'platypush',
2023-04-03 00:44:29 +02:00
doc_dir,
)
2019-12-30 10:16:55 +01:00
2023-04-03 00:44:29 +02:00
for comp in components:
comp_file = os.path.join(docs_dir, comp + '.rst')
if not os.path.exists(comp_file):
comp = f'platypush.{package_name}.{comp}'
header = '``' + '.'.join(comp.split('.')[2:]) + '``'
2019-12-30 10:16:55 +01:00
divider = '=' * len(header)
2023-04-03 00:44:29 +02:00
body = f'\n.. automodule:: {comp}\n :members:\n'
2019-12-30 10:16:55 +01:00
out = '\n'.join([header, divider, body])
2023-04-03 00:44:29 +02:00
with open(comp_file, 'w') as f:
2019-12-30 10:16:55 +01:00
f.write(out)
2023-04-03 00:44:29 +02:00
with open(index_file, 'w') as f:
f.write(
2023-04-03 00:44:29 +02:00
f'''
{index_name.title()}
{''.join(['='] * len(index_name))}
2019-12-30 10:16:55 +01:00
.. toctree::
:maxdepth: 1
2023-04-03 00:44:29 +02:00
:caption: {index_name.title()}:
2019-12-30 10:16:55 +01:00
'''
)
2019-12-30 10:16:55 +01:00
2023-04-03 00:44:29 +02:00
for comp in components:
f.write(f' platypush/{doc_dir}/{comp}.rst\n')
2019-12-30 18:50:01 +01:00
2023-04-03 00:44:29 +02:00
_cleanup_removed_components_docs(docs_dir, components)
2019-12-30 18:50:01 +01:00
2023-04-03 00:44:29 +02:00
def _cleanup_removed_components_docs(docs_dir: str, components: Iterable[str]):
new_components = set(components)
existing_files = {
os.path.join(root, file)
for root, _, files in os.walk(docs_dir)
for file in files
if file.endswith('.rst')
}
2019-12-30 18:50:01 +01:00
2023-04-03 00:44:29 +02:00
files_to_remove = {
file
for file in existing_files
if os.path.basename(file).removesuffix('.rst') not in new_components
}
2019-12-30 18:50:01 +01:00
2023-04-03 00:44:29 +02:00
for file in files_to_remove:
print(f'Removing unlinked component {file}')
os.unlink(file)
2019-12-30 18:50:01 +01:00
2023-04-03 00:44:29 +02:00
def generate_plugins_doc():
_generate_components_doc(
index_name='plugins', package_name='plugins', components=get_all_plugins()
)
2019-12-30 18:50:01 +01:00
2023-04-03 00:44:29 +02:00
def generate_backends_doc():
_generate_components_doc(
index_name='backends',
package_name='backend',
components=get_all_backends(),
doc_dir='backend',
)
2023-04-03 00:44:29 +02:00
def generate_events_doc():
_generate_components_doc(
index_name='events',
package_name='message.event',
components=sorted(event for event in get_all_events().keys() if event),
)
2023-04-03 00:44:29 +02:00
def generate_responses_doc():
_generate_components_doc(
index_name='responses',
package_name='message.response',
components=sorted(
response for response in get_all_responses().keys() if response
),
)
2023-04-03 00:44:29 +02:00
def main():
generate_plugins_doc()
generate_backends_doc()
generate_events_doc()
generate_responses_doc()
2023-04-03 00:44:29 +02:00
if __name__ == '__main__':
main()
2019-12-30 10:16:55 +01:00
# vim:sw=4:ts=4:et: