Coverage for gws-app/gws/base/printer/action.py: 0%

45 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-16 22:59 +0200

1"""Provides the printing API.""" 

2 

3from typing import Optional 

4 

5import gws 

6import gws.base.action 

7import gws.config 

8import gws.lib.jsonx 

9import gws.lib.mime 

10 

11gws.ext.new.action('printer') 

12 

13 

14class Config(gws.base.action.Config): 

15 """Configuration for the printer action.""" 

16 

17 pass 

18 

19 

20class Props(gws.base.action.Props): 

21 pass 

22 

23 

24class CliParams(gws.CliParams): 

25 project: Optional[str] 

26 """project uid""" 

27 request: str 

28 """path to request.json""" 

29 output: str 

30 """output path""" 

31 

32 

33class Object(gws.base.action.Object): 

34 @gws.ext.command.api('printerStart') 

35 def printer_start(self, req: gws.WebRequester, p: gws.PrintRequest) -> gws.JobStatusResponse: 

36 """Start a background print job""" 

37 return self.root.app.printerMgr.start_print_job(p, req.user) 

38 

39 @gws.ext.command.api('printerStatus') 

40 def printer_status(self, req: gws.WebRequester, p: gws.JobRequest) -> gws.JobStatusResponse: 

41 res = self.root.app.jobMgr.handle_status_request(req, p) 

42 if res.state == gws.JobState.complete: 

43 res.output = {'url': gws.u.action_url_path('printerOutput', jobUid=res.jobUid) + '/gws.pdf'} 

44 return res 

45 

46 @gws.ext.command.api('printerCancel') 

47 def printer_cancel(self, req: gws.WebRequester, p: gws.JobRequest) -> gws.JobStatusResponse: 

48 return self.root.app.jobMgr.handle_cancel_request(req, p) 

49 

50 @gws.ext.command.get('printerOutput') 

51 def printer_output(self, req: gws.WebRequester, p: gws.JobRequest) -> gws.ContentResponse: 

52 job = self.root.app.jobMgr.require_job(req, p) 

53 

54 if job.state != gws.JobState.complete: 

55 raise gws.NotFoundError(f'JOB {p.jobUid}: wrong state {job.state!r}') 

56 

57 out = job.payload.get('outputPath') 

58 if not out: 

59 raise gws.NotFoundError(f'JOB {p.jobUid}: no output path') 

60 return gws.ContentResponse(contentPath=out, mime=gws.lib.mime.PDF) 

61 

62 @gws.ext.command.cli('printerPrint') 

63 def print(self, p: CliParams): 

64 """Print using the specified params""" 

65 

66 root = gws.config.load() 

67 request = root.specs.read( 

68 gws.lib.jsonx.from_path(p.request), 

69 'gws.PrintRequest', 

70 path=p.request, 

71 ) 

72 

73 root.app.printerMgr.exec_print(request, p.output)