From ac2e363d043eef1631a83cb03d7a0115a6e9c9af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sat, 26 Sep 2015 22:42:19 +0200 Subject: [PATCH] Allow setting credentials and certificate in callback ctor This allows for a less verbose way of setting one-liners as these callbacks. --- pygit2/remote.py | 15 +++++++++++++++ test/test_repository.py | 6 +----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pygit2/remote.py b/pygit2/remote.py index c38dea0..a30cf27 100644 --- a/pygit2/remote.py +++ b/pygit2/remote.py @@ -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 diff --git a/test/test_repository.py b/test/test_repository.py index 810e275..c12c822 100644 --- a/test/test_repository.py +++ b/test/test_repository.py @@ -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)