Create Config type

This commit is contained in:
Martin Lenders
2012-06-08 12:17:37 +02:00
parent bffd195907
commit c4d17b759b
6 changed files with 165 additions and 2 deletions

8
include/pygit2/config.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef INCLUDE_pygit2_config_h
#define INCLUDE_pygit2_config_h
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <git2.h>
#endif

View File

@@ -30,6 +30,7 @@ OBJECT_STRUCT(Tag, git_tag, tag)
OBJECT_STRUCT(Index, git_index, index)
OBJECT_STRUCT(Walker, git_revwalk, walk)
OBJECT_STRUCT(Diff, git_diff_list, diff)
OBJECT_STRUCT(Config, git_config, config)
typedef struct {
PyObject_HEAD

View File

@@ -52,6 +52,7 @@ extern PyTypeObject IndexType;
extern PyTypeObject IndexEntryType;
extern PyTypeObject IndexIterType;
extern PyTypeObject WalkerType;
extern PyTypeObject ConfigType;
extern PyTypeObject ReferenceType;
extern PyTypeObject RefLogIterType;
extern PyTypeObject RefLogEntryType;
@@ -160,6 +161,9 @@ moduleinit(PyObject* m)
TreeBuilderType.tp_new = PyType_GenericNew;
if (PyType_Ready(&TreeBuilderType) < 0)
return NULL;
ConfigType.tp_new = PyType_GenericNew;
if (PyType_Ready(&ConfigType) < 0)
return NULL;
WalkerType.tp_new = PyType_GenericNew;
if (PyType_Ready(&WalkerType) < 0)
return NULL;
@@ -190,6 +194,9 @@ moduleinit(PyObject* m)
Py_INCREF(&TreeType);
PyModule_AddObject(m, "Tree", (PyObject *)&TreeType);
Py_INCREF(&ConfigType);
PyModule_AddObject(m, "Config", (PyObject *)&ConfigType);
Py_INCREF(&BlobType);
PyModule_AddObject(m, "Blob", (PyObject *)&BlobType);

97
src/pygit2/config.c Normal file
View File

@@ -0,0 +1,97 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <pygit2/error.h>
#include <pygit2/types.h>
#include <pygit2/utils.h>
#include <pygit2/config.h>
extern PyTypeObject ConfigType;
int
Config_init(Config *self, PyObject *args, PyObject *kwds)
{
char *path;
int err;
if (kwds) {
PyErr_SetString(PyExc_TypeError,
"Repository takes no keyword arguments");
return -1;
}
if (PySequence_Length(args) > 0) {
if (!PyArg_ParseTuple(args, "s", &path)) {
return -1;
}
err = git_config_open_ondisk(&self->config, path);
if (err < 0) {
Error_set_str(err, path);
return -1;
}
} else {
err = git_config_new(&self->config);
if (err < 0) {
Error_set(err);
return -1;
}
}
return 0;
}
void
Config_dealloc(Config *self)
{
PyObject_GC_UnTrack(self);
Py_XDECREF(self->repo);
git_config_free(self->config);
PyObject_GC_Del(self);
}
int
Config_traverse(Config *self, visitproc visit, void *arg)
{
Py_VISIT(self->repo);
return 0;
}
PyTypeObject ConfigType = {
PyVarObject_HEAD_INIT(NULL, 0)
"_pygit2.Config", /* tp_name */
sizeof(Config), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)Config_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT |
Py_TPFLAGS_HAVE_GC, /* tp_flags */
"Configuration management", /* tp_doc */
(traverseproc)Config_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)Config_init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};

View File

@@ -35,8 +35,8 @@ import sys
import unittest
names = ['blob', 'commit', 'index', 'refs', 'repository', 'revwalk', 'tag',
'tree', 'signature', 'status', 'treebuilder', 'diff']
names = ['blob', 'commit', 'config', 'index', 'refs', 'repository', 'revwalk',
'tag', 'tree', 'signature', 'status', 'treebuilder']
def test_suite():
modules = ['test.test_%s' % n for n in names]
return unittest.defaultTestLoader.loadTestsFromNames(modules)

50
test/test_config.py Normal file
View File

@@ -0,0 +1,50 @@
# -*- coding: UTF-8 -*-
#
# Copyright 2012 elego
#
# 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.
"""Tests for Index files."""
import os
import unittest
import pygit2
from . import utils
__author__ = 'mlenders@elegosoft.com (M. Lenders)'
config_filename = "test_config"
class ConfigTest(utils.RepoTestCase):
def test_new(self):
config_write = pygit2.Config(config_filename)
self.assertNotEqual(config_write, None)
if __name__ == '__main__':
unittest.main()