Repository :
http://git.fedorahosted.org/cgit/copr.git
On branch : master
---------------------------------------------------------------
commit db3e8e3ee379542a0298cef25d949863df1ead9d
Author: Valentin Gologuzov <vgologuz(a)redhat.com>
Date: Tue Nov 4 16:54:37 2014 +0100
[python] fixed poor decision abou CoprClient constructor, now it accepts kwargs
arguments instead of config dict
---------------------------------------------------------------
cli/copr_cli/main.py | 8 ++++----
cli/tests/test_cli.py | 4 ++--
python/copr/README.rst | 12 ++++++------
python/copr/client/client.py | 25 +++++++++++++++----------
python/copr/test/test_client.py | 4 ++--
python/docs/Examples.rst | 7 +++++++
6 files changed, 36 insertions(+), 24 deletions(-)
diff --git a/cli/copr_cli/main.py b/cli/copr_cli/main.py
index 4752905..3998fa3 100644
--- a/cli/copr_cli/main.py
+++ b/cli/copr_cli/main.py
@@ -41,10 +41,10 @@ class Commands(object):
except (copr_exceptions.CoprNoConfException,
copr_exceptions.CoprConfigException):
print(no_config_warning)
- self.client = CoprClient({
- "copr_url": "http://copr.fedoraproject.org",
- "no_config": True
- })
+ self.client = CoprClient(
+ copr_url=u"http://copr.fedoraproject.org",
+ no_config=True
+ )
def requires_api_auth(func):
""" Decorator that checks config presence
diff --git a/cli/tests/test_cli.py b/cli/tests/test_cli.py
index e9b7e20..d9bed34 100644
--- a/cli/tests/test_cli.py
+++ b/cli/tests/test_cli.py
@@ -199,7 +199,7 @@ def test_list_project(mock_cc, capsys):
# no config
mock_cc.create_from_file_config.side_effect = CoprNoConfException()
- mocked_client = MagicMock(CoprClient(dict(no_config=True)))
+ mocked_client = MagicMock(CoprClient(no_config=True))
control_response = CoprResponse(client=None, method="",
data=response_data,
parsers=[ProjectListParser,
CommonMsgErrorOutParser])
@@ -229,7 +229,7 @@ def test_list_project_no_username(mock_cc, capsys):
@mock.patch('copr_cli.main.CoprClient')
def test_list_project_no_username2(mock_cc, capsys):
- mock_cc.create_from_file_config.return_value = CoprClient(defaultdict())
+ mock_cc.create_from_file_config.return_value = CoprClient()
with pytest.raises(SystemExit) as err:
main.main(argv=["list"])
diff --git a/python/copr/README.rst b/python/copr/README.rst
index c624ebb..8b3f708 100644
--- a/python/copr/README.rst
+++ b/python/copr/README.rst
@@ -26,12 +26,12 @@ Usage:
::
from python_copr.main import CoprClient
- client = CoprClient({
- "login": "<login from /api>",
- "token": "<token from /api>",
- "username": "<copr username>",
- "copr_url": "<url copr instance ; optional>"
- })
+ client = CoprClient(
+ login="<login from /api>",
+ token="<token from /api>",
+ username="<copr username>",
+ copr_url="<url copr instance ; optional>"
+ )
Alternatively you could use configuration file:
- Create the file ``~/.config/copr``
diff --git a/python/copr/client/client.py b/python/copr/client/client.py
index ad59f76..8f7824f 100644
--- a/python/copr/client/client.py
+++ b/python/copr/client/client.py
@@ -56,22 +56,27 @@ class CoprClient(object):
:ivar unicode copr_url: used as copr projects root
Could be created:
- - directly by providing a `dict` with credentionals
+ - directly
- using static method :py:meth:`CoprClient.create_from_file_config`
"""
- def __init__(self, config=None):
+ def __init__(self, username=None, login=None, token=None, copr_url=None,
+ no_config=False):
"""
- :param dict config: Dictionary with client configuration.
+ :param unicode username: username used by default for all requests
+ :param unicode login: user login, used for identification
+ :param unicode token: copr api token
+ :param unicode copr_url: used as copr projects root
+ :param bool no_config: helper flag to indicate that no config was provided
"""
- self.token = config.get("token")
- self.login = config.get("login")
- self.username = config.get("username")
- self.copr_url = config.get("copr_url",
"http://copr.fedoraproject.org/")
+ self.token = token
+ self.login = login
+ self.username = username
+ self.copr_url = copr_url or "http://copr.fedoraproject.org/"
- self.no_config = config.get("no_config", False)
+ self.no_config = no_config
def __str__(self):
return "<Copr client. username: {0}, api url: {1}, " \
@@ -109,7 +114,7 @@ class CoprClient(object):
log.warning(
"No configuration file '~/.config/copr' found. "
"See man copr-cli for more information")
-
+ config["no_config"] = True
if not ignore_error:
raise CoprNoConfException()
else:
@@ -124,7 +129,7 @@ class CoprClient(object):
if not ignore_error:
raise CoprConfigException(
"Bad configuration file: {0}".format(err))
- return CoprClient(config=config)
+ return CoprClient(**config)
def _fetch(self, url, data=None, projectname=None, username=None,
method=None, skip_auth=False, on_error_response=None):
diff --git a/python/copr/test/test_client.py b/python/copr/test/test_client.py
index 6d73f97..642f2f9 100644
--- a/python/copr/test/test_client.py
+++ b/python/copr/test/test_client.py
@@ -13,12 +13,12 @@ config_location = os.path.join(resource_location,
"copr_cli.conf")
def test_client_from_dict():
- cl = CoprClient(dict(
+ cl = CoprClient(
login="api-login",
username="user_name",
token="api-token",
copr_url="http://copr-fe-dev.cloud.fedoraproject.org"
- ))
+ )
assert isinstance(cl, CoprClient)
assert cl.login == "api-login"
diff --git a/python/docs/Examples.rst b/python/docs/Examples.rst
index 540ca2b..37171e2 100644
--- a/python/docs/Examples.rst
+++ b/python/docs/Examples.rst
@@ -3,8 +3,15 @@ Create client
::
from copr.client import CoprClient
+
+ # using default config location ~/.config/copr
cl = CoprClient.create_from_file_config()
+ # using config at ~/.config/alt_copr
+ cl = CoprClient.create_from_file_config("~/.config/alt_copr")
+
+ # directly
+ cl = CoprClient(username="foo", login="abcd",
token="efgk",
copr_url="http://example.com/")
Get projects list
-----------------