Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: add _create_attrs & _update_attrs to RESTManager#1371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nejch merged 1 commit intopython-gitlab:masterfromJohnVillalovos:jlvillal/create_attrs_1
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletiongitlab/base.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -17,7 +17,7 @@

import importlib
from types import ModuleType
from typing import Any, Dict, Optional, Type
from typing import Any, Dict, Optional,Tuple,Type

from .client import Gitlab, GitlabList
from gitlab import types as g_types
Expand DownExpand Up@@ -258,6 +258,8 @@ class RESTManager(object):
``_obj_cls``: The class of objects that will be created
"""

_create_attrs: Tuple[Tuple[str, ...], Tuple[str, ...]] = (tuple(), tuple())
_update_attrs: Tuple[Tuple[str, ...], Tuple[str, ...]] = (tuple(), tuple())
_path: Optional[str] = None
_obj_cls: Optional[Type[RESTObject]] = None
_from_parent_attrs: Dict[str, Any] = {}
Expand Down
32 changes: 7 additions & 25 deletionsgitlab/mixins.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -266,24 +266,14 @@ class CreateMixin(_RestManagerBase):
gitlab: gitlab.Gitlab

def _check_missing_create_attrs(self, data: Dict[str, Any]) -> None:
required, optional = self.get_create_attrs()
missing = []
for attr inrequired:
for attr inself._create_attrs[0]:
if attr not in data:
missing.append(attr)
continue
if missing:
raise AttributeError("Missing attributes: %s" % ", ".join(missing))

def get_create_attrs(self) -> Tuple[Tuple[str, ...], Tuple[str, ...]]:
"""Return the required and optional arguments.

Returns:
tuple: 2 items: list of required arguments and list of optional
arguments for creation (in that order)
"""
return getattr(self, "_create_attrs", (tuple(), tuple()))

@exc.on_http_error(exc.GitlabCreateError)
def create(
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any
Expand DownExpand Up@@ -344,11 +334,13 @@ class UpdateMixin(_RestManagerBase):
gitlab: gitlab.Gitlab

def _check_missing_update_attrs(self, data: Dict[str, Any]) -> None:
required, optional = self.get_update_attrs()
if TYPE_CHECKING:
assert self._obj_cls is not None
# Remove the id field from the required list as it was previously moved to the http path.
required = tuple([k for k in required if k != self._obj_cls._id_attr])
# Remove the id field from the required list as it was previously moved
# to the http path.
required = tuple(
[k for k in self._update_attrs[0] if k != self._obj_cls._id_attr]
)
missing = []
for attr in required:
if attr not in data:
Expand All@@ -357,15 +349,6 @@ def _check_missing_update_attrs(self, data: Dict[str, Any]) -> None:
if missing:
raise AttributeError("Missing attributes: %s" % ", ".join(missing))

def get_update_attrs(self) -> Tuple[Tuple[str, ...], Tuple[str, ...]]:
"""Return the required and optional arguments.

Returns:
tuple: 2 items: list of required arguments and list of optional
arguments for update (in that order)
"""
return getattr(self, "_update_attrs", (tuple(), tuple()))

def _get_update_method(
self,
) -> Callable[..., Union[Dict[str, Any], requests.Response]]:
Expand DownExpand Up@@ -535,8 +518,7 @@ class SaveMixin(_RestObjectBase):

def _get_updated_data(self) -> Dict[str, Any]:
updated_data = {}
required, optional = self.manager.get_update_attrs()
for attr in required:
for attr in self.manager._update_attrs[0]:
# Get everything required, no matter if it's been updated
updated_data[attr] = getattr(self, attr)
# Add the updated attributes
Expand Down
42 changes: 0 additions & 42 deletionsgitlab/tests/mixins/test_mixin_methods.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -129,27 +129,6 @@ def resp_cont(url, request):
obj_list.next()


def test_create_mixin_get_attrs(gl):
class M1(CreateMixin, FakeManager):
pass

class M2(CreateMixin, FakeManager):
_create_attrs = (("foo",), ("bar", "baz"))
_update_attrs = (("foo",), ("bam",))

mgr = M1(gl)
required, optional = mgr.get_create_attrs()
assert len(required) == 0
assert len(optional) == 0

mgr = M2(gl)
required, optional = mgr.get_create_attrs()
assert "foo" in required
assert "bar" in optional
assert "baz" in optional
assert "bam" not in optional


def test_create_mixin_missing_attrs(gl):
class M(CreateMixin, FakeManager):
_create_attrs = (("foo",), ("bar", "baz"))
Expand DownExpand Up@@ -202,27 +181,6 @@ def resp_cont(url, request):
assert obj.foo == "bar"


def test_update_mixin_get_attrs(gl):
class M1(UpdateMixin, FakeManager):
pass

class M2(UpdateMixin, FakeManager):
_create_attrs = (("foo",), ("bar", "baz"))
_update_attrs = (("foo",), ("bam",))

mgr = M1(gl)
required, optional = mgr.get_update_attrs()
assert len(required) == 0
assert len(optional) == 0

mgr = M2(gl)
required, optional = mgr.get_update_attrs()
assert "foo" in required
assert "bam" in optional
assert "bar" not in optional
assert "baz" not in optional


def test_update_mixin_missing_attrs(gl):
class M(UpdateMixin, FakeManager):
_update_attrs = (("foo",), ("bar", "baz"))
Expand Down
35 changes: 12 additions & 23 deletionsgitlab/v4/cli.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -177,42 +177,31 @@ def _populate_sub_parser_by_class(cls, sub_parser):
]

if action_name == "create":
if hasattr(mgr_cls, "_create_attrs"):
[
sub_parser_action.add_argument(
"--%s" % x.replace("_", "-"), required=True
)
for x in mgr_cls._create_attrs[0]
]

[
sub_parser_action.add_argument(
"--%s" % x.replace("_", "-"), required=False
)
for x in mgr_cls._create_attrs[1]
]
for x in mgr_cls._create_attrs[0]:
sub_parser_action.add_argument(
"--%s" % x.replace("_", "-"), required=True
)
for x in mgr_cls._create_attrs[1]:
sub_parser_action.add_argument(
"--%s" % x.replace("_", "-"), required=False
)

if action_name == "update":
if cls._id_attr is not None:
id_attr = cls._id_attr.replace("_", "-")
sub_parser_action.add_argument("--%s" % id_attr, required=True)

if hasattr(mgr_cls, "_update_attrs"):
[
for x in mgr_cls._update_attrs[0]:
if x != cls._id_attr:
sub_parser_action.add_argument(
"--%s" % x.replace("_", "-"), required=True
)
for x in mgr_cls._update_attrs[0]
if x != cls._id_attr
]

[
for x in mgr_cls._update_attrs[1]:
if x != cls._id_attr:
sub_parser_action.add_argument(
"--%s" % x.replace("_", "-"), required=False
)
for x in mgr_cls._update_attrs[1]
if x != cls._id_attr
]

if cls.__name__ in cli.custom_actions:
name = cls.__name__
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp