Skip to content

Commit 48ff3a8

Browse files
committed
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.
1 parent ab44196 commit 48ff3a8

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

pygit2/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
# High level API
3636
from .repository import Repository
3737
from .version import __version__
38+
from .settings import Settings
3839

3940

4041
def init_repository(path, bare=False):
@@ -75,3 +76,5 @@ def clone_repository(
7576
_pygit2.clone_repository(
7677
url, path, bare, ignore_cert_errors, remote_name, checkout_branch)
7778
return Repository(path)
79+
80+
settings = Settings()

pygit2/settings.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2010-2014 The pygit2 contributors
4+
#
5+
# This file is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License, version 2,
7+
# as published by the Free Software Foundation.
8+
#
9+
# In addition to the permissions in the GNU General Public License,
10+
# the authors give you unlimited permission to link the compiled
11+
# version of this file into combinations with other programs,
12+
# and to distribute those combinations without any restriction
13+
# coming from the use of this file. (The General Public License
14+
# restrictions do apply in other respects; for example, they cover
15+
# modification of the file, and distribution when not linked into
16+
# a combined executable.)
17+
#
18+
# This file is distributed in the hope that it will be useful, but
19+
# WITHOUT ANY WARRANTY; without even the implied warranty of
20+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21+
# General Public License for more details.
22+
#
23+
# You should have received a copy of the GNU General Public License
24+
# along with this program; see the file COPYING. If not, write to
25+
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
26+
# Boston, MA 02110-1301, USA.
27+
28+
from _pygit2 import option
29+
from _pygit2 import GIT_OPT_GET_SEARCH_PATH, GIT_OPT_SET_SEARCH_PATH
30+
from _pygit2 import GIT_OPT_GET_MWINDOW_SIZE, GIT_OPT_SET_MWINDOW_SIZE
31+
32+
class SearchPathList(object):
33+
34+
def __getitem__(self, key):
35+
return option(GIT_OPT_GET_SEARCH_PATH, key)
36+
37+
def __setitem__(self, key, value):
38+
option(GIT_OPT_SET_SEARCH_PATH, key, value)
39+
40+
class Settings(object):
41+
42+
__slots__ = ['mwindow_size', 'search_path']
43+
44+
search_path = SearchPathList()
45+
46+
@property
47+
def mwindow_size(self):
48+
return option(GIT_OPT_GET_MWINDOW_SIZE)
49+
50+
@mwindow_size.setter
51+
def mwindow_size(self, value):
52+
option(GIT_OPT_SET_MWINDOW_SIZE, value)

test/test_options.py

+15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ def test_mwindow_size(self):
4444
option(GIT_OPT_SET_MWINDOW_SIZE, new_size)
4545
self.assertEqual(new_size, option(GIT_OPT_GET_MWINDOW_SIZE))
4646

47+
def test_mwindow_size_proxy(self):
48+
new_size = 300 * 1024
49+
pygit2.settings.mwindow_size = new_size
50+
51+
self.assertEqual(new_size, pygit2.settings.mwindow_size)
52+
4753
def test_search_path(self):
4854
paths = [(GIT_CONFIG_LEVEL_GLOBAL, '/tmp/global'),
4955
(GIT_CONFIG_LEVEL_XDG, '/tmp/xdg'),
@@ -53,5 +59,14 @@ def test_search_path(self):
5359
option(GIT_OPT_SET_SEARCH_PATH, level, path)
5460
self.assertEqual(path, option(GIT_OPT_GET_SEARCH_PATH, level))
5561

62+
def test_search_path_proxy(self):
63+
paths = [(GIT_CONFIG_LEVEL_GLOBAL, '/tmp2/global'),
64+
(GIT_CONFIG_LEVEL_XDG, '/tmp2/xdg'),
65+
(GIT_CONFIG_LEVEL_SYSTEM, '/tmp2/etc')]
66+
67+
for level, path in paths:
68+
pygit2.settings.search_path[level] = path
69+
self.assertEqual(path, pygit2.settings.search_path[level])
70+
5671
if __name__ == '__main__':
5772
unittest.main()

0 commit comments

Comments
 (0)