Merge pull request #1962 from huge-success/cli-upgrade
Sanic CLI upgrade
This commit is contained in:
commit
5fbdcb62e4
|
@ -1,28 +1,83 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser, RawDescriptionHelpFormatter
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from typing import Any, Dict, Optional
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
|
from sanic import __version__
|
||||||
from sanic.app import Sanic
|
from sanic.app import Sanic
|
||||||
|
from sanic.config import BASE_LOGO
|
||||||
from sanic.log import logger
|
from sanic.log import logger
|
||||||
|
|
||||||
|
|
||||||
|
class SanicArgumentParser(ArgumentParser):
|
||||||
|
def add_bool_arguments(self, *args, **kwargs):
|
||||||
|
group = self.add_mutually_exclusive_group()
|
||||||
|
group.add_argument(*args, action="store_true", **kwargs)
|
||||||
|
kwargs["help"] = "no " + kwargs["help"]
|
||||||
|
group.add_argument(
|
||||||
|
"--no-" + args[0][2:], *args[1:], action="store_false", **kwargs
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = ArgumentParser(prog="sanic")
|
parser = SanicArgumentParser(
|
||||||
parser.add_argument("--host", dest="host", type=str, default="127.0.0.1")
|
prog="sanic",
|
||||||
parser.add_argument("--port", dest="port", type=int, default=8000)
|
description=BASE_LOGO,
|
||||||
parser.add_argument("--unix", dest="unix", type=str, default="")
|
formatter_class=RawDescriptionHelpFormatter,
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-H",
|
||||||
|
"--host",
|
||||||
|
dest="host",
|
||||||
|
type=str,
|
||||||
|
default="127.0.0.1",
|
||||||
|
help="host address [default 127.0.0.1]",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-p",
|
||||||
|
"--port",
|
||||||
|
dest="port",
|
||||||
|
type=int,
|
||||||
|
default=8000,
|
||||||
|
help="port to serve on [default 8000]",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-u",
|
||||||
|
"--unix",
|
||||||
|
dest="unix",
|
||||||
|
type=str,
|
||||||
|
default="",
|
||||||
|
help="location of unix socket",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--cert", dest="cert", type=str, help="location of certificate for SSL"
|
"--cert", dest="cert", type=str, help="location of certificate for SSL"
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--key", dest="key", type=str, help="location of keyfile for SSL."
|
"--key", dest="key", type=str, help="location of keyfile for SSL."
|
||||||
)
|
)
|
||||||
parser.add_argument("--workers", dest="workers", type=int, default=1)
|
parser.add_argument(
|
||||||
|
"-w",
|
||||||
|
"--workers",
|
||||||
|
dest="workers",
|
||||||
|
type=int,
|
||||||
|
default=1,
|
||||||
|
help="number of worker processes [default 1]",
|
||||||
|
)
|
||||||
parser.add_argument("--debug", dest="debug", action="store_true")
|
parser.add_argument("--debug", dest="debug", action="store_true")
|
||||||
parser.add_argument("module")
|
parser.add_bool_arguments(
|
||||||
|
"--access-logs", dest="access_log", help="display access logs"
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-v",
|
||||||
|
"--version",
|
||||||
|
action="version",
|
||||||
|
version=f"Sanic {__version__}",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"module", help="path to your Sanic app. Example: path.to.server:app"
|
||||||
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -30,6 +85,9 @@ def main():
|
||||||
if module_path not in sys.path:
|
if module_path not in sys.path:
|
||||||
sys.path.append(module_path)
|
sys.path.append(module_path)
|
||||||
|
|
||||||
|
if ":" in args.module:
|
||||||
|
module_name, app_name = args.module.rsplit(":", 1)
|
||||||
|
else:
|
||||||
module_parts = args.module.split(".")
|
module_parts = args.module.split(".")
|
||||||
module_name = ".".join(module_parts[:-1])
|
module_name = ".".join(module_parts[:-1])
|
||||||
app_name = module_parts[-1]
|
app_name = module_parts[-1]
|
||||||
|
@ -57,6 +115,7 @@ def main():
|
||||||
unix=args.unix,
|
unix=args.unix,
|
||||||
workers=args.workers,
|
workers=args.workers,
|
||||||
debug=args.debug,
|
debug=args.debug,
|
||||||
|
access_log=args.access_log,
|
||||||
ssl=ssl,
|
ssl=ssl,
|
||||||
)
|
)
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
|
|
|
@ -735,6 +735,7 @@ def test_static_blueprint_name(app: Sanic, static_file_directory, file_name):
|
||||||
_, response = app.test_client.get("/static/test.file/")
|
_, response = app.test_client.get("/static/test.file/")
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("file_name", ["test.file"])
|
@pytest.mark.parametrize("file_name", ["test.file"])
|
||||||
def test_static_blueprintp_mw(app: Sanic, static_file_directory, file_name):
|
def test_static_blueprintp_mw(app: Sanic, static_file_directory, file_name):
|
||||||
current_file = inspect.getfile(inspect.currentframe())
|
current_file = inspect.getfile(inspect.currentframe())
|
||||||
|
@ -745,7 +746,7 @@ def test_static_blueprintp_mw(app: Sanic, static_file_directory, file_name):
|
||||||
|
|
||||||
bp = Blueprint(name="test_mw", url_prefix="")
|
bp = Blueprint(name="test_mw", url_prefix="")
|
||||||
|
|
||||||
@bp.middleware('request')
|
@bp.middleware("request")
|
||||||
def bp_mw1(request):
|
def bp_mw1(request):
|
||||||
nonlocal triggered
|
nonlocal triggered
|
||||||
triggered = True
|
triggered = True
|
||||||
|
@ -754,7 +755,7 @@ def test_static_blueprintp_mw(app: Sanic, static_file_directory, file_name):
|
||||||
"/test.file",
|
"/test.file",
|
||||||
get_file_path(static_file_directory, file_name),
|
get_file_path(static_file_directory, file_name),
|
||||||
strict_slashes=True,
|
strict_slashes=True,
|
||||||
name="static"
|
name="static",
|
||||||
)
|
)
|
||||||
|
|
||||||
app.blueprint(bp)
|
app.blueprint(bp)
|
||||||
|
|
|
@ -20,7 +20,9 @@ def test_load_module_from_file_location(loaded_module_from_file_location):
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.dependency(depends=["test_load_module_from_file_location"])
|
@pytest.mark.dependency(depends=["test_load_module_from_file_location"])
|
||||||
def test_loaded_module_from_file_location_name(loaded_module_from_file_location,):
|
def test_loaded_module_from_file_location_name(
|
||||||
|
loaded_module_from_file_location,
|
||||||
|
):
|
||||||
name = loaded_module_from_file_location.__name__
|
name = loaded_module_from_file_location.__name__
|
||||||
if "C:\\" in name:
|
if "C:\\" in name:
|
||||||
name = name.split("\\")[-1]
|
name = name.split("\\")[-1]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user