| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0c5a121 commit 9d629bb
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -278,7 +278,7 @@ def create( | |||
| 278 | 278 | if data is None: | |
| 279 | 279 | data = {} | |
| 280 | 280 | ||
| 281 | - utils._validate_attrs(data=data, attributes=self._create_attrs) | ||
| 281 | + self._create_attrs.validate_attrs(data=data) | ||
| 282 | 282 | data, files = utils._transform_types(data, self._types) | |
| 283 | 283 | ||
| 284 | 284 | # Handle specific URL for creation | |
@@ -345,9 +345,7 @@ def update( | |||
| 345 | 345 | excludes = [] | |
| 346 | 346 | if self._obj_cls is not None and self._obj_cls._id_attr is not None: | |
| 347 | 347 | excludes = [self._obj_cls._id_attr] | |
| 348 | - utils._validate_attrs( | ||
| 349 | - data=new_data, attributes=self._update_attrs, excludes=excludes | ||
| 350 | - ) | ||
| 348 | + self._update_attrs.validate_attrs(data=new_data, excludes=excludes) | ||
| 351 | 349 | new_data, files = utils._transform_types(new_data, self._types) | |
| 352 | 350 | ||
| 353 | 351 | http_method = self._get_update_method() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,7 +16,7 @@ | |||
| 16 | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | 17 | ||
| 18 | 18 | import dataclasses | |
| 19 | - from typing import Any, Optional, Tuple, TYPE_CHECKING | ||
| 19 | + from typing import Any, Dict, List, Optional, Tuple, TYPE_CHECKING | ||
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | 22 | @dataclasses.dataclass(frozen=True) | |
@@ -25,6 +25,33 @@ class RequiredOptional: | |||
| 25 | 25 | optional: Tuple[str, ...] = () | |
| 26 | 26 | exclusive: Tuple[str, ...] = () | |
| 27 | 27 | ||
| 28 | + def validate_attrs( | ||
| 29 | + self, | ||
| 30 | + *, | ||
| 31 | + data: Dict[str, Any], | ||
| 32 | + excludes: Optional[List[str]] = None, | ||
| 33 | + ) -> None: | ||
| 34 | + if excludes is None: | ||
| 35 | + excludes = [] | ||
| 36 | + | ||
| 37 | + if self.required: | ||
| 38 | + required = [k for k in self.required if k not in excludes] | ||
| 39 | + missing = [attr for attr in required if attr not in data] | ||
| 40 | + if missing: | ||
| 41 | + raise AttributeError(f"Missing attributes: {', '.join(missing)}") | ||
| 42 | + | ||
| 43 | + if self.exclusive: | ||
| 44 | + exclusives = [attr for attr in data if attr in self.exclusive] | ||
| 45 | + if len(exclusives) > 1: | ||
| 46 | + raise AttributeError( | ||
| 47 | + f"Provide only one of these attributes: {', '.join(exclusives)}" | ||
| 48 | + ) | ||
| 49 | + if not exclusives: | ||
| 50 | + raise AttributeError( | ||
| 51 | + f"Must provide one of these attributes: " | ||
| 52 | + f"{', '.join(self.exclusive)}" | ||
| 53 | + ) | ||
| 54 | + | ||
| 28 | 55 | ||
| 29 | 56 | class GitlabAttribute: | |
| 30 | 57 | def __init__(self, value: Any = None) -> None: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,7 +19,7 @@ | |||
| 19 | 19 | import traceback | |
| 20 | 20 | import urllib.parse | |
| 21 | 21 | import warnings | |
| 22 | - from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union | ||
| 22 | + from typing import Any, Callable, Dict, Optional, Tuple, Type, Union | ||
| 23 | 23 | ||
| 24 | 24 | import requests | |
| 25 | 25 | ||
@@ -159,30 +159,3 @@ def warn( | |||
| 159 | 159 | stacklevel=stacklevel, | |
| 160 | 160 | source=source, | |
| 161 | 161 | ) | |
| 162 | - | ||
| 163 | - | ||
| 164 | - def _validate_attrs( | ||
| 165 | - data: Dict[str, Any], | ||
| 166 | - attributes: types.RequiredOptional, | ||
| 167 | - excludes: Optional[List[str]] = None, | ||
| 168 | - ) -> None: | ||
| 169 | - if excludes is None: | ||
| 170 | - excludes = [] | ||
| 171 | - | ||
| 172 | - if attributes.required: | ||
| 173 | - required = [k for k in attributes.required if k not in excludes] | ||
| 174 | - missing = [attr for attr in required if attr not in data] | ||
| 175 | - if missing: | ||
| 176 | - raise AttributeError(f"Missing attributes: {', '.join(missing)}") | ||
| 177 | - | ||
| 178 | - if attributes.exclusive: | ||
| 179 | - exclusives = [attr for attr in data if attr in attributes.exclusive] | ||
| 180 | - if len(exclusives) > 1: | ||
| 181 | - raise AttributeError( | ||
| 182 | - f"Provide only one of these attributes: {', '.join(exclusives)}" | ||
| 183 | - ) | ||
| 184 | - if not exclusives: | ||
| 185 | - raise AttributeError( | ||
| 186 | - f"Must provide one of these attributes: " | ||
| 187 | - f"{', '.join(attributes.exclusive)}" | ||
| 188 | - ) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | 1 | from typing import Any, cast, Dict, Optional, TYPE_CHECKING, Union | |
| 2 | 2 | ||
| 3 | 3 | from gitlab import exceptions as exc | |
| 4 | - from gitlab import types, utils | ||
| 4 | + from gitlab import types | ||
| 5 | 5 | from gitlab.base import RESTManager, RESTObject | |
| 6 | 6 | from gitlab.mixins import ( | |
| 7 | 7 | CreateMixin, | |
@@ -107,7 +107,7 @@ def create( | |||
| 107 | 107 | """ | |
| 108 | 108 | if TYPE_CHECKING: | |
| 109 | 109 | assert data is not None | |
| 110 | - utils._validate_attrs(data=data, attributes=self._create_attrs) | ||
| 110 | + self._create_attrs.validate_attrs(data=data) | ||
| 111 | 111 | path = f"{self.path}/{data.pop('issue_id')}" | |
| 112 | 112 | server_data = self.gitlab.http_post(path, **kwargs) | |
| 113 | 113 | if TYPE_CHECKING: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -145,7 +145,7 @@ def create( | |||
| 145 | 145 | ||
| 146 | 146 | if TYPE_CHECKING: | |
| 147 | 147 | assert data is not None | |
| 148 | - utils._validate_attrs(data=data, attributes=self._create_attrs) | ||
| 148 | + self._create_attrs.validate_attrs(data=data) | ||
| 149 | 149 | new_data = data.copy() | |
| 150 | 150 | file_path = utils.EncodedId(new_data.pop("file_path")) | |
| 151 | 151 | path = f"{self.path}/{file_path}" | |
@@ -179,7 +179,7 @@ def update( # type: ignore | |||
| 179 | 179 | file_path = utils.EncodedId(file_path) | |
| 180 | 180 | data["file_path"] = file_path | |
| 181 | 181 | path = f"{self.path}/{file_path}" | |
| 182 | - utils._validate_attrs(data=data, attributes=self._update_attrs) | ||
| 182 | + self._update_attrs.validate_attrs(data=data) | ||
| 183 | 183 | result = self.gitlab.http_put(path, post_data=data, **kwargs) | |
| 184 | 184 | if TYPE_CHECKING: | |
| 185 | 185 | assert isinstance(result, dict) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | 3 | from gitlab import cli | |
| 4 | 4 | from gitlab import exceptions as exc | |
| 5 | - from gitlab import types, utils | ||
| 5 | + from gitlab import types | ||
| 6 | 6 | from gitlab.base import RESTManager, RESTObject | |
| 7 | 7 | from gitlab.mixins import ( | |
| 8 | 8 | CreateMixin, | |
@@ -272,7 +272,7 @@ def create( # type: ignore | |||
| 272 | 272 | GitlabAuthenticationError: If authentication is not correct | |
| 273 | 273 | GitlabCreateError: If the server cannot perform the request | |
| 274 | 274 | """ | |
| 275 | - utils._validate_attrs(data=data, attributes=self._create_attrs) | ||
| 275 | + self._create_attrs.validate_attrs(data=data) | ||
| 276 | 276 | if TYPE_CHECKING: | |
| 277 | 277 | assert self.path is not None | |
| 278 | 278 | server_data = self.gitlab.http_post(self.path, post_data=data, **kwargs) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,6 @@ | |||
| 3 | 3 | ||
| 4 | 4 | from gitlab import base | |
| 5 | 5 | from gitlab import types as gl_types | |
| 6 | - from gitlab import utils | ||
| 7 | 6 | from gitlab.mixins import ( | |
| 8 | 7 | CreateMixin, | |
| 9 | 8 | DeleteMixin, | |
@@ -174,11 +173,11 @@ class M(CreateMixin, FakeManager): | |||
| 174 | 173 | ||
| 175 | 174 | mgr = M(gl) | |
| 176 | 175 | data = {"foo": "bar", "baz": "blah"} | |
| 177 | - utils._validate_attrs(data=data, attributes=mgr._create_attrs) | ||
| 176 | + mgr._create_attrs.validate_attrs(data=data) | ||
| 178 | 177 | ||
| 179 | 178 | data = {"baz": "blah"} | |
| 180 | 179 | with pytest.raises(AttributeError) as error: | |
| 181 | - utils._validate_attrs(data=data, attributes=mgr._create_attrs) | ||
| 180 | + mgr._create_attrs.validate_attrs(data=data) | ||
| 182 | 181 | assert "foo" in str(error.value) | |
| 183 | 182 | ||
| 184 | 183 | ||
@@ -240,11 +239,11 @@ class M(UpdateMixin, FakeManager): | |||
| 240 | 239 | ||
| 241 | 240 | mgr = M(gl) | |
| 242 | 241 | data = {"foo": "bar", "baz": "blah"} | |
| 243 | - utils._validate_attrs(data=data, attributes=mgr._update_attrs) | ||
| 242 | + mgr._update_attrs.validate_attrs(data=data) | ||
| 244 | 243 | ||
| 245 | 244 | data = {"baz": "blah"} | |
| 246 | 245 | with pytest.raises(AttributeError) as error: | |
| 247 | - utils._validate_attrs(data=data, attributes=mgr._update_attrs) | ||
| 246 | + mgr._update_attrs.validate_attrs(data=data) | ||
| 248 | 247 | assert "foo" in str(error.value) | |
| 249 | 248 | ||
| 250 | 249 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments