Mercurial > libervia-backend
comparison sat_frontends/jp/cmd_application.py @ 3374:47755614b82a
jp (application): new `application` (or `app`) commands:
those commands allow to:
- list available SàT applications
- start an application
- stop an application
- display/get exposed values
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 28 Sep 2020 21:10:33 +0200 |
parents | |
children | 297389b1563c |
comparison
equal
deleted
inserted
replaced
3373:f44402f8a81f | 3374:47755614b82a |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 # jp: a SàT command line tool | |
4 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) | |
5 | |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | |
19 from . import base | |
20 from sat.core.i18n import _ | |
21 from sat.tools.common import data_format | |
22 from sat_frontends.jp.constants import Const as C | |
23 | |
24 __commands__ = ["Application"] | |
25 | |
26 | |
27 class List(base.CommandBase): | |
28 """List available applications""" | |
29 | |
30 def __init__(self, host): | |
31 super(List, self).__init__( | |
32 host, "list", use_profile=False, use_output=C.OUTPUT_LIST, | |
33 help=_("list available applications") | |
34 ) | |
35 | |
36 def add_parser_options(self): | |
37 self.parser.add_argument( | |
38 "-f", | |
39 "--filters", | |
40 action="extend", | |
41 nargs="+", | |
42 choices=["available", "running"], | |
43 default=["available"], | |
44 help=_("show applications with this status"), | |
45 ) | |
46 | |
47 async def start(self): | |
48 try: | |
49 found_apps = await self.host.bridge.applicationsList(self.args.filters) | |
50 except Exception as e: | |
51 self.disp(f"can't get applications list: {e}", error=True) | |
52 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
53 else: | |
54 await self.output(found_apps) | |
55 self.host.quit() | |
56 | |
57 | |
58 class Start(base.CommandBase): | |
59 """Start an application""" | |
60 | |
61 def __init__(self, host): | |
62 super(Start, self).__init__( | |
63 host, "start", use_profile=False, help=_("start an application") | |
64 ) | |
65 | |
66 def add_parser_options(self): | |
67 self.parser.add_argument( | |
68 "name", | |
69 help=_("name of the application to start"), | |
70 ) | |
71 | |
72 async def start(self): | |
73 try: | |
74 await self.host.bridge.applicationStart( | |
75 self.args.name, | |
76 "", | |
77 ) | |
78 except Exception as e: | |
79 self.disp(f"can't start {self.args.name}: {e}", error=True) | |
80 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
81 else: | |
82 self.host.quit() | |
83 | |
84 | |
85 class Stop(base.CommandBase): | |
86 | |
87 def __init__(self, host): | |
88 super(Stop, self).__init__( | |
89 host, "stop", use_profile=False, help=_("stop a running application") | |
90 ) | |
91 | |
92 def add_parser_options(self): | |
93 id_group = self.parser.add_mutually_exclusive_group(required=True) | |
94 id_group.add_argument( | |
95 "name", | |
96 nargs="?", | |
97 help=_("name of the application to stop"), | |
98 ) | |
99 id_group.add_argument( | |
100 "-i", | |
101 "--id", | |
102 help=_("identifier of the instance to stop"), | |
103 ) | |
104 | |
105 async def start(self): | |
106 try: | |
107 if self.args.name is not None: | |
108 args = [self.args.name, "name"] | |
109 else: | |
110 args = [self.args.id, "instance"] | |
111 await self.host.bridge.applicationStop( | |
112 *args, | |
113 "", | |
114 ) | |
115 except Exception as e: | |
116 if self.args.name is not None: | |
117 self.disp( | |
118 f"can't stop application {self.args.name!r}: {e}", error=True) | |
119 else: | |
120 self.disp( | |
121 f"can't stop application instance with id {self.args.id!r}: {e}", | |
122 error=True) | |
123 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
124 else: | |
125 self.host.quit() | |
126 | |
127 | |
128 class Exposed(base.CommandBase): | |
129 | |
130 def __init__(self, host): | |
131 super(Exposed, self).__init__( | |
132 host, "exposed", use_profile=False, use_output=C.OUTPUT_DICT, | |
133 help=_("show data exposed by a running application") | |
134 ) | |
135 | |
136 def add_parser_options(self): | |
137 id_group = self.parser.add_mutually_exclusive_group(required=True) | |
138 id_group.add_argument( | |
139 "name", | |
140 nargs="?", | |
141 help=_("name of the application to stop"), | |
142 ) | |
143 id_group.add_argument( | |
144 "-i", | |
145 "--id", | |
146 help=_("identifier of the instance to stop"), | |
147 ) | |
148 | |
149 async def start(self): | |
150 try: | |
151 if self.args.name is not None: | |
152 args = [self.args.name, "name"] | |
153 else: | |
154 args = [self.args.id, "instance"] | |
155 exposed_data_raw = await self.host.bridge.applicationExposedGet( | |
156 *args, | |
157 "", | |
158 ) | |
159 except Exception as e: | |
160 if self.args.name is not None: | |
161 self.disp( | |
162 f"can't get values exposed from application {self.args.name!r}: {e}", | |
163 error=True) | |
164 else: | |
165 self.disp( | |
166 f"can't values exposed from application instance with id {self.args.id!r}: {e}", | |
167 error=True) | |
168 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
169 else: | |
170 exposed_data = data_format.deserialise(exposed_data_raw) | |
171 await self.output(exposed_data) | |
172 self.host.quit() | |
173 | |
174 | |
175 class Application(base.CommandBase): | |
176 subcommands = (List, Start, Stop, Exposed) | |
177 | |
178 def __init__(self, host): | |
179 super(Application, self).__init__( | |
180 host, "application", use_profile=False, help=_("manage applications"), | |
181 aliases=['app'], | |
182 ) |