Coverage for .tox/coverage/lib/python3.13/site-packages/wuttaweb/cli/webapp.py: 100%

27 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-20 15:24 -0500

1# -*- coding: utf-8; -*- 

2################################################################################ 

3# 

4# wuttaweb -- Web App for Wutta Framework 

5# Copyright © 2024-2026 Lance Edgar 

6# 

7# This file is part of Wutta Framework. 

8# 

9# Wutta Framework is free software: you can redistribute it and/or modify it 

10# under the terms of the GNU General Public License as published by the Free 

11# Software Foundation, either version 3 of the License, or (at your option) any 

12# later version. 

13# 

14# Wutta Framework is distributed in the hope that it will be useful, but 

15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 

16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 

17# more details. 

18# 

19# You should have received a copy of the GNU General Public License along with 

20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>. 

21# 

22################################################################################ 

23""" 

24See also: :ref:`wutta-webapp` 

25""" 

26 

27import os 

28import sys 

29 

30try: 

31 from typing import Annotated 

32except ImportError: # pragma: no cover 

33 from typing_extensions import Annotated 

34 

35import typer 

36from pyramid.scripts import pserve 

37 

38from wuttjamaican.cli import wutta_typer 

39 

40 

41@wutta_typer.command() 

42def webapp( # pylint: disable=unused-argument 

43 ctx: typer.Context, 

44 auto_reload: Annotated[ 

45 bool, 

46 typer.Option("--reload", "-r", help="Auto-reload web app when files change."), 

47 ] = False, 

48): 

49 """ 

50 Run the configured web app 

51 """ 

52 config = ctx.parent.wutta_config 

53 

54 # we'll need config file(s) to specify for web app 

55 if not config.files_read: 

56 sys.stderr.write("no config files found!\n") 

57 sys.exit(1) 

58 

59 runner = config.get(f"{config.appname}.web.app.runner", default="pserve") 

60 if runner == "pserve": 

61 

62 # run pserve 

63 argv = ["pserve", f"file+ini:{config.files_read[0]}"] 

64 if ctx.params["auto_reload"]: 

65 argv.append("--reload") 

66 pserve.main(argv=argv) 

67 

68 elif runner == "uvicorn": 

69 

70 import uvicorn # pylint: disable=import-error,import-outside-toplevel 

71 

72 # need service details from config 

73 spec = config.require(f"{config.appname}.web.app.spec") 

74 kw = { 

75 "host": config.get(f"{config.appname}.web.app.host", default="127.0.0.1"), 

76 "port": config.get_int(f"{config.appname}.web.app.port", default=8000), 

77 "reload": ctx.params["auto_reload"], 

78 "reload_dirs": config.get_list(f"{config.appname}.web.app.reload_dirs"), 

79 "factory": config.get_bool( 

80 f"{config.appname}.web.app.factory", default=False 

81 ), 

82 "interface": config.get( 

83 f"{config.appname}.web.app.interface", default="auto" 

84 ), 

85 "root_path": config.get(f"{config.appname}.web.app.root_path", default=""), 

86 } 

87 

88 # also must inject our config files to env, since there is no 

89 # other way to specify when running via uvicorn 

90 os.environ["WUTTA_CONFIG_FILES"] = os.pathsep.join(config.files_read) 

91 

92 # run uvicorn 

93 uvicorn.run(spec, **kw) 

94 

95 else: 

96 sys.stderr.write(f"unknown web app runner: {runner}\n") 

97 sys.exit(2)