Mercurial > libervia-backend
comparison sat_frontends/jp/cmd_merge_request.py @ 3040:fee60f17ebac
jp: jp asyncio port:
/!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\
This patch implements the port of jp to asyncio, so it is now correctly using the bridge
asynchronously, and it can be used with bridges like `pb`. This also simplify the code,
notably for things which were previously implemented with many callbacks (like pagination
with RSM).
During the process, some behaviours have been modified/fixed, in jp and backends, check
diff for details.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 25 Sep 2019 08:56:41 +0200 |
parents | ab2696e34d29 |
children | 9d0df638c8b4 |
comparison
equal
deleted
inserted
replaced
3039:a1bc34f90fa5 | 3040:fee60f17ebac |
---|---|
16 | 16 |
17 # You should have received a copy of the GNU Affero General Public License | 17 # You should have received a copy of the GNU Affero General Public License |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 | 20 |
21 import os.path | |
21 from . import base | 22 from . import base |
22 from sat.core.i18n import _ | 23 from sat.core.i18n import _ |
23 from sat.tools.common import data_format | 24 from sat.tools.common import data_format |
24 from sat_frontends.jp.constants import Const as C | 25 from sat_frontends.jp.constants import Const as C |
25 from sat_frontends.jp import xmlui_manager | 26 from sat_frontends.jp import xmlui_manager |
26 from sat_frontends.jp import common | 27 from sat_frontends.jp import common |
27 from functools import partial | |
28 import os.path | |
29 | 28 |
30 __commands__ = ["MergeRequest"] | 29 __commands__ = ["MergeRequest"] |
31 | 30 |
32 | 31 |
33 class Set(base.CommandBase): | 32 class Set(base.CommandBase): |
33 | |
34 def __init__(self, host): | 34 def __init__(self, host): |
35 base.CommandBase.__init__( | 35 base.CommandBase.__init__( |
36 self, | 36 self, |
37 host, | 37 host, |
38 "set", | 38 "set", |
39 use_pubsub=True, | 39 use_pubsub=True, |
40 pubsub_defaults={"service": _("auto"), "node": _("auto")}, | 40 pubsub_defaults={"service": _("auto"), "node": _("auto")}, |
41 help=_("publish or update a merge request"), | 41 help=_("publish or update a merge request"), |
42 ) | 42 ) |
43 self.need_loop = True | |
44 | 43 |
45 def add_parser_options(self): | 44 def add_parser_options(self): |
46 self.parser.add_argument( | 45 self.parser.add_argument( |
47 "-i", | 46 "-i", |
48 "--item", | 47 "--item", |
68 dest="labels", | 67 dest="labels", |
69 action="append", | 68 action="append", |
70 help=_("labels to categorize your request"), | 69 help=_("labels to categorize your request"), |
71 ) | 70 ) |
72 | 71 |
73 def mergeRequestSetCb(self, published_id): | 72 async def start(self): |
74 if published_id: | 73 self.repository = os.path.expanduser(os.path.abspath(self.args.repository)) |
75 self.disp("Merge request published at {pub_id}".format(pub_id=published_id)) | 74 await common.fill_well_known_uri(self, self.repository, "merge requests") |
76 else: | 75 if not self.args.force: |
77 self.disp("Merge request published") | 76 message = _( |
78 self.host.quit(C.EXIT_OK) | 77 f"You are going to publish your changes to service " |
79 | 78 f"[{self.args.service}], are you sure ?" |
80 def sendRequest(self): | 79 ) |
80 await self.host.confirmOrQuit( | |
81 message, _("merge request publication cancelled")) | |
82 | |
81 extra = {"update": True} if self.args.item else {} | 83 extra = {"update": True} if self.args.item else {} |
82 values = {} | 84 values = {} |
83 if self.args.labels is not None: | 85 if self.args.labels is not None: |
84 values["labels"] = self.args.labels | 86 values["labels"] = self.args.labels |
85 self.host.bridge.mergeRequestSet( | 87 try: |
86 self.args.service, | 88 published_id = await self.host.bridge.mergeRequestSet( |
87 self.args.node, | 89 self.args.service, |
88 self.repository, | 90 self.args.node, |
89 "auto", | 91 self.repository, |
90 values, | 92 "auto", |
91 "", | 93 values, |
92 self.args.item, | 94 "", |
93 data_format.serialise(extra), | 95 self.args.item, |
94 self.profile, | 96 data_format.serialise(extra), |
95 callback=self.mergeRequestSetCb, | 97 self.profile, |
96 errback=partial( | 98 ) |
97 self.errback, | 99 except Exception as e: |
98 msg=_("can't create merge request: {}"), | 100 self.disp(f"can't create merge requests: {e}", error=True) |
99 exit_code=C.EXIT_BRIDGE_ERRBACK, | 101 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
100 ), | 102 |
101 ) | 103 if published_id: |
102 | 104 self.disp(_(f"Merge request published at {published_id}")) |
103 def askConfirmation(self): | 105 else: |
104 if not self.args.force: | 106 self.disp(_("Merge request published")) |
105 message = _( | 107 |
106 "You are going to publish your changes to service [{service}], are you sure ?" | 108 self.host.quit(C.EXIT_OK) |
107 ).format(service=self.args.service) | |
108 self.host.confirmOrQuit(message, _("merge request publication cancelled")) | |
109 self.sendRequest() | |
110 | |
111 def start(self): | |
112 self.repository = os.path.expanduser(os.path.abspath(self.args.repository)) | |
113 common.URIFinder(self, self.repository, "merge requests", self.askConfirmation) | |
114 | 109 |
115 | 110 |
116 class Get(base.CommandBase): | 111 class Get(base.CommandBase): |
117 def __init__(self, host): | 112 def __init__(self, host): |
118 base.CommandBase.__init__( | 113 base.CommandBase.__init__( |
123 use_pubsub=True, | 118 use_pubsub=True, |
124 pubsub_flags={C.MULTI_ITEMS}, | 119 pubsub_flags={C.MULTI_ITEMS}, |
125 pubsub_defaults={"service": _("auto"), "node": _("auto")}, | 120 pubsub_defaults={"service": _("auto"), "node": _("auto")}, |
126 help=_("get a merge request"), | 121 help=_("get a merge request"), |
127 ) | 122 ) |
128 self.need_loop = True | |
129 | 123 |
130 def add_parser_options(self): | 124 def add_parser_options(self): |
131 pass | 125 pass |
132 | 126 |
133 def mergeRequestGetCb(self, requests_data): | 127 async def start(self): |
128 await common.fill_well_known_uri( | |
129 self, os.getcwd(), "merge requests", meta_map={}) | |
130 extra = {} | |
131 try: | |
132 requests_data = await self.host.bridge.mergeRequestsGet( | |
133 self.args.service, | |
134 self.args.node, | |
135 self.args.max, | |
136 self.args.items, | |
137 "", | |
138 extra, | |
139 self.profile, | |
140 ) | |
141 except Exception as e: | |
142 self.disp(f"can't get merge request: {e}", error=True) | |
143 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
144 | |
134 if self.verbosity >= 1: | 145 if self.verbosity >= 1: |
135 whitelist = None | 146 whitelist = None |
136 else: | 147 else: |
137 whitelist = {"id", "title", "body"} | 148 whitelist = {"id", "title", "body"} |
138 for request_xmlui in requests_data[0]: | 149 for request_xmlui in requests_data[0]: |
139 xmlui = xmlui_manager.create(self.host, request_xmlui, whitelist=whitelist) | 150 xmlui = xmlui_manager.create(self.host, request_xmlui, whitelist=whitelist) |
140 xmlui.show(values_only=True) | 151 await xmlui.show(values_only=True) |
141 self.disp("") | 152 self.disp("") |
142 self.host.quit(C.EXIT_OK) | 153 self.host.quit(C.EXIT_OK) |
143 | |
144 def getRequests(self): | |
145 extra = {} | |
146 self.host.bridge.mergeRequestsGet( | |
147 self.args.service, | |
148 self.args.node, | |
149 self.args.max, | |
150 self.args.items, | |
151 "", | |
152 extra, | |
153 self.profile, | |
154 callback=self.mergeRequestGetCb, | |
155 errback=partial( | |
156 self.errback, | |
157 msg=_("can't get merge request: {}"), | |
158 exit_code=C.EXIT_BRIDGE_ERRBACK, | |
159 ), | |
160 ) | |
161 | |
162 def start(self): | |
163 common.URIFinder( | |
164 self, os.getcwd(), "merge requests", self.getRequests, meta_map={} | |
165 ) | |
166 | 154 |
167 | 155 |
168 class Import(base.CommandBase): | 156 class Import(base.CommandBase): |
169 def __init__(self, host): | 157 def __init__(self, host): |
170 base.CommandBase.__init__( | 158 base.CommandBase.__init__( |
174 use_pubsub=True, | 162 use_pubsub=True, |
175 pubsub_flags={C.SINGLE_ITEM, C.ITEM}, | 163 pubsub_flags={C.SINGLE_ITEM, C.ITEM}, |
176 pubsub_defaults={"service": _("auto"), "node": _("auto")}, | 164 pubsub_defaults={"service": _("auto"), "node": _("auto")}, |
177 help=_("import a merge request"), | 165 help=_("import a merge request"), |
178 ) | 166 ) |
179 self.need_loop = True | |
180 | 167 |
181 def add_parser_options(self): | 168 def add_parser_options(self): |
182 self.parser.add_argument( | 169 self.parser.add_argument( |
183 "-r", | 170 "-r", |
184 "--repository", | 171 "--repository", |
185 metavar="PATH", | 172 metavar="PATH", |
186 default=".", | 173 default=".", |
187 help=_("path of the repository (DEFAULT: current directory)"), | 174 help=_("path of the repository (DEFAULT: current directory)"), |
188 ) | 175 ) |
189 | 176 |
190 def mergeRequestImportCb(self): | 177 async def start(self): |
191 self.host.quit(C.EXIT_OK) | 178 self.repository = os.path.expanduser(os.path.abspath(self.args.repository)) |
192 | 179 await common.fill_well_known_uri( |
193 def importRequest(self): | 180 self, self.repository, "merge requests", meta_map={}) |
194 extra = {} | 181 extra = {} |
195 self.host.bridge.mergeRequestsImport( | 182 try: |
196 self.repository, | 183 await self.host.bridge.mergeRequestsImport( |
197 self.args.item, | 184 self.repository, |
198 self.args.service, | 185 self.args.item, |
199 self.args.node, | 186 self.args.service, |
200 extra, | 187 self.args.node, |
201 self.profile, | 188 extra, |
202 callback=self.mergeRequestImportCb, | 189 self.profile, |
203 errback=partial( | 190 ) |
204 self.errback, | 191 except Exception as e: |
205 msg=_("can't import merge request: {}"), | 192 self.disp(f"can't import merge request: {e}", error=True) |
206 exit_code=C.EXIT_BRIDGE_ERRBACK, | 193 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
207 ), | 194 else: |
208 ) | 195 self.host.quit() |
209 | |
210 def start(self): | |
211 self.repository = os.path.expanduser(os.path.abspath(self.args.repository)) | |
212 common.URIFinder( | |
213 self, self.repository, "merge requests", self.importRequest, meta_map={} | |
214 ) | |
215 | 196 |
216 | 197 |
217 class MergeRequest(base.CommandBase): | 198 class MergeRequest(base.CommandBase): |
218 subcommands = (Set, Get, Import) | 199 subcommands = (Set, Get, Import) |
219 | 200 |