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

41 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-05 13:35 +0200

1"""Printer action.""" 

2 

3from typing import Optional, cast 

4 

5import gws 

6import gws.base.action 

7import gws.lib.jsonx 

8import gws.lib.osx 

9import gws.lib.mime 

10 

11 

12@gws.ext.config.action('printer') 

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

14 """Runs print jobs in the background and returns their output.""" 

15 

16 pass 

17 

18 

19@gws.ext.props.action('printer') 

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

21 pass 

22 

23 

24class CliParams(gws.CliParams): 

25 """Parameters for the ``printerPrint`` command.""" 

26 

27 project: Optional[str] 

28 """Project uid.""" 

29 request: str 

30 """Path to a JSON file with the print request.""" 

31 output: str 

32 """Output path.""" 

33 

34 

35@gws.ext.object.action('printer') 

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

37 """Printer action.""" 

38 

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

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

41 """Start a background print job.""" 

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

43 

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

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

46 """Return the status of a print job.""" 

47 

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

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

50 pr = gws.PrintResult(self.root.app.jobMgr.require_result(req, p)) 

51 ext = gws.lib.mime.extension_for(pr.mimeType) or 'bin' 

52 res.output = { 

53 'url': gws.u.action_url_path('printerOutput', jobUid=res.jobUid) + f'/gws.{ext}', 

54 } 

55 return res 

56 

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

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

59 """Cancel a print job.""" 

60 

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

62 

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

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

65 """Return the result file of a completed print job.""" 

66 

67 pr = gws.PrintResult(self.root.app.jobMgr.require_result(req, p)) 

68 return gws.ContentResponse(contentPath=pr.path, mimeType=pr.mimeType) 

69 

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

71 def print(self, p: CliParams): 

72 """Print a request from a JSON file.""" 

73 

74 root = gws.load_root() 

75 request = root.specs.read( 

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

77 'gws.PrintRequest', 

78 path=p.request, 

79 ) 

80 

81 root.app.printerMgr.exec_print(cast(gws.PrintRequest, request), p.output)