Create proxy object for libgit2 options

pygit2.settings proxies though the lower-level varargs option() via
getters and setters which provide a more natural abstraction.
This commit is contained in:
Carlos Martín Nieto
2014-03-23 15:53:32 +01:00
parent ab44196730
commit 48ff3a8983
3 changed files with 70 additions and 0 deletions

View File

@@ -35,6 +35,7 @@ from _pygit2 import *
# High level API
from .repository import Repository
from .version import __version__
from .settings import Settings
def init_repository(path, bare=False):
@@ -75,3 +76,5 @@ def clone_repository(
_pygit2.clone_repository(
url, path, bare, ignore_cert_errors, remote_name, checkout_branch)
return Repository(path)
settings = Settings()

52
pygit2/settings.py Normal file
View File

@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
#
# Copyright 2010-2014 The pygit2 contributors
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2,
# as published by the Free Software Foundation.
#
# In addition to the permissions in the GNU General Public License,
# the authors give you unlimited permission to link the compiled
# version of this file into combinations with other programs,
# and to distribute those combinations without any restriction
# coming from the use of this file. (The General Public License
# restrictions do apply in other respects; for example, they cover
# modification of the file, and distribution when not linked into
# a combined executable.)
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
from _pygit2 import option
from _pygit2 import GIT_OPT_GET_SEARCH_PATH, GIT_OPT_SET_SEARCH_PATH
from _pygit2 import GIT_OPT_GET_MWINDOW_SIZE, GIT_OPT_SET_MWINDOW_SIZE
class SearchPathList(object):
def __getitem__(self, key):
return option(GIT_OPT_GET_SEARCH_PATH, key)
def __setitem__(self, key, value):
option(GIT_OPT_SET_SEARCH_PATH, key, value)
class Settings(object):
__slots__ = ['mwindow_size', 'search_path']
search_path = SearchPathList()
@property
def mwindow_size(self):
return option(GIT_OPT_GET_MWINDOW_SIZE)
@mwindow_size.setter
def mwindow_size(self, value):
option(GIT_OPT_SET_MWINDOW_SIZE, value)

View File

@@ -44,6 +44,12 @@ class OptionsTest(utils.NoRepoTestCase):
option(GIT_OPT_SET_MWINDOW_SIZE, new_size)
self.assertEqual(new_size, option(GIT_OPT_GET_MWINDOW_SIZE))
def test_mwindow_size_proxy(self):
new_size = 300 * 1024
pygit2.settings.mwindow_size = new_size
self.assertEqual(new_size, pygit2.settings.mwindow_size)
def test_search_path(self):
paths = [(GIT_CONFIG_LEVEL_GLOBAL, '/tmp/global'),
(GIT_CONFIG_LEVEL_XDG, '/tmp/xdg'),
@@ -53,5 +59,14 @@ class OptionsTest(utils.NoRepoTestCase):
option(GIT_OPT_SET_SEARCH_PATH, level, path)
self.assertEqual(path, option(GIT_OPT_GET_SEARCH_PATH, level))
def test_search_path_proxy(self):
paths = [(GIT_CONFIG_LEVEL_GLOBAL, '/tmp2/global'),
(GIT_CONFIG_LEVEL_XDG, '/tmp2/xdg'),
(GIT_CONFIG_LEVEL_SYSTEM, '/tmp2/etc')]
for level, path in paths:
pygit2.settings.search_path[level] = path
self.assertEqual(path, pygit2.settings.search_path[level])
if __name__ == '__main__':
unittest.main()