| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6fde243 commit 147f05d
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,7 +17,7 @@ | |||
| 17 | 17 | ||
| 18 | 18 | import importlib | |
| 19 | 19 | from types import ModuleType | |
| 20 | - from typing import Any, Dict, Optional, Type | ||
| 20 | + from typing import Any, Dict, Optional, Tuple, Type | ||
| 21 | 21 | ||
| 22 | 22 | from .client import Gitlab, GitlabList | |
| 23 | 23 | from gitlab import types as g_types | |
@@ -258,6 +258,8 @@ class RESTManager(object): | |||
| 258 | 258 | ``_obj_cls``: The class of objects that will be created | |
| 259 | 259 | """ | |
| 260 | 260 | ||
| 261 | + _create_attrs: Tuple[Tuple[str, ...], Tuple[str, ...]] = (tuple(), tuple()) | ||
| 262 | + _update_attrs: Tuple[Tuple[str, ...], Tuple[str, ...]] = (tuple(), tuple()) | ||
| 261 | 263 | _path: Optional[str] = None | |
| 262 | 264 | _obj_cls: Optional[Type[RESTObject]] = None | |
| 263 | 265 | _from_parent_attrs: Dict[str, Any] = {} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -266,24 +266,14 @@ class CreateMixin(_RestManagerBase): | |||
| 266 | 266 | gitlab: gitlab.Gitlab | |
| 267 | 267 | ||
| 268 | 268 | def _check_missing_create_attrs(self, data: Dict[str, Any]) -> None: | |
| 269 | - required, optional = self.get_create_attrs() | ||
| 270 | 269 | missing = [] | |
| 271 | - for attr in required: | ||
| 270 | + for attr in self._create_attrs[0]: | ||
| 272 | 271 | if attr not in data: | |
| 273 | 272 | missing.append(attr) | |
| 274 | 273 | continue | |
| 275 | 274 | if missing: | |
| 276 | 275 | raise AttributeError("Missing attributes: %s" % ", ".join(missing)) | |
| 277 | 276 | ||
| 278 | - def get_create_attrs(self) -> Tuple[Tuple[str, ...], Tuple[str, ...]]: | ||
| 279 | - """Return the required and optional arguments. | ||
| 280 | - | ||
| 281 | - Returns: | ||
| 282 | - tuple: 2 items: list of required arguments and list of optional | ||
| 283 | - arguments for creation (in that order) | ||
| 284 | - """ | ||
| 285 | - return getattr(self, "_create_attrs", (tuple(), tuple())) | ||
| 286 | - | ||
| 287 | 277 | @exc.on_http_error(exc.GitlabCreateError) | |
| 288 | 278 | def create( | |
| 289 | 279 | self, data: Optional[Dict[str, Any]] = None, **kwargs: Any | |
@@ -344,11 +334,13 @@ class UpdateMixin(_RestManagerBase): | |||
| 344 | 334 | gitlab: gitlab.Gitlab | |
| 345 | 335 | ||
| 346 | 336 | def _check_missing_update_attrs(self, data: Dict[str, Any]) -> None: | |
| 347 | - required, optional = self.get_update_attrs() | ||
| 348 | 337 | if TYPE_CHECKING: | |
| 349 | 338 | assert self._obj_cls is not None | |
| 350 | - # Remove the id field from the required list as it was previously moved to the http path. | ||
| 351 | - required = tuple([k for k in required if k != self._obj_cls._id_attr]) | ||
| 339 | + # Remove the id field from the required list as it was previously moved | ||
| 340 | + # to the http path. | ||
| 341 | + required = tuple( | ||
| 342 | + [k for k in self._update_attrs[0] if k != self._obj_cls._id_attr] | ||
| 343 | + ) | ||
| 352 | 344 | missing = [] | |
| 353 | 345 | for attr in required: | |
| 354 | 346 | if attr not in data: | |
@@ -357,15 +349,6 @@ def _check_missing_update_attrs(self, data: Dict[str, Any]) -> None: | |||
| 357 | 349 | if missing: | |
| 358 | 350 | raise AttributeError("Missing attributes: %s" % ", ".join(missing)) | |
| 359 | 351 | ||
| 360 | - def get_update_attrs(self) -> Tuple[Tuple[str, ...], Tuple[str, ...]]: | ||
| 361 | - """Return the required and optional arguments. | ||
| 362 | - | ||
| 363 | - Returns: | ||
| 364 | - tuple: 2 items: list of required arguments and list of optional | ||
| 365 | - arguments for update (in that order) | ||
| 366 | - """ | ||
| 367 | - return getattr(self, "_update_attrs", (tuple(), tuple())) | ||
| 368 | - | ||
| 369 | 352 | def _get_update_method( | |
| 370 | 353 | self, | |
| 371 | 354 | ) -> Callable[..., Union[Dict[str, Any], requests.Response]]: | |
@@ -535,8 +518,7 @@ class SaveMixin(_RestObjectBase): | |||
| 535 | 518 | ||
| 536 | 519 | def _get_updated_data(self) -> Dict[str, Any]: | |
| 537 | 520 | updated_data = {} | |
| 538 | - required, optional = self.manager.get_update_attrs() | ||
| 539 | - for attr in required: | ||
| 521 | + for attr in self.manager._update_attrs[0]: | ||
| 540 | 522 | # Get everything required, no matter if it's been updated | |
| 541 | 523 | updated_data[attr] = getattr(self, attr) | |
| 542 | 524 | # Add the updated attributes | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -129,27 +129,6 @@ def resp_cont(url, request): | |||
| 129 | 129 | obj_list.next() | |
| 130 | 130 | ||
| 131 | 131 | ||
| 132 | - def test_create_mixin_get_attrs(gl): | ||
| 133 | - class M1(CreateMixin, FakeManager): | ||
| 134 | - pass | ||
| 135 | - | ||
| 136 | - class M2(CreateMixin, FakeManager): | ||
| 137 | - _create_attrs = (("foo",), ("bar", "baz")) | ||
| 138 | - _update_attrs = (("foo",), ("bam",)) | ||
| 139 | - | ||
| 140 | - mgr = M1(gl) | ||
| 141 | - required, optional = mgr.get_create_attrs() | ||
| 142 | - assert len(required) == 0 | ||
| 143 | - assert len(optional) == 0 | ||
| 144 | - | ||
| 145 | - mgr = M2(gl) | ||
| 146 | - required, optional = mgr.get_create_attrs() | ||
| 147 | - assert "foo" in required | ||
| 148 | - assert "bar" in optional | ||
| 149 | - assert "baz" in optional | ||
| 150 | - assert "bam" not in optional | ||
| 151 | - | ||
| 152 | - | ||
| 153 | 132 | def test_create_mixin_missing_attrs(gl): | |
| 154 | 133 | class M(CreateMixin, FakeManager): | |
| 155 | 134 | _create_attrs = (("foo",), ("bar", "baz")) | |
@@ -202,27 +181,6 @@ def resp_cont(url, request): | |||
| 202 | 181 | assert obj.foo == "bar" | |
| 203 | 182 | ||
| 204 | 183 | ||
| 205 | - def test_update_mixin_get_attrs(gl): | ||
| 206 | - class M1(UpdateMixin, FakeManager): | ||
| 207 | - pass | ||
| 208 | - | ||
| 209 | - class M2(UpdateMixin, FakeManager): | ||
| 210 | - _create_attrs = (("foo",), ("bar", "baz")) | ||
| 211 | - _update_attrs = (("foo",), ("bam",)) | ||
| 212 | - | ||
| 213 | - mgr = M1(gl) | ||
| 214 | - required, optional = mgr.get_update_attrs() | ||
| 215 | - assert len(required) == 0 | ||
| 216 | - assert len(optional) == 0 | ||
| 217 | - | ||
| 218 | - mgr = M2(gl) | ||
| 219 | - required, optional = mgr.get_update_attrs() | ||
| 220 | - assert "foo" in required | ||
| 221 | - assert "bam" in optional | ||
| 222 | - assert "bar" not in optional | ||
| 223 | - assert "baz" not in optional | ||
| 224 | - | ||
| 225 | - | ||
| 226 | 184 | def test_update_mixin_missing_attrs(gl): | |
| 227 | 185 | class M(UpdateMixin, FakeManager): | |
| 228 | 186 | _update_attrs = (("foo",), ("bar", "baz")) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -177,42 +177,31 @@ def _populate_sub_parser_by_class(cls, sub_parser): | |||
| 177 | 177 | ] | |
| 178 | 178 | ||
| 179 | 179 | if action_name == "create": | |
| 180 | - if hasattr(mgr_cls, "_create_attrs"): | ||
| 181 | - [ | ||
| 182 | - sub_parser_action.add_argument( | ||
| 183 | - "--%s" % x.replace("_", "-"), required=True | ||
| 184 | - ) | ||
| 185 | - for x in mgr_cls._create_attrs[0] | ||
| 186 | - ] | ||
| 187 | - | ||
| 188 | - [ | ||
| 189 | - sub_parser_action.add_argument( | ||
| 190 | - "--%s" % x.replace("_", "-"), required=False | ||
| 191 | - ) | ||
| 192 | - for x in mgr_cls._create_attrs[1] | ||
| 193 | - ] | ||
| 180 | + for x in mgr_cls._create_attrs[0]: | ||
| 181 | + sub_parser_action.add_argument( | ||
| 182 | + "--%s" % x.replace("_", "-"), required=True | ||
| 183 | + ) | ||
| 184 | + for x in mgr_cls._create_attrs[1]: | ||
| 185 | + sub_parser_action.add_argument( | ||
| 186 | + "--%s" % x.replace("_", "-"), required=False | ||
| 187 | + ) | ||
| 194 | 188 | ||
| 195 | 189 | if action_name == "update": | |
| 196 | 190 | if cls._id_attr is not None: | |
| 197 | 191 | id_attr = cls._id_attr.replace("_", "-") | |
| 198 | 192 | sub_parser_action.add_argument("--%s" % id_attr, required=True) | |
| 199 | 193 | ||
| 200 | - if hasattr(mgr_cls, "_update_attrs"): | ||
| 201 | - [ | ||
| 194 | + for x in mgr_cls._update_attrs[0]: | ||
| 195 | + if x != cls._id_attr: | ||
| 202 | 196 | sub_parser_action.add_argument( | |
| 203 | 197 | "--%s" % x.replace("_", "-"), required=True | |
| 204 | 198 | ) | |
| 205 | - for x in mgr_cls._update_attrs[0] | ||
| 206 | - if x != cls._id_attr | ||
| 207 | - ] | ||
| 208 | 199 | ||
| 209 | - [ | ||
| 200 | + for x in mgr_cls._update_attrs[1]: | ||
| 201 | + if x != cls._id_attr: | ||
| 210 | 202 | sub_parser_action.add_argument( | |
| 211 | 203 | "--%s" % x.replace("_", "-"), required=False | |
| 212 | 204 | ) | |
| 213 | - for x in mgr_cls._update_attrs[1] | ||
| 214 | - if x != cls._id_attr | ||
| 215 | - ] | ||
| 216 | 205 | ||
| 217 | 206 | if cls.__name__ in cli.custom_actions: | |
| 218 | 207 | name = cls.__name__ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments