<commit_before>#! /usr/bin/env python3

"""
sphinxview-serve - like restview, but for Sphinx projects

Usage:
    sphinxview.py [options]
    sphinxview.py -h | -- help

Options:
    -c, --clean               If set, a clean build is created
    -r, --rootdir=<path>      Root documentation directory [default: .]
    -s, --sourcedir=<path>    Directory containing the source files [default: .]
    -b, --builddir=<path>     Directory to store build [default: _build]
    -m, --masterdoc=<file>    Master document (contains root toctree directive),
                              without suffix [default: index]
    --suffix=<suffix>         Source file suffix [default: .rst]
    -i, --interface=<ip>      Interface to serve build on [default: 127.0.0.1]
    -p, --port=<port>         Port to serve build on [default: 16001]
"""

# Moving parts:
# - the server
# - serves get request with a page, head request receives inner html, uses
# regex to get the timestamp of last_updated and compares that to mtime of
# corresponding source file, if source file is newer, build sphinx project
# and force reload of page
# - the extension
# - add javascript that gets inner html from footer div and sends it to server
# - the cli
# - call make (or sphinx build) with the parameter -D html_last_updated_fmt="%s"
# make html SPHINXOPTS='-D html_last_updated_fmt="%a"'
# - spawn server with any arguments that it needs
# - launch webbrowser with master_doc in build as first target

# TODO: Subclass HTTP server, new class gets source_dir and build_dir, build_dir used for serving and source_dir used by Handler
# TODO: Handler also needs access to building the project - perhaps this is a job for the server as well?
# TODO: Fix issue that makes server hang when trying to havigate to new page (looks like eternal loop)
# TODO: What is rootdir argument used for?
# TODO: builddir and sourcedir should be relative to rootdir

from docopt import docopt
from os import chdir, path
from time import sleep
from urllib.parse import parse_qs
from http.server import HTTPServer, SimpleHTTPRequestHandler
from os import stat
from subprocess import call
from re import search
from shutil import rmtree
from socketserver import ThreadingMixIn

SPHINXVIEW_OUTPUT_DIR = 'sphinxview'
LAST_UPDATED_FMT = 'html_last_updated_fmt=%% %s %%'
SPHINXVIEW_ENABLED_TRUE = 'sphinxview_enabled=1'
# TODO: is SOURCE_DIR ever used? For what?
SOURCE_DIR = ""


class SphinxViewRequestHandler(SimpleHTTPRequestHandler):

    def __init__(self, request, client_address, server):<commit_msg>add TODO<commit_after>#! /usr/bin/env python3

"""
sphinxview-serve - like restview, but for Sphinx projects

Usage:
    sphinxview.py [options]
    sphinxview.py -h | -- help

Options:
    -c, --clean               If set, a clean build is created
    -r, --rootdir=<path>      Root documentation directory [default: .]
    -s, --sourcedir=<path>    Directory containing the source files [default: .]
    -b, --builddir=<path>     Directory to store build [default: _build]
    -m, --masterdoc=<file>    Master document (contains root toctree directive),
                              without suffix [default: index]
    --suffix=<suffix>         Source file suffix [default: .rst]
    -i, --interface=<ip>      Interface to serve build on [default: 127.0.0.1]
    -p, --port=<port>         Port to serve build on [default: 16001]
"""

# Moving parts:
# - the server
# - serves get request with a page, head request receives inner html, uses
# regex to get the timestamp of last_updated and compares that to mtime of
# corresponding source file, if source file is newer, build sphinx project
# and force reload of page
# - the extension
# - add javascript that gets inner html from footer div and sends it to server
# - the cli
# - call make (or sphinx build) with the parameter -D html_last_updated_fmt="%s"
# make html SPHINXOPTS='-D html_last_updated_fmt="%a"'
# - spawn server with any arguments that it needs
# - launch webbrowser with master_doc in build as first target

# TODO: Subclass HTTP server, new class gets source_dir and build_dir, build_dir used for serving and source_dir used by Handler
# TODO: Handler also needs access to building the project - perhaps this is a job for the server as well?
# TODO: Fix issue that makes server hang when trying to havigate to new page (looks like eternal loop)
# TODO: What is rootdir argument used for?
# TODO: builddir and sourcedir should be relative to rootdir
# TODO: user should be able to specify where to point browser at, instead of using masterdoc (default to index)

from docopt import docopt
from os import chdir, path
from time import sleep
from urllib.parse import parse_qs
from http.server import HTTPServer, SimpleHTTPRequestHandler
from os import stat
from subprocess import call
from re import search
from shutil import rmtree
from socketserver import ThreadingMixIn

SPHINXVIEW_OUTPUT_DIR = 'sphinxview'
LAST_UPDATED_FMT = 'html_last_updated_fmt=%% %s %%'
SPHINXVIEW_ENABLED_TRUE = 'sphinxview_enabled=1'
# TODO: is SOURCE_DIR ever used? For what?
SOURCE_DIR = ""


class SphinxViewRequestHandler(SimpleHTTPRequestHandler):

    def __init__(self, request, client_address, server):
{'fraction_non_alphanumeric': 0.07896253602305475, 'fraction_numerical': 0.004995196926032661, 'mean_word_length': 3.7241379310344827, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '191453', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1505, 'n_words': 647}