Coverage for gws-app/gws/base/action/cli.py: 0%
29 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-05 13:35 +0200
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-05 13:35 +0200
1"""CLI commands to invoke and profile web actions."""
3import cProfile
5import gws
6import gws.base.action
7import gws.base.auth
8import gws.base.web
9import gws.lib.jsonx
10import gws.lib.vendor.slon
13class InvokeRequest(gws.Request):
14 """Parameters of the ``actionInvoke`` and ``actionProfile`` CLI commands."""
16 cmd: str
17 """API command name, for example ``mapDescribeLayer``."""
18 params: str
19 """Command parameters in SLON format."""
22class Object(gws.Node):
23 """CLI commands for web actions.
25 Invokes an API command from the command line and prints the response, or runs
26 it with the profiler and saves the statistics.
27 """
29 @gws.ext.command.cli('actionInvoke')
30 def invoke(self, p: InvokeRequest):
31 """Invoke an API command and print the response as JSON."""
33 res = self._invoke(p)
34 print(gws.lib.jsonx.to_pretty_string(res))
36 @gws.ext.command.cli('actionProfile')
37 def profile(self, p: InvokeRequest):
38 """Run an API command with the profiler and save the statistics."""
40 filename = f'{gws.c.VAR_DIR}/{p.cmd}.pstats'
42 cProfile.runctx(
43 'self._invoke(p)',
44 {},
45 locals(),
46 filename=filename
47 )
48 print(f'profile saved to {filename!r}')
50 def _invoke(self, p: InvokeRequest):
51 """Load the configuration, run an API command with an empty web request and return its response."""
52 environ = {}
53 root = gws.load_root()
54 req = gws.base.web.wsgi.Requester(root, environ, root.app.webMgr.site)
56 fn, request = root.app.actionMgr.prepare_action(
57 gws.CommandCategory.api,
58 p.cmd,
59 gws.lib.vendor.slon.parse(p.params),
60 '',
61 req.user,
62 )
63 try:
64 return fn(req, request)
65 except:
66 gws.log.exception()