Repository :
http://git.fedorahosted.org/cgit/copr.git
On branch : python_copr_apiv2
---------------------------------------------------------------
commit e0ca7601cb06ba17cfa11d8739cef20e8bab60ad
Author: Valentin Gologuzov <vgologuz(a)redhat.com>
Date: Wed Sep 30 14:35:20 2015 +0200
[python][api_v2] added support for MockChroot resource
---------------------------------------------------------------
python/copr/client_v2/client.py | 22 +++++++-
python/copr/client_v2/common.py | 13 +++++
python/copr/client_v2/entities.py | 73 ++++++++++++++++++-------
python/copr/client_v2/handlers.py | 45 +++++++++++++++-
python/copr/client_v2/net_client.py | 4 +-
python/copr/client_v2/resources.py | 45 ++++++++++++++-
python/copr/client_v2/schemas.py | 16 +-----
python/copr/test/client_v2/test_net_client.py | 1 +
python/run_tmp.py | 9 +++-
9 files changed, 185 insertions(+), 43 deletions(-)
diff --git a/python/copr/client_v2/client.py b/python/copr/client_v2/client.py
index be05dae..9035d5e 100644
--- a/python/copr/client_v2/client.py
+++ b/python/copr/client_v2/client.py
@@ -20,7 +20,7 @@ from six.moves import configparser
# from requests_toolbelt.multipart.encoder import MultipartEncoder
from .resources import Root
-from .handlers import ProjectHandle, ProjectChrootHandle, BuildHandle
+from .handlers import ProjectHandle, ProjectChrootHandle, BuildHandle, MockChrootHandle
from .common import EntityTypes
from .net_client import NetClient
@@ -68,6 +68,14 @@ class HandlersProvider(with_metaclass(ABCMeta)):
# def build_tasks(self):
# pass
+ @abstractproperty
+ def mock_chroots(self):
+ """
+ :rtype: MockChrootHandle
+ """
+ pass
+
+
class CoprClient(UnicodeMixin, HandlersProvider):
""" Main interface to the copr service
@@ -100,6 +108,7 @@ class CoprClient(UnicodeMixin, HandlersProvider):
self._projects = None
self._project_chroots = None
self._builds = None
+ self._mock_chroots = None
def _check_client_init(self):
if not self._post_init_done:
@@ -121,6 +130,11 @@ class CoprClient(UnicodeMixin, HandlersProvider):
self._check_client_init()
return self._builds
+ @property
+ def mock_chroots(self):
+ self._check_client_init()
+ return self._mock_chroots
+
def __unicode__(self):
return (
u"<Copr client. api root url: {}, config provided: {}, net client:
{}>"
@@ -203,9 +217,15 @@ class CoprClient(UnicodeMixin, HandlersProvider):
projects_href=self.root.get_href_by_name(u"projects"),)
self._project_chroots = ProjectChrootHandle(
weakref.proxy(self), self.nc, root_url=self.root_url)
+
self._builds = BuildHandle(
weakref.proxy(self), self.nc, root_url=self.root_url,
builds_href=self.root.get_href_by_name(u"builds"),)
+ # self._build_chroots = BuildTaskHandle()
+ self._mock_chroots = MockChrootHandle(
+ weakref.proxy(self), self.nc, root_url=self.root_url,
+ href=self.root.get_href_by_name(u"mock_chroots")
+ )
self._post_init_done = True
diff --git a/python/copr/client_v2/common.py b/python/copr/client_v2/common.py
index 4e96bf5..8f64195 100644
--- a/python/copr/client_v2/common.py
+++ b/python/copr/client_v2/common.py
@@ -1,4 +1,7 @@
# coding: utf-8
+from copr.util import UnicodeMixin
+
+
class EntityTypes(object):
ROOT = "root"
PROJECT = "project"
@@ -6,3 +9,13 @@ class EntityTypes(object):
BUILD = "build"
BUILD_TASK = "build_task"
MOCK_CHROOT = "mock_chroot"
+
+
+class BuiltPackage(UnicodeMixin):
+
+ def __init__(self, name, version):
+ self.name = name
+ self.version = version
+
+ def __unicode__(self):
+ return u"{} {}".format(self.name, self.version)
diff --git a/python/copr/client_v2/entities.py b/python/copr/client_v2/entities.py
index 6789872..b39aa12 100644
--- a/python/copr/client_v2/entities.py
+++ b/python/copr/client_v2/entities.py
@@ -1,11 +1,10 @@
# coding: utf-8
from ..util import UnicodeMixin
-from .schemas import ProjectSchema, EmptySchema, ProjectChrootSchema, BuildSchema,
BuildTaskSchema
+from .schemas import ProjectSchema, EmptySchema, ProjectChrootSchema, BuildSchema,
BuildTaskSchema, MockChrootSchema
class Link(UnicodeMixin):
-
def __init__(self, role, href, target_type):
self.role = role
self.href = href
@@ -44,9 +43,12 @@ class Link(UnicodeMixin):
class Entity(UnicodeMixin):
-
_schema = EmptySchema()
+ def __init__(self, **kwargs):
+ for field in self._schema.fields.keys():
+ setattr(self, field, kwargs.get(field))
+
def to_dict(self):
return self._schema.dump(self).data
@@ -54,52 +56,83 @@ class Entity(UnicodeMixin):
return self._schema.dumps(self).data
@classmethod
- def constructor(cls, **kwargs):
- return cls.from_dict(kwargs)
-
- @classmethod
def from_dict(cls, raw_dict):
parsed = cls._schema.load(raw_dict)
- obj = cls()
- for field, value in parsed.data.items():
- setattr(obj, field, value)
- return obj
+ return cls(**parsed.data)
class ProjectEntity(Entity):
-
_schema = ProjectSchema(strict=True)
+ # def __init__(self, **kwargs):
+ # self.id = kwargs["id"]
+ # self.name = kwargs["name"]
+ #
+ # self.owner = kwargs["owner"]
+ # self.description = kwargs.get("description")
+ # self.instructions = kwargs.get("instructions")
+ # self.homepage = kwargs.get("homepage")
+ # self.contact = kwargs.get("contact")
+ #
+ # self.disable_createrepo = kwargs.get("disable_createrepo")
+ # self.build_enable_net = kwargs.get("build_enable_net")
+ # self.last_modified = kwargs.get("last_modified")
+ #
+ # self.repos = kwargs.get("repos", list())
+
def __unicode__(self):
return "<Project #{}: {}/{}>".format(self.id, self.owner,
self.name)
class ProjectChrootEntity(Entity):
-
_schema = ProjectChrootSchema(strict=True)
+ # def __init__(self, **kwargs):
+ # self.name = kwargs["name"]
+ # self.buildroot_pkgs = kwargs.get("buildroot_pkgs", list())
+ #
+ # self.comps = kwargs.get("comps")
+ # self.comps_name = kwargs.get("comps_name")
+ # self.comps_len = kwargs.get("comps_len")
+
def __unicode__(self):
return "<Project chroot: {}, additional " \
- "packages: {}, comps size if any: {}>".format(
- self.name,
- self.buildroot_pkgs,
- self.comps_len,
- )
+ "packages: {}, comps size if any: {}>"\
+ .format(self.name, self.buildroot_pkgs, self.comps_len,)
class BuildEntity(Entity):
-
_schema = BuildSchema(strict=True)
+ # def __init__(self, **kwargs):
+ #
+ # self.id = kwargs["id"]
+ # self.state = kwargs["state"]
+ #
+ # self.submitter = kwargs["submitter"]
+ #
+ # self.built_packages = kwargs["built_packages"]
+ #
+ #
+
def __unicode__(self):
return "<Build #{} state: {}>".format(self.id, self.state)
class BuildTaskEntity(Entity):
-
_schema = BuildTaskSchema(strict=True)
def __unicode__(self):
return "<Build task #{}-{}, state: {}>".format(
self.build_id, self.chroot_name, self.state
)
+
+
+class MockChrootEntity(Entity):
+
+ _schema = MockChrootSchema(strict=True)
+
+ def __unicode__(self):
+ return "<Mock chroot: {} is active: {}>".format(
+ self.name, self.is_active
+ )
diff --git a/python/copr/client_v2/handlers.py b/python/copr/client_v2/handlers.py
index d590f75..03eb7c3 100644
--- a/python/copr/client_v2/handlers.py
+++ b/python/copr/client_v2/handlers.py
@@ -6,7 +6,8 @@ import os
from .common import EntityTypes
from copr.client_v2.net_client import RequestError, MultiPartTuple
from .entities import Link, ProjectChrootEntity
-from .resources import Project, OperationResult, ProjectsList, ProjectChroot,
ProjectChrootList, Build, BuildList
+from .resources import Project, OperationResult, ProjectsList, ProjectChroot,
ProjectChrootList, Build, BuildList, \
+ MockChroot, MockChrootList
class AbstractHandle(object):
@@ -373,7 +374,7 @@ class ProjectChrootHandle(AbstractHandle):
:type project: copr.client_v2.resources.Project
"""
- new_entity = ProjectChrootEntity.constructor(
+ new_entity = ProjectChrootEntity(
name=name,
buildroot_pkgs=buildroot_pkgs or list()
)
@@ -398,3 +399,43 @@ class ProjectChrootHandle(AbstractHandle):
do_auth=True
)
return OperationResult(self, response)
+
+
+class MockChrootHandle(AbstractHandle):
+
+ def __init__(self, client, nc, root_url, href):
+ super(MockChrootHandle, self).__init__(client, nc, root_url)
+ self._href = href
+ self._base_url = "{}{}".format(self.root_url, href)
+
+ def get_base_url(self):
+ return self._base_url
+
+ def get_one(self, name):
+ url = "{}/{}".format(self.get_base_url(), name)
+ response = self.nc.get(url)
+ return MockChroot.from_response(self, response, response.json)
+
+ def get_list(self, active_only=True):
+ options = dict(active_only=active_only)
+
+ response = self.nc.get(
+ self.get_base_url(),
+ query_params=options
+ )
+ data_dict = response.json
+ return MockChrootList(
+ self,
+ response=response,
+ links=Link.from_dict(data_dict["_links"], {
+ "self": EntityTypes.MOCK_CHROOT,
+ }),
+ individuals=[
+ MockChroot.from_response(
+ handle=self,
+ response=None,
+ data_dict=dict_part,
+ )
+ for dict_part in data_dict["chroots"]
+ ]
+ )
diff --git a/python/copr/client_v2/net_client.py b/python/copr/client_v2/net_client.py
index 676d4f6..74b66b0 100644
--- a/python/copr/client_v2/net_client.py
+++ b/python/copr/client_v2/net_client.py
@@ -24,9 +24,9 @@ class RequestError(Exception, UnicodeMixin):
@property
def response_json(self):
- if self.response.headers["content-type"] ==
"application/json":
- if self.response is None:
+ if self.response is None:
raise ValueError("No response")
+ if self.response.headers["content-type"] ==
"application/json":
try:
result = json.loads(self.response.text)
except (ValueError, AttributeError):
diff --git a/python/copr/client_v2/resources.py b/python/copr/client_v2/resources.py
index ac0537d..fa40551 100644
--- a/python/copr/client_v2/resources.py
+++ b/python/copr/client_v2/resources.py
@@ -4,7 +4,7 @@ from collections import Iterable
from ..util import UnicodeMixin
from .common import EntityTypes
-from .entities import Link, ProjectEntity, ProjectChrootEntity, BuildEntity
+from .entities import Link, ProjectEntity, ProjectChrootEntity, BuildEntity,
MockChrootEntity
from .schemas import EmptySchema
@@ -143,7 +143,7 @@ class Project(IndividualResource):
See additional options `:py:method:BuildHandle.create_from_file:`
"""
builds = self._handle.get_builds_handle()
- builds.create_from_file(self.id, *args, **kwargs)
+ return builds.create_from_file(self.id, *args, **kwargs)
@classmethod
def from_response(cls, handle, response, data_dict, options=None):
@@ -186,6 +186,29 @@ class ProjectChroot(IndividualResource):
return self._handle.update(self._project, self._entity)
+class MockChroot(IndividualResource):
+ """
+ :type entity: copr.client_v2.entities.MockChrootEntity
+ :type handle: copr.client_v2.handlers.MockChrootHandle
+ """
+
+ def __init__(self, entity, handle, **kwargs):
+ super(MockChroot, self).__init__(
+ entity=entity,
+ handle=handle,
+ **kwargs
+ )
+
+ @classmethod
+ def from_response(cls, handle, response, data_dict, options=None):
+ links = Link.from_dict(data_dict["_links"], {
+ "self": EntityTypes.MOCK_CHROOT,
+ })
+ entity = MockChrootEntity.from_dict(data_dict["chroot"])
+ return cls(entity=entity, handle=handle,
+ response=response, links=links, options=options)
+
+
class OperationResult(IndividualResource):
# TODO: app param expected_status=200 and method is_successful() which would compare
@@ -256,6 +279,8 @@ class CollectionResource(Iterable, UnicodeMixin):
"""
return iter(self._individuals)
+ # todo: add classmethod from response
+
class ProjectsList(CollectionResource):
"""
@@ -286,7 +311,7 @@ class BuildList(CollectionResource):
class ProjectChrootList(CollectionResource):
"""
- :type handle: coprclient_v2.handlers.ProjectChrootHandle
+ :type handle: copr.client_v2.handlers.ProjectChrootHandle
"""
def __init__(self, handle, project, **kwargs):
@@ -300,3 +325,17 @@ class ProjectChrootList(CollectionResource):
def enable(self, name):
return self._handle.enable(self._project, name)
+
+
+class MockChrootList(CollectionResource):
+ """
+ :type handle: copr.client_v2.handlers.MockChrootHandle
+ """
+
+ def __init__(self, handle, **kwargs):
+ super(MockChrootList, self).__init__(**kwargs)
+ self._handle = handle
+
+ @property
+ def chroots(self):
+ return self._individuals
diff --git a/python/copr/client_v2/schemas.py b/python/copr/client_v2/schemas.py
index c4c780c..68ca2b4 100644
--- a/python/copr/client_v2/schemas.py
+++ b/python/copr/client_v2/schemas.py
@@ -1,14 +1,12 @@
# coding: utf-8
-from collections import Iterable
-from marshmallow import Schema, fields, post_load
-from marshmallow import Schema, fields, validates_schema, ValidationError, validate
+from marshmallow import Schema, fields
# todo: maybe define schemas for Link sets in each indvidual/collection
# class LinkSchema(Schema):
# href = fields.Str()
#
-from copr.util import UnicodeMixin
+from .common import BuiltPackage
class EmptySchema(Schema):
@@ -43,16 +41,6 @@ class ProjectChrootSchema(Schema):
comps_len = fields.Int(load_only=True, allow_none=True)
-class BuiltPackage(UnicodeMixin):
-
- def __init__(self, name, version):
- self.name = name
- self.version = version
-
- def __unicode__(self):
- return u"{} {}".format(self.name, self.version)
-
-
class BuiltPackageSchema(Schema):
name = fields.Str()
diff --git a/python/copr/test/client_v2/test_net_client.py
b/python/copr/test/client_v2/test_net_client.py
index ba9113f..7cca53a 100644
--- a/python/copr/test/client_v2/test_net_client.py
+++ b/python/copr/test/client_v2/test_net_client.py
@@ -66,4 +66,5 @@ class TestNetClient(object):
# some coverage for Request error
s = str(exc_info.value)
with pytest.raises(ValueError):
+ # import ipdb; ipdb.set_trace()
x = exc_info.value.response_json
diff --git a/python/run_tmp.py b/python/run_tmp.py
index 6347eb8..cc630b1 100644
--- a/python/run_tmp.py
+++ b/python/run_tmp.py
@@ -124,8 +124,15 @@ def main():
b = b.get_self()
print(b)
- t8()
+ # t8()
+ def t9():
+
+ mcl = client.mock_chroots.get_list(active_only=False)
+ for mc in mcl:
+ print(mc)
+
+ t9()
if __name__ == "__main__":
try: