Add a command line tool to generate OAS in a file
This commit is contained in:
45
aiohttp_pydantic/oas/cmd.py
Normal file
45
aiohttp_pydantic/oas/cmd.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import argparse
|
||||
import importlib
|
||||
import json
|
||||
|
||||
from .view import generate_oas
|
||||
|
||||
|
||||
def application_type(value):
|
||||
"""
|
||||
Return aiohttp application defined in the value.
|
||||
"""
|
||||
try:
|
||||
module_name, app_name = value.split(":")
|
||||
except ValueError:
|
||||
module_name, app_name = value, "app"
|
||||
|
||||
module = importlib.import_module(module_name)
|
||||
try:
|
||||
if app_name.endswith("()"):
|
||||
app_name = app_name.strip("()")
|
||||
factory_app = getattr(module, app_name)
|
||||
return factory_app()
|
||||
return getattr(module, app_name)
|
||||
|
||||
except AttributeError as error:
|
||||
raise argparse.ArgumentTypeError(error) from error
|
||||
|
||||
|
||||
def setup(parser: argparse.ArgumentParser):
|
||||
parser.add_argument(
|
||||
"apps",
|
||||
metavar="APP",
|
||||
type=application_type,
|
||||
nargs="*",
|
||||
help="The name of the module containing the asyncio.web.Application."
|
||||
" By default the variable named 'app' is loaded but you can define"
|
||||
" an other variable name ending the name of module with : characters"
|
||||
" and the name of variable. Example: my_package.my_module:my_app",
|
||||
)
|
||||
|
||||
parser.set_defaults(func=show_oas)
|
||||
|
||||
|
||||
def show_oas(args: argparse.Namespace):
|
||||
print(json.dumps(generate_oas(args.apps), sort_keys=True, indent=4))
|
||||
Reference in New Issue
Block a user