Allow setting credentials and certificate in callback ctor

This allows for a less verbose way of setting one-liners as these
callbacks.
This commit is contained in:
Carlos Martín Nieto
2015-09-26 22:42:19 +02:00
parent ab97c08f72
commit ac2e363d04
2 changed files with 16 additions and 5 deletions

View File

@@ -78,6 +78,21 @@ class RemoteCallbacks(object):
in your class, which you can then pass to the network operations.
"""
def __init__(self, credentials=None, certificate=None):
"""Initialize some callbacks in-line
Use this constructor to provide credentials and certificate
callbacks in-line, instead of defining your own class for these ones.
You can e.g. also pass in one of the credential objects as 'credentials'
instead of creating a function which returns a hard-coded object.
"""
if credentials is not None:
self.credentials = credentials
if certificate is not None:
self.certificate = certificate
def sideband_progress(self, string):
"""Progress output callback

View File

@@ -514,13 +514,9 @@ class CloneRepositoryTest(utils.NoRepoTestCase):
self.assertIsNotNone(repo.remotes["custom_remote"])
def test_clone_with_credentials(self):
class MyCallbacks(pygit2.RemoteCallbacks):
def __init__(self):
self.credentials = pygit2.UserPass("libgit2", "libgit2")
repo = clone_repository(
"https://bitbucket.org/libgit2/testgitrepository.git",
self._temp_dir, callbacks=MyCallbacks())
self._temp_dir, callbacks=pygit2.RemoteCallbacks(credentials=pygit2.UserPass("libgit2", "libgit2")))
self.assertFalse(repo.is_empty)