-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2676 from gresm/docs-serve
Serving documentation with a simple server.
- Loading branch information
Showing
10 changed files
with
139 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from pathlib import Path | ||
|
||
PKG_DIR: Path | ||
|
||
|
||
def has_local_docs() -> bool: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
def main(): ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from http.server import SimpleHTTPRequestHandler | ||
|
||
|
||
class DocsHandler(SimpleHTTPRequestHandler): ... | ||
|
||
|
||
def serve(address: str, port: str): ... | ||
|
||
|
||
def main(): ... | ||
|
||
|
||
TARGET: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
def open_docs(): ... | ||
|
||
|
||
def main(): ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# python -m pygame.docs | ||
|
||
from pathlib import Path | ||
|
||
|
||
PKG_DIR = Path(__file__).absolute().parent / "generated" | ||
|
||
|
||
# for test suite to confirm pygame-ce built with local docs | ||
def has_local_docs(): | ||
return PKG_DIR.exists() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,15 @@ | ||
# python -m pygame.docs | ||
import platform | ||
|
||
import os | ||
import webbrowser | ||
from urllib.parse import quote, urlunparse | ||
from pygame.docs.serve import main as serve | ||
from pygame.docs.static import main as static | ||
|
||
|
||
def _iterpath(path): | ||
path, last = os.path.split(path) | ||
if last: | ||
yield from _iterpath(path) | ||
yield last | ||
|
||
|
||
# for test suite to confirm pygame-ce built with local docs | ||
def has_local_docs(): | ||
pkg_dir = os.path.dirname(os.path.abspath(__file__)) | ||
main_page = os.path.join(pkg_dir, "generated", "index.html") | ||
return os.path.exists(main_page) | ||
|
||
|
||
def open_docs(): | ||
pkg_dir = os.path.dirname(os.path.abspath(__file__)) | ||
main_page = os.path.join(pkg_dir, "generated", "index.html") | ||
if os.path.exists(main_page): | ||
url_path = quote("/".join(_iterpath(main_page))) | ||
drive, rest = os.path.splitdrive(__file__) | ||
if drive: | ||
url_path = f"{drive}/{url_path}" | ||
url = urlunparse(("file", "", url_path, "", "", "")) | ||
def main(): | ||
if platform.system() == "Linux": | ||
serve() | ||
else: | ||
url = "https://pyga.me/docs/" | ||
webbrowser.open(url) | ||
static() | ||
|
||
|
||
if __name__ == "__main__": | ||
open_docs() | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# python -m pygame.docs.serve | ||
|
||
import sys | ||
import webbrowser | ||
from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler | ||
|
||
from pygame.docs import PKG_DIR, has_local_docs | ||
|
||
|
||
class DocsHandler(SimpleHTTPRequestHandler): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs, directory=str(PKG_DIR)) | ||
|
||
|
||
def serve(address: str, port: int): | ||
with ThreadingHTTPServer((address, port), DocsHandler) as httpd: | ||
try: | ||
httpd.serve_forever() | ||
except KeyboardInterrupt: | ||
print("\nKeyboard interrupt received, exiting.") | ||
sys.exit(0) | ||
|
||
|
||
TARGET = "localhost" | ||
|
||
|
||
def main(): | ||
print("Running a simple server to serve documentation.") | ||
print( | ||
"If you want to just open documentation files in the browser, " | ||
"run 'python -m pygame.docs.static'." | ||
) | ||
if not has_local_docs(): | ||
print("ERROR: no local documentation found, cannot serve anything, exiting...") | ||
sys.exit(1) | ||
print("WARNING: this is not for production use!") | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"port", | ||
action="store", | ||
default=8000, | ||
type=int, | ||
nargs="?", | ||
help="specify alternate port (default: 8000)", | ||
) | ||
parser.add_argument( | ||
"--no-browser", | ||
"-n", | ||
action="store_false", | ||
default=True, | ||
dest="open_browser", | ||
help="whether to open a browser tab", | ||
) | ||
|
||
parsed_args = parser.parse_args() | ||
|
||
print(f"Serving on: http://{TARGET}:{parsed_args.port}") | ||
|
||
if parsed_args.open_browser: | ||
webbrowser.open(f"http://{TARGET}:{parsed_args.port}") | ||
|
||
serve(TARGET, parsed_args.port) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import webbrowser | ||
|
||
from pygame.docs import PKG_DIR | ||
|
||
|
||
def open_docs(): | ||
main_page = PKG_DIR / "index.html" | ||
if main_page.exists(): | ||
url = main_page.as_uri() | ||
else: | ||
url = "https://pyga.me/docs/" | ||
webbrowser.open(url) | ||
|
||
|
||
def main(): | ||
print("Opening local documentation files in the browser.") | ||
print( | ||
"If you want to run a simple server instead, run 'python -m pygame.docs.serve'." | ||
) | ||
open_docs() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters