Simpler CLI targets (#2700)

Co-authored-by: L. Kärkkäinen <98187+Tronic@users.noreply.github.com>
This commit is contained in:
Adam Hopkins
2023-03-21 20:50:25 +02:00
committed by GitHub
parent 932088e37e
commit 6e1c787e5d
6 changed files with 81 additions and 83 deletions

View File

@@ -24,17 +24,22 @@ class SanicCLI:
{get_logo(True)}
To start running a Sanic application, provide a path to the module, where
app is a Sanic() instance:
app is a Sanic() instance in the global scope:
$ sanic path.to.server:app
If the Sanic instance variable is called 'app', you can leave off the last
part, and only provide a path to the module where the instance is:
$ sanic path.to.server
Or, a path to a callable that returns a Sanic() instance:
$ sanic path.to.factory:create_app --factory
$ sanic path.to.factory:create_app
Or, a path to a directory to run as a simple HTTP server:
$ sanic ./path/to/static --simple
$ sanic ./path/to/static
""",
prefix=" ",
)
@@ -95,7 +100,7 @@ Or, a path to a directory to run as a simple HTTP server:
self.args = self.parser.parse_args(args=parse_args)
self._precheck()
app_loader = AppLoader(
self.args.module, self.args.factory, self.args.simple, self.args
self.args.target, self.args.factory, self.args.simple, self.args
)
if self.args.inspect or self.args.inspect_raw or self.args.trigger:
@@ -120,9 +125,9 @@ Or, a path to a directory to run as a simple HTTP server:
def _inspector_legacy(self, app_loader: AppLoader):
host = port = None
module = cast(str, self.args.module)
if ":" in module:
maybe_host, maybe_port = module.rsplit(":", 1)
target = cast(str, self.args.target)
if ":" in target:
maybe_host, maybe_port = target.rsplit(":", 1)
if maybe_port.isnumeric():
host, port = maybe_host, int(maybe_port)
if not host:

View File

@@ -57,11 +57,15 @@ class GeneralGroup(Group):
)
self.container.add_argument(
"module",
"target",
help=(
"Path to your Sanic app. Example: path.to.server:app\n"
"If running a Simple Server, path to directory to serve. "
"Example: ./\n"
"Path to your Sanic app instance.\n"
"\tExample: path.to.server:app\n"
"If running a Simple Server, path to directory to serve.\n"
"\tExample: ./\n"
"Additionally, this can be a path to a factory function\n"
"that returns a Sanic app instance.\n"
"\tExample: path.to.server:create_app\n"
),
)

View File

@@ -3,7 +3,9 @@ from __future__ import annotations
import os
import sys
from contextlib import suppress
from importlib import import_module
from inspect import isfunction
from pathlib import Path
from ssl import SSLContext
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union, cast
@@ -15,6 +17,8 @@ from sanic.http.tls.creators import MkcertCreator, TrustmeCreator
if TYPE_CHECKING:
from sanic import Sanic as SanicApp
DEFAULT_APP_NAME = "app"
class AppLoader:
def __init__(
@@ -36,7 +40,11 @@ class AppLoader:
if module_input:
delimiter = ":" if ":" in module_input else "."
if module_input.count(delimiter):
if (
delimiter in module_input
and "\\" not in module_input
and "/" not in module_input
):
module_name, app_name = module_input.rsplit(delimiter, 1)
self.module_name = module_name
self.app_name = app_name
@@ -55,21 +63,30 @@ class AppLoader:
from sanic.app import Sanic
from sanic.simple import create_simple_server
if self.as_simple:
path = Path(self.module_input)
app = create_simple_server(path)
maybe_path = Path(self.module_input)
if self.as_simple or (
maybe_path.is_dir()
and ("\\" in self.module_input or "/" in self.module_input)
):
app = create_simple_server(maybe_path)
else:
if self.module_name == "" and os.path.isdir(self.module_input):
raise ValueError(
"App not found.\n"
" Please use --simple if you are passing a "
"directory to sanic.\n"
f" eg. sanic {self.module_input} --simple"
)
implied_app_name = False
if not self.module_name and not self.app_name:
self.module_name = self.module_input
self.app_name = DEFAULT_APP_NAME
implied_app_name = True
module = import_module(self.module_name)
app = getattr(module, self.app_name, None)
if self.as_factory:
if not app and implied_app_name:
raise ValueError(
"Looks like you only supplied a module name. Sanic "
"tried to locate an application instance named "
f"{self.module_name}:app, but was unable to locate "
"an application instance. Please provide a path "
"to a global instance of Sanic(), or a callable that "
"will return a Sanic() application instance."
)
if self.as_factory or isfunction(app):
try:
app = app(self.args)
except TypeError:
@@ -80,21 +97,18 @@ class AppLoader:
if (
not isinstance(app, Sanic)
and self.args
and hasattr(self.args, "module")
and hasattr(self.args, "target")
):
if callable(app):
solution = f"sanic {self.args.module} --factory"
raise ValueError(
"Module is not a Sanic app, it is a "
f"{app_type_name}\n"
" If this callable returns a "
f"Sanic instance try: \n{solution}"
with suppress(ModuleNotFoundError):
maybe_module = import_module(self.module_input)
app = getattr(maybe_module, "app", None)
if not app:
message = (
"Module is not a Sanic app, "
f"it is a {app_type_name}\n"
f" Perhaps you meant {self.args.target}:app?"
)
raise ValueError(
f"Module is not a Sanic app, it is a {app_type_name}\n"
f" Perhaps you meant {self.args.module}:app?"
)
raise ValueError(message)
return app