Skip to content

Commit 0748c89

Browse files
author
Gauvain Pocentek
committed
Move the mixins in their own module
1 parent 29e0bae commit 0748c89

File tree

3 files changed

+208
-189
lines changed

3 files changed

+208
-189
lines changed

gitlab/base.py

Lines changed: 0 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -641,195 +641,6 @@ def next(self):
641641
return self._obj_cls(self.manager, data)
642642

643643

644-
class GetMixin(object):
645-
def get(self, id, **kwargs):
646-
"""Retrieve a single object.
647-
648-
Args:
649-
id (int or str): ID of the object to retrieve
650-
**kwargs: Extra data to send to the Gitlab server (e.g. sudo)
651-
652-
Returns:
653-
object: The generated RESTObject.
654-
655-
Raises:
656-
GitlabGetError: If the server cannot perform the request.
657-
"""
658-
path = '%s/%s' % (self._path, id)
659-
server_data = self.gitlab.http_get(path, **kwargs)
660-
return self._obj_cls(self, server_data)
661-
662-
663-
class GetWithoutIdMixin(object):
664-
def get(self, **kwargs):
665-
"""Retrieve a single object.
666-
667-
Args:
668-
**kwargs: Extra data to send to the Gitlab server (e.g. sudo)
669-
670-
Returns:
671-
object: The generated RESTObject.
672-
673-
Raises:
674-
GitlabGetError: If the server cannot perform the request.
675-
"""
676-
server_data = self.gitlab.http_get(self._path, **kwargs)
677-
return self._obj_cls(self, server_data)
678-
679-
680-
class ListMixin(object):
681-
def list(self, **kwargs):
682-
"""Retrieves a list of objects.
683-
684-
Args:
685-
**kwargs: Extra data to send to the Gitlab server (e.g. sudo).
686-
If ``all`` is passed and set to True, the entire list of
687-
objects will be returned.
688-
689-
Returns:
690-
RESTObjectList: Generator going through the list of objects, making
691-
queries to the server when required.
692-
If ``all=True`` is passed as argument, returns
693-
list(RESTObjectList).
694-
"""
695-
696-
obj = self.gitlab.http_list(self._path, **kwargs)
697-
if isinstance(obj, list):
698-
return [self._obj_cls(self, item) for item in obj]
699-
else:
700-
return RESTObjectList(self, self._obj_cls, obj)
701-
702-
703-
class GetFromListMixin(ListMixin):
704-
def get(self, id, **kwargs):
705-
"""Retrieve a single object.
706-
707-
Args:
708-
id (int or str): ID of the object to retrieve
709-
**kwargs: Extra data to send to the Gitlab server (e.g. sudo)
710-
711-
Returns:
712-
object: The generated RESTObject.
713-
714-
Raises:
715-
GitlabGetError: If the server cannot perform the request.
716-
"""
717-
gen = self.list()
718-
for obj in gen:
719-
if str(obj.get_id()) == str(id):
720-
return obj
721-
722-
723-
class RetrieveMixin(ListMixin, GetMixin):
724-
pass
725-
726-
727-
class CreateMixin(object):
728-
def _check_missing_attrs(self, data):
729-
required, optional = self.get_create_attrs()
730-
missing = []
731-
for attr in required:
732-
if attr not in data:
733-
missing.append(attr)
734-
continue
735-
if missing:
736-
raise AttributeError("Missing attributes: %s" % ", ".join(missing))
737-
738-
def get_create_attrs(self):
739-
"""Returns the required and optional arguments.
740-
741-
Returns:
742-
tuple: 2 items: list of required arguments and list of optional
743-
arguments for creation (in that order)
744-
"""
745-
if hasattr(self, '_create_attrs'):
746-
return (self._create_attrs['required'],
747-
self._create_attrs['optional'])
748-
return (tuple(), tuple())
749-
750-
def create(self, data, **kwargs):
751-
"""Created a new object.
752-
753-
Args:
754-
data (dict): parameters to send to the server to create the
755-
resource
756-
**kwargs: Extra data to send to the Gitlab server (e.g. sudo)
757-
758-
Returns:
759-
RESTObject: a new instance of the manage object class build with
760-
the data sent by the server
761-
"""
762-
self._check_missing_attrs(data)
763-
if hasattr(self, '_sanitize_data'):
764-
data = self._sanitize_data(data, 'create')
765-
server_data = self.gitlab.http_post(self._path, post_data=data, **kwargs)
766-
return self._obj_cls(self, server_data)
767-
768-
769-
class UpdateMixin(object):
770-
def _check_missing_attrs(self, data):
771-
required, optional = self.get_update_attrs()
772-
missing = []
773-
for attr in required:
774-
if attr not in data:
775-
missing.append(attr)
776-
continue
777-
if missing:
778-
raise AttributeError("Missing attributes: %s" % ", ".join(missing))
779-
780-
def get_update_attrs(self):
781-
"""Returns the required and optional arguments.
782-
783-
Returns:
784-
tuple: 2 items: list of required arguments and list of optional
785-
arguments for update (in that order)
786-
"""
787-
if hasattr(self, '_update_attrs'):
788-
return (self._update_attrs['required'],
789-
self._update_attrs['optional'])
790-
return (tuple(), tuple())
791-
792-
def update(self, id=None, new_data={}, **kwargs):
793-
"""Update an object on the server.
794-
795-
Args:
796-
id: ID of the object to update (can be None if not required)
797-
new_data: the update data for the object
798-
**kwargs: Extra data to send to the Gitlab server (e.g. sudo)
799-
800-
Returns:
801-
dict: The new object data (*not* a RESTObject)
802-
"""
803-
804-
if id is None:
805-
path = self._path
806-
else:
807-
path = '%s/%s' % (self._path, id)
808-
809-
self._check_missing_attrs(new_data)
810-
if hasattr(self, '_sanitize_data'):
811-
data = self._sanitize_data(new_data, 'update')
812-
server_data = self.gitlab.http_put(self._path, post_data=data,
813-
**kwargs)
814-
return server_data
815-
816-
817-
class DeleteMixin(object):
818-
def delete(self, id, **kwargs):
819-
"""Deletes an object on the server.
820-
821-
Args:
822-
id: ID of the object to delete
823-
**kwargs: Extra data to send to the Gitlab server (e.g. sudo)
824-
"""
825-
path = '%s/%s' % (self._path, id)
826-
self.gitlab.http_delete(path, **kwargs)
827-
828-
829-
class CRUDMixin(GetMixin, ListMixin, CreateMixin, UpdateMixin, DeleteMixin):
830-
pass
831-
832-
833644
class RESTManager(object):
834645
"""Base class for CRUD operations on objects.
835646

0 commit comments

Comments
 (0)