-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add aiohttp.web command-line interface to serve apps #740
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0c9b118
Add aiohttp.web command-line interface to serve apps.
jashandeep-sohi 97d8ec0
Fix cyclic importing errors
jashandeep-sohi 3fbf106
Move web_main.py content to web.py
jashandeep-sohi de3cdc9
Use 'module:function' syntax for entry-function
jashandeep-sohi 74639e0
Use flags -H & -P for --hostname & --port, respectively
jashandeep-sohi 0251d3d
Update CLI example to use nested argparse
jashandeep-sohi 699b1ef
Add tests for aiohttp.web CLI
jashandeep-sohi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example should have a code for demonstrating how to parse
args
again viaargparse
.I mean
parse.parse_args(args)
-- not every user know about the ability.I want to encourage argparse usage, not ugly code like
if args[0] == '--param' and args[1] == 'value'
.