-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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 #740 from jashandeep-sohi/exec-web
Add aiohttp.web command-line interface to serve apps
- v4.0.0a1
- v4.0.0a0
- v3.11.11
- v3.11.10
- v3.11.9
- v3.11.8
- v3.11.7
- v3.11.6
- v3.11.5
- v3.11.4
- v3.11.3
- v3.11.2
- v3.11.1
- v3.11.0
- v3.11.0rc2
- v3.11.0rc1
- v3.11.0rc0
- v3.11.0b5
- v3.11.0b4
- v3.11.0b3
- v3.11.0b2
- v3.11.0b1
- v3.11.0b0
- v3.10.11
- v3.10.11rc0
- v3.10.10
- v3.10.9
- v3.10.8
- v3.10.7
- v3.10.6
- v3.10.6rc2
- v3.10.6rc1
- v3.10.6rc0
- v3.10.5
- v3.10.4
- v3.10.3
- v3.10.2
- v3.10.1
- v3.10.0
- v3.10.0rc0
- v3.10.0b1
- v3.10.0b0
- v3.9.5
- v3.9.4
- v3.9.4rc0
- v3.9.3
- v3.9.2
- v3.9.1
- v3.9.0
- v3.9.0rc0
- v3.9.0b1
- v3.9.0b0
- v3.8.6
- v3.8.5
- v3.8.4
- v3.8.3
- v3.8.2
- v3.8.2a0
- v3.8.1
- v3.8.0
- v3.8.0b0
- v3.8.0a7
- v3.8.0a6
- v3.8.0a5
- v3.8.0a4
- v3.8.0a3
- v3.8.0a2
- v3.8.0a1
- v3.8.0a0
- v3.7.4
- v3.7.4.post0
- v3.7.3
- v3.7.2
- v3.7.1
- v3.7.0
- v3.7.0b1
- v3.7.0b0
- v3.6.3
- v3.6.2
- v3.6.2a2
- v3.6.2a1
- v3.6.2a0
- v3.6.1
- v3.6.1b4
- v3.6.1b3
- v3.6.0
- v3.6.0b0
- v3.6.0a12
- v3.6.0a11
- v3.6.0a10
- v3.6.0a9
- v3.6.0a8
- v3.6.0a7
- v3.6.0a6
- v3.6.0a5
- v3.6.0a4
- v3.6.0a3
- v3.6.0a2
- v3.6.0a1
- v3.6.0a0
- v3.5.4
- v3.5.3
- v3.5.2
- v3.5.1
- v3.5.0
- v3.5.0b3
- v3.5.0b2
- v3.5.0b1
- v3.5.0a1
- v3.4.4
- v3.4.3
- v3.4.2
- v3.4.1
- v3.4.0
- v3.4.0b2
- v3.4.0b1
- v3.4.0a3
- v3.4.0a2
- v3.4.0a1
- v3.4.0a0
- v3.3.2
- v3.3.2a0
- v3.3.1
- v3.3.1b1
- v3.3.0
- v3.2.1
- v3.2.0
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0b4
- v3.0.0b3
- v3.0.0b2
- v3.0.0b1
- v3.0.0b0
- v2.3.10
- v2.3.9
- v2.3.8
- v2.3.7
- v2.3.6
- v2.3.5
- v2.3.4
- v2.3.3
- v2.3.2
- v2.3.2b3
- v2.3.2b2
- v2.3.2b1
- v2.3.1
- v2.3.1a1
- v2.3.0
- v2.3.0a4
- v2.3.0a3
- v2.3.0a2
- v2.3.0a1
- v2.2.5
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.0
- v1.2.0
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.5
- v1.0.4
- v1.0.2
- v1.0.1
- v1.0.0
- v0.22.5
- v0.22.4
- v0.22.3
- v0.22.2
- v0.22.1
- v0.22.0
- v0.22.0b6
- v0.22.0b5
- v0.22.0b4
- v0.22.0b3
- v0.22.0b2
- v0.22.0b1
- v0.22.0b0
- v0.21.6
- v0.21.5
- v0.21.4
- v0.21.3
- v0.21.2
- v0.21.1
- v0.21.0
- 4v0.21.6
- 3.3.0a0
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.3-1
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0rc1
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
Showing
4 changed files
with
252 additions
and
6 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
""" | ||
Example of serving an Application using the `aiohttp.web` CLI. | ||
Serve this app using:: | ||
$ python -m aiohttp.web -H localhost -P 8080 --repeat 10 web_app.init \ | ||
> "Hello World" | ||
Here ``--repeat`` & ``"Hello World"`` are application specific command-line | ||
arguments. `aiohttp.web` only parses & consumes the command-line arguments it | ||
needs (i.e. ``-H``, ``-P`` & ``entry-func``) and passes on any additional | ||
arguments to the `web_app.init` function for processing. | ||
""" | ||
|
||
from aiohttp.web import Application, Response | ||
from argparse import ArgumentParser | ||
|
||
|
||
def display_message(req): | ||
args = req.app["args"] | ||
text = "\n".join([args.message] * args.repeat) | ||
return Response(text=text) | ||
|
||
|
||
def init(args): | ||
arg_parser = ArgumentParser( | ||
prog="aiohttp.web ...", description="Application CLI", add_help=False | ||
) | ||
|
||
# Positional argument | ||
arg_parser.add_argument( | ||
"message", | ||
help="message to print" | ||
) | ||
|
||
# Optional argument | ||
arg_parser.add_argument( | ||
"--repeat", | ||
help="number of times to repeat message", type=int, default="1" | ||
) | ||
|
||
# Avoid conflict with -h from `aiohttp.web` CLI parser | ||
arg_parser.add_argument( | ||
"--app-help", | ||
help="show this message and exit", action="help" | ||
) | ||
|
||
parsed_args = arg_parser.parse_args(args) | ||
|
||
app = Application() | ||
app["args"] = parsed_args | ||
app.router.add_route('GET', '/', display_message) | ||
|
||
return app |
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,121 @@ | ||
import pytest | ||
|
||
|
||
from aiohttp import web | ||
from unittest import mock | ||
|
||
|
||
@mock.patch("aiohttp.web.ArgumentParser.error", side_effect=SystemExit) | ||
def test_entry_func_empty(error): | ||
argv = [""] | ||
|
||
with pytest.raises(SystemExit): | ||
web.main(argv) | ||
|
||
error.assert_called_with( | ||
"'entry-func' not in 'module:function' syntax" | ||
) | ||
|
||
|
||
@mock.patch("aiohttp.web.ArgumentParser.error", side_effect=SystemExit) | ||
def test_entry_func_only_module(error): | ||
argv = ["test"] | ||
|
||
with pytest.raises(SystemExit): | ||
web.main(argv) | ||
|
||
error.assert_called_with( | ||
"'entry-func' not in 'module:function' syntax" | ||
) | ||
|
||
|
||
@mock.patch("aiohttp.web.ArgumentParser.error", side_effect=SystemExit) | ||
def test_entry_func_only_function(error): | ||
argv = [":test"] | ||
|
||
with pytest.raises(SystemExit): | ||
web.main(argv) | ||
|
||
error.assert_called_with( | ||
"'entry-func' not in 'module:function' syntax" | ||
) | ||
|
||
|
||
@mock.patch("aiohttp.web.ArgumentParser.error", side_effect=SystemExit) | ||
def test_entry_func_only_seperator(error): | ||
argv = [":"] | ||
|
||
with pytest.raises(SystemExit): | ||
web.main(argv) | ||
|
||
error.assert_called_with( | ||
"'entry-func' not in 'module:function' syntax" | ||
) | ||
|
||
|
||
@mock.patch("aiohttp.web.ArgumentParser.error", side_effect=SystemExit) | ||
def test_entry_func_relative_module(error): | ||
argv = [".a.b:c"] | ||
|
||
with pytest.raises(SystemExit): | ||
web.main(argv) | ||
|
||
error.assert_called_with("relative module names not supported") | ||
|
||
|
||
@mock.patch("aiohttp.web.import_module", side_effect=ImportError) | ||
@mock.patch("aiohttp.web.ArgumentParser.error", side_effect=SystemExit) | ||
def test_entry_func_non_existent_module(error, import_module): | ||
argv = ["alpha.beta:func"] | ||
|
||
with pytest.raises(SystemExit): | ||
web.main(argv) | ||
|
||
error.assert_called_with("module %r not found" % "alpha.beta") | ||
|
||
|
||
@mock.patch("aiohttp.web.import_module") | ||
@mock.patch("aiohttp.web.ArgumentParser.error", side_effect=SystemExit) | ||
def test_entry_func_non_existent_attribute(error, import_module): | ||
argv = ["alpha.beta:func"] | ||
module = import_module("alpha.beta") | ||
del module.func | ||
|
||
with pytest.raises(SystemExit): | ||
web.main(argv) | ||
|
||
error.assert_called_with( | ||
"module %r has no attribute %r" % ("alpha.beta", "func") | ||
) | ||
|
||
|
||
@mock.patch("aiohttp.web.run_app") | ||
@mock.patch("aiohttp.web.import_module") | ||
def test_entry_func_call(import_module, run_app): | ||
argv = ("-H testhost -P 6666 --extra-optional-eins alpha.beta:func " | ||
"--extra-optional-zwei extra positional args").split() | ||
module = import_module("alpha.beta") | ||
|
||
with pytest.raises(SystemExit): | ||
web.main(argv) | ||
|
||
module.func.assert_called_with( | ||
("--extra-optional-eins --extra-optional-zwei extra positional " | ||
"args").split() | ||
) | ||
|
||
|
||
@mock.patch("aiohttp.web.run_app") | ||
@mock.patch("aiohttp.web.import_module") | ||
@mock.patch("aiohttp.web.ArgumentParser.exit", side_effect=SystemExit) | ||
def test_running_application(exit, import_module, run_app): | ||
argv = ("-H testhost -P 6666 --extra-optional-eins alpha.beta:func " | ||
"--extra-optional-zwei extra positional args").split() | ||
module = import_module("alpha.beta") | ||
app = module.func() | ||
|
||
with pytest.raises(SystemExit): | ||
web.main(argv) | ||
|
||
run_app.assert_called_with(app, host="testhost", port=6666) | ||
exit.assert_called_with(message="Stopped\n") |