From 48ff3a8983467fd86f64636b8751ef2e2beda0f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sun, 23 Mar 2014 15:53:32 +0100 Subject: [PATCH] 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. --- pygit2/__init__.py | 3 +++ pygit2/settings.py | 52 ++++++++++++++++++++++++++++++++++++++++++++ test/test_options.py | 15 +++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 pygit2/settings.py diff --git a/pygit2/__init__.py b/pygit2/__init__.py index 244fdad..8d40fc4 100644 --- a/pygit2/__init__.py +++ b/pygit2/__init__.py @@ -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() diff --git a/pygit2/settings.py b/pygit2/settings.py new file mode 100644 index 0000000..d6e4199 --- /dev/null +++ b/pygit2/settings.py @@ -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) diff --git a/test/test_options.py b/test/test_options.py index e35f520..a6bc22b 100644 --- a/test/test_options.py +++ b/test/test_options.py @@ -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()