-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathhook.py
More file actions
71 lines (51 loc) · 1.99 KB
/
hook.py
File metadata and controls
71 lines (51 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import asyncio
import logging
import pathlib
import concurrent.futures
from aiohttp import web
from app.utility.base_world import BaseWorld
name = 'fieldmanual'
description = 'Holds and serves Caldera documentation'
address = f'/plugin/{name}/gui'
access = BaseWorld.Access.APP
plugin_root = pathlib.Path('plugins') / name
plugin_root = plugin_root.absolute()
sphinx_docs_root = plugin_root / 'sphinx-docs'
sphinx_build_dir = sphinx_docs_root / '_build'
html_docs_root = sphinx_build_dir / 'html'
logger = logging.getLogger('fieldmanual')
async def landing(_):
return web.FileResponse(plugin_root / 'static' / 'opener.html')
def _run_sphinx_build():
import sys
import io
import sphinx.cmd.build
out = io.StringIO()
err = io.StringIO()
sys.stdout = out
sys.stderr = err
argv = ['-M', 'html', str(sphinx_docs_root), str(sphinx_build_dir)]
sphinx.cmd.build.main(argv)
out.seek(0)
err.seek(0)
return out.read(), err.read()
async def build_docs(loop=None):
loop = loop or asyncio.get_event_loop()
with concurrent.futures.ProcessPoolExecutor() as pool:
try:
out, err = await loop.run_in_executor(pool, _run_sphinx_build)
except Exception:
logger.warning('Encountered problem while building documentation.', exc_info=True)
if 'build succeeded' in out and err:
logger.info(f'Docs built successfully with the following warnings\n{err}')
elif 'build succeeded' in out:
logger.info('Docs built successfully.')
else:
logger.warning(f'Unable to build docs:\n{err}')
async def enable(services, loop=None):
loop = loop if loop else asyncio.get_event_loop()
html_docs_root.mkdir(parents=True, exist_ok=True)
loop.create_task(build_docs(loop=loop))
app_svc = services.get('app_svc')
app_svc.application.router.add_route('GET', address, landing)
app_svc.application.router.add_static('/docs/', str(html_docs_root.resolve()), append_version=True)