Browse Source

Added web api that can build a document

master
Youen 2 years ago
parent
commit
cd57a96ac2
  1. 1
      .gitignore
  2. 32
      requirements.txt
  3. 12
      src/api/document.py
  4. 13
      src/app.py
  5. 43
      src/data/document.py
  6. 11
      src/web_utils/get_arg.py

1
.gitignore vendored

@ -1,2 +1,3 @@
/venv /venv
__pycache__ __pycache__
/data/doc

32
requirements.txt

@ -1,9 +1,41 @@
alabaster==0.7.12
Babel==2.10.3
certifi==2022.6.15
charset-normalizer==2.1.0
click==8.1.3 click==8.1.3
colorama==0.4.5 colorama==0.4.5
docutils==0.17.1
Flask==2.2.2 Flask==2.2.2
idna==3.3
imagesize==1.4.1
importlib-metadata==4.12.0 importlib-metadata==4.12.0
itsdangerous==2.1.2 itsdangerous==2.1.2
Jinja2==3.1.2 Jinja2==3.1.2
linkify-it-py==2.0.0
markdown-it-py==2.1.0
MarkupSafe==2.1.1 MarkupSafe==2.1.1
mdit-py-plugins==0.3.0
mdurl==0.1.1
myst-parser==0.18.0
packaging==21.3
pkg_resources==0.0.0
Pygments==2.12.0
pyparsing==3.0.9
pytz==2022.2
PyYAML==6.0
requests==2.28.1
snowballstemmer==2.2.0
Sphinx==5.1.1
sphinx-rtd-theme==1.0.0
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.0
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
sphinxcontrib-svg2pdfconverter==1.2.0
typing_extensions==4.3.0
uc-micro-py==1.0.1
urllib3==1.26.11
Werkzeug==2.2.2 Werkzeug==2.2.2
zipp==3.8.1 zipp==3.8.1

12
src/api/document.py

@ -1,8 +1,18 @@
from flask import Blueprint, request from flask import Blueprint, request
from markupsafe import escape
from web_utils.get_arg import get_arg
from data.document import Document
bp = Blueprint('document', __name__, url_prefix='/api/doc') bp = Blueprint('document', __name__, url_prefix='/api/doc')
@bp.route('/build') @bp.route('/build')
def build(): def build():
return '<p>Building '+request.args.get('doc')+'@'+request.args.get('branch')+' ...</p>' doc_name = get_arg('doc')
branch = get_arg('branch', 'master')
doc = Document(doc_name, branch)
output = doc.build()
return output.replace('\n', '<br/>')

13
src/app.py

@ -1,10 +1,15 @@
import os
from flask import Flask from flask import Flask
import data.document
def create_app(): def create_app():
app = Flask(__name__) app = Flask(__name__)
from api import document src_path = os.path.dirname(os.path.realpath(__file__))
app.register_blueprint(document.bp) data.document.set_document_root(os.path.realpath(src_path+'/../data/doc'))
return app from api import document
app.register_blueprint(document.bp)
return app

43
src/data/document.py

@ -0,0 +1,43 @@
import os
from subprocess import Popen, PIPE, STDOUT
document_root = None
class Document:
def __init__(self, doc_name, branch = 'master'):
self.doc_name = doc_name
self.branch = branch
#print(get_document_root())
#print(doc_name)
#print(branch)
doc_path = os.path.realpath(get_document_root()+'/'+doc_name+'/'+branch)
if not doc_path.startswith(get_document_root()):
raise Exception("Invalid document path for '"+doc_name+"'@'"+branch)
self.doc_path = doc_path
def build(self):
#venv_path = os.getenv('VIRTUAL_ENV')
cmd = "sphinx-build -M html \""+self.doc_path + "/source\" \""+self.doc_path+"/build\""
p = Popen(cmd, stdout = PIPE, stderr = STDOUT, shell = True)
outputStr = ""
for line in p.stdout:
outputStr += line.decode()
p.wait()
if p.returncode != 0:
raise Exception("Build failed ("+str(p.returncode)+")\n"+outputStr)
return outputStr
def set_document_root(dir):
global document_root
document_root = dir
def get_document_root():
global document_root
return document_root

11
src/web_utils/get_arg.py

@ -0,0 +1,11 @@
from flask import request
def get_arg(arg_name, default_value = None):
result = request.args.get(arg_name)
if result == None:
if default_value == None:
raise Exception("Missing query string parameter '"+arg_name+"'")
else:
return default_value
return result
Loading…
Cancel
Save