Coverage for gws-app/gws/base/printer/action.py: 0%
43 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-24 12:46 +0200
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-24 12:46 +0200
1"""Provides the printing API."""
3from typing import Optional, cast
5import gws
6import gws.base.action
7import gws.config
8import gws.lib.jsonx
9import gws.lib.osx
10import gws.lib.mime
12gws.ext.new.action('printer')
15class Config(gws.base.action.Config):
16 """Configuration for the printer action."""
18 pass
21class Props(gws.base.action.Props):
22 pass
25class CliParams(gws.CliParams):
26 project: Optional[str]
27 """project uid"""
28 request: str
29 """path to request.json"""
30 output: str
31 """output path"""
34class Object(gws.base.action.Object):
35 @gws.ext.command.api('printerStart')
36 def printer_start(self, req: gws.WebRequester, p: gws.PrintRequest) -> gws.JobStatusResponse:
37 """Start a background print job"""
38 return self.root.app.printerMgr.start_print_job(p, req.user)
40 @gws.ext.command.api('printerStatus')
41 def printer_status(self, req: gws.WebRequester, p: gws.JobRequest) -> gws.JobStatusResponse:
42 res = self.root.app.jobMgr.handle_status_request(req, p)
43 if res.state == gws.JobState.complete:
44 pr = gws.PrintResult(self.root.app.jobMgr.require_result(req, p))
45 ext = gws.lib.mime.extension_for(pr.mime) or 'bin'
46 res.output = {
47 'url': gws.u.action_url_path('printerOutput', jobUid=res.jobUid) + f'/gws.{ext}',
48 }
49 return res
51 @gws.ext.command.api('printerCancel')
52 def printer_cancel(self, req: gws.WebRequester, p: gws.JobRequest) -> gws.JobStatusResponse:
53 return self.root.app.jobMgr.handle_cancel_request(req, p)
55 @gws.ext.command.get('printerOutput')
56 def printer_output(self, req: gws.WebRequester, p: gws.JobRequest) -> gws.ContentResponse:
57 pr = gws.PrintResult(self.root.app.jobMgr.require_result(req, p))
58 return gws.ContentResponse(contentPath=pr.path, mime=pr.mime)
60 @gws.ext.command.cli('printerPrint')
61 def print(self, p: CliParams):
62 """Print using the specified params"""
64 root = gws.config.load()
65 request = root.specs.read(
66 gws.lib.jsonx.from_path(p.request),
67 'gws.PrintRequest',
68 path=p.request,
69 )
71 root.app.printerMgr.exec_print(cast(gws.PrintRequest, request), p.output)