Debian Codegraph @

addon/doxycommentview/doxycommentview.py

raw
1 #!/usr/bin/env python3
2 #
3 # python3 helper script to start a web server that can do life rendering of doxygen comments.
4 #
5 # Copyright (C) 1997-2024 by Dimitri van Heesch.
6 #
7 # Permission to use, copy, modify, and distribute this software and its
8 # documentation under the terms of the GNU General Public License is hereby
9 # granted. No representations are made about the suitability of this software
10 # for any purpose. It is provided "as is" without express or implied warranty.
11 # See the GNU General Public License for more details.
12 #
13 # Documents produced by Doxygen are derivative works derived from the
14 # input used in their production; they are not affected by this license.
15 #
16
17 import http.server
18 import socketserver
19 import json
20 import subprocess
21 import argparse
22 import signal
23 import threading
24 import html
25
26 def main():
27 # Set up argument parser
28 parser = argparse.ArgumentParser(description="Runs the doxygen comment viewer HTTP server.")
29 parser.add_argument('--port', type=int, default=8000, help='Port number to run the server on')
30 parser.add_argument('--doxygen', type=str, default='doxygen', help='Path to doxygen executable')
31 parser.add_argument('--doxyfile', type=str, default='Doxyfile', help='Path to Doxyfile to use')
32 args = parser.parse_args()
33
34 PORT = args.port
35 DOXYGEN = args.doxygen
36 DOXYFILE = args.doxyfile
37 VERSION_STR = subprocess.run([DOXYGEN, '-v'], capture_output=True, text=True, encoding="utf-8").stdout
38
39 class RequestHandler(http.server.SimpleHTTPRequestHandler):
40 def do_POST(self):
41 if self.path == '/process':
42 content_length = int(self.headers['Content-Length'])
43 post_data = self.rfile.read(content_length)
44 data = json.loads(post_data)
45 input_text = data['input']
46
47 # Run doxygen in single comment mode, reading from stdin and writing to stdout and stderr
48 result = subprocess.run([DOXYGEN, '-c', '-', DOXYFILE], \
49 input=input_text, capture_output=True, text=True, encoding="utf-8")
50
51 # Prepare the response
52 response = json.dumps({
53 'html_output': result.stdout,
54 'error_output': "<b>Doxygen version "+html.escape(VERSION_STR)+"</b><pre>"+html.escape(result.stderr)+"</pre>"
55 })
56
57 # Send the result to the requesting HTML page
58 self.send_response(200)
59 self.send_header('Content-type', 'text/html')
60 self.end_headers()
61 self.wfile.write(response.encode())
62
63 httpd = socketserver.TCPServer(("", PORT), RequestHandler)
64
65 def signal_handler(sig, frame):
66 print('Shutting down the web server...')
67 threading.Thread(target=httpd.shutdown).start()
68
69 signal.signal(signal.SIGINT, signal_handler)
70
71 print("Running web server on port", PORT)
72 httpd.serve_forever()
73
74 if __name__ == '__main__':
75 main()