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
|
|
|
|
2021-09-16 17:53:40 +02:00
|
|
|
from platypush.backend import Backend
|
2019-12-30 10:16:55 +01:00
|
|
|
from platypush.context import get_plugin
|
2021-09-16 17:53:40 +02:00
|
|
|
from platypush.plugins import Plugin
|
|
|
|
from platypush.utils.manifest import get_manifests
|
2019-12-30 10:16:55 +01:00
|
|
|
|
|
|
|
|
2022-03-03 15:15: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():
|
2022-03-03 15:15:55 +01:00
|
|
|
return _get_inspect_plugin().get_all_events().output
|
2019-12-30 18:50:01 +01:00
|
|
|
|
|
|
|
|
2020-03-05 17:39:50 +01:00
|
|
|
def get_all_responses():
|
2022-03-03 15:15:55 +01:00
|
|
|
return _get_inspect_plugin().get_all_responses().output
|
2020-03-05 17:39:50 +01:00
|
|
|
|
|
|
|
|
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(
|
2023-03-26 22:55:22 +02:00
|
|
|
os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
'docs',
|
|
|
|
'source',
|
2023-04-03 00:44:29 +02:00
|
|
|
f'{index_name}.rst',
|
2023-03-26 22:55:22 +02:00
|
|
|
)
|
2023-04-03 00:44:29 +02:00
|
|
|
docs_dir = os.path.join(
|
2023-03-26 22:55:22 +02:00
|
|
|
os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
'docs',
|
|
|
|
'source',
|
|
|
|
'platypush',
|
2023-04-03 00:44:29 +02:00
|
|
|
doc_dir,
|
2023-03-26 22:55:22 +02:00
|
|
|
)
|
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:
|
2023-03-26 22:55:22 +02:00
|
|
|
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::
|
2023-03-26 22:55:22 +02:00
|
|
|
:maxdepth: 1
|
2023-04-03 00:44:29 +02:00
|
|
|
:caption: {index_name.title()}:
|
2019-12-30 10:16:55 +01:00
|
|
|
|
2023-03-26 22:55:22 +02: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-03-26 22:55:22 +02: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-03-26 22:55:22 +02:00
|
|
|
)
|
2020-03-05 17:39:50 +01:00
|
|
|
|
|
|
|
|
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),
|
|
|
|
)
|
2020-03-05 17:39:50 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
),
|
|
|
|
)
|
2020-03-05 17:39:50 +01:00
|
|
|
|
|
|
|
|
2023-04-03 00:44:29 +02:00
|
|
|
def main():
|
|
|
|
generate_plugins_doc()
|
|
|
|
generate_backends_doc()
|
|
|
|
generate_events_doc()
|
|
|
|
generate_responses_doc()
|
2020-03-05 17:39:50 +01:00
|
|
|
|
|
|
|
|
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:
|