Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose tuber.server.main() for user API #43

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions tuber/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import warnings
from .codecs import Codecs

__all__ = ["run"]
__all__ = ["run", "main"]


# request handling
Expand Down Expand Up @@ -370,20 +370,39 @@ def run(registry, json_module="json", port=80, webroot="/var/www/", max_age=3600
)


def main():
def load_registry(filename):
"""
Server entry point
Load a user registry from a user provided python file
"""

import importlib.util

modname = os.path.splitext(os.path.basename(filename))[0]
spec = importlib.util.spec_from_file_location(modname, filename)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)

return mod.registry


def main(registry=None):
"""
Server entry point.

If supplied, run the server with the given registry. Otherwise, use the
``--registry`` command-line argument to provide a path to a registry file.
"""

import argparse as ap

P = ap.ArgumentParser(description="Tuber server")
P.add_argument(
"-r",
"--registry",
default="/usr/share/tuberd/registry.py",
help="Location of registry Python code",
)
if registry is None:
P.add_argument(
"-r",
"--registry",
default="/usr/share/tuberd/registry.py",
help="Location of registry Python code",
)
P.add_argument(
"-j",
"--json",
Expand All @@ -409,9 +428,8 @@ def main():
# setup environment
os.environ["TUBER_SERVER"] = "1"

code = compile(open(args.registry, "r").read(), args.registry, "exec")
exec(code, globals())
args.registry = registry
# load registry
args.registry = registry if registry else load_registry(args.registry)

run(**vars(args))

Expand Down
Loading