Implement getting and setting of multivars
This commit is contained in:
@@ -10,6 +10,8 @@ PyObject* Config_get_system_config(void);
|
|||||||
PyObject* Config_add_file(Config *self, PyObject *args);
|
PyObject* Config_add_file(Config *self, PyObject *args);
|
||||||
PyObject* Config_getitem(Config *self, PyObject *key);
|
PyObject* Config_getitem(Config *self, PyObject *key);
|
||||||
PyObject* Config_foreach(Config *self, PyObject *args);
|
PyObject* Config_foreach(Config *self, PyObject *args);
|
||||||
|
PyObject* Config_get_multivar(Config *self, PyObject *args);
|
||||||
|
PyObject* Config_set_multivar(Config *self, PyObject *args);
|
||||||
int Config_setitem(Config *self, PyObject *key, PyObject *value);
|
int Config_setitem(Config *self, PyObject *key, PyObject *value);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -255,6 +255,64 @@ Config_add_file(Config *self, PyObject *args)
|
|||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
Config_get_multivar_fn_wrapper(const char *value, void *data)
|
||||||
|
{
|
||||||
|
PyObject *list = (PyObject *)data;
|
||||||
|
PyObject *item = NULL;
|
||||||
|
|
||||||
|
if (!(item = PyUnicode_FromString(value)))
|
||||||
|
return -2;
|
||||||
|
|
||||||
|
PyList_Append(list, item);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
PyObject *
|
||||||
|
Config_get_multivar(Config *self, PyObject *args)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
PyObject *list = PyList_New(0);
|
||||||
|
const char *name = NULL;
|
||||||
|
const char *regex = NULL;
|
||||||
|
|
||||||
|
if (!PyArg_ParseTuple(args, "s|s", &name, ®ex))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if ((err = git_config_get_multivar(self->config, name, regex,
|
||||||
|
Config_get_multivar_fn_wrapper, (void *)list)) < 0) {
|
||||||
|
if (err == GIT_ENOTFOUND)
|
||||||
|
Error_set(err);
|
||||||
|
else
|
||||||
|
PyErr_SetNone(PyExc_TypeError);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
PyObject *
|
||||||
|
Config_set_multivar(Config *self, PyObject *args)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
const char *name;
|
||||||
|
const char *regex;
|
||||||
|
const char *value;
|
||||||
|
|
||||||
|
if (!PyArg_ParseTuple(args, "sss", &name, ®ex, &value))
|
||||||
|
return NULL;
|
||||||
|
if ((err = git_config_set_multivar(self->config, name, regex, value)) < 0) {
|
||||||
|
if (err == GIT_ENOTFOUND)
|
||||||
|
Error_set(err);
|
||||||
|
else
|
||||||
|
PyErr_SetNone(PyExc_TypeError);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
Py_RETURN_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
PyMethodDef Config_methods[] = {
|
PyMethodDef Config_methods[] = {
|
||||||
{"get_system_config", (PyCFunction)Config_get_system_config,
|
{"get_system_config", (PyCFunction)Config_get_system_config,
|
||||||
METH_NOARGS | METH_STATIC,
|
METH_NOARGS | METH_STATIC,
|
||||||
@@ -270,6 +328,13 @@ PyMethodDef Config_methods[] = {
|
|||||||
"an integer other than 0, this function returns that value."},
|
"an integer other than 0, this function returns that value."},
|
||||||
{"add_file", (PyCFunction)Config_add_file, METH_VARARGS,
|
{"add_file", (PyCFunction)Config_add_file, METH_VARARGS,
|
||||||
"Add a config file instance to an existing config."},
|
"Add a config file instance to an existing config."},
|
||||||
|
{"get_multivar", (PyCFunction)Config_get_multivar, METH_VARARGS,
|
||||||
|
"Get each value of a multivar ''name'' as a list. The optional ''regex'' "
|
||||||
|
"parameter is expected to be a regular expression to filter the which "
|
||||||
|
"variables we're interested in."},
|
||||||
|
{"set_multivar", (PyCFunction)Config_set_multivar, METH_VARARGS,
|
||||||
|
"Set a multivar ''name'' to ''value''. ''regexp'' is a regular expression "
|
||||||
|
"to indicate which values to replace"},
|
||||||
{NULL}
|
{NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -111,6 +111,13 @@ class ConfigTest(utils.RepoTestCase):
|
|||||||
new_file.write("[this]\n\tthat = foobar\n\tthat = foobeer\n")
|
new_file.write("[this]\n\tthat = foobar\n\tthat = foobeer\n")
|
||||||
new_file.close()
|
new_file.close()
|
||||||
|
|
||||||
|
config.add_file(config_filename, 0)
|
||||||
|
self.assertTrue('this.that' in config)
|
||||||
|
self.assertEqual(len(config.get_multivar('this.that')), 2)
|
||||||
|
l = config.get_multivar('this.that', 'bar')
|
||||||
|
self.assertEqual(len(l),1)
|
||||||
|
self.assertEqual(l[0], 'foobar')
|
||||||
|
|
||||||
def test_write(self):
|
def test_write(self):
|
||||||
config = self.repo.config
|
config = self.repo.config
|
||||||
|
|
||||||
@@ -139,6 +146,21 @@ class ConfigTest(utils.RepoTestCase):
|
|||||||
del config['core.dummy3']
|
del config['core.dummy3']
|
||||||
self.assertFalse('core.dummy3' in config)
|
self.assertFalse('core.dummy3' in config)
|
||||||
|
|
||||||
|
new_file = open(config_filename, "w")
|
||||||
|
new_file.write("[this]\n\tthat = foobar\n\tthat = foobeer\n")
|
||||||
|
new_file.close()
|
||||||
|
|
||||||
|
config.add_file(config_filename, 0)
|
||||||
|
self.assertTrue('this.that' in config)
|
||||||
|
config.set_multivar('this.that', '^.*beer', 'fool')
|
||||||
|
l = config.get_multivar('this.that', 'fool')
|
||||||
|
self.assertEqual(len(l),1)
|
||||||
|
self.assertEqual(l[0], 'fool')
|
||||||
|
config.set_multivar('this.that', 'foo.*', '123456')
|
||||||
|
l = config.get_multivar('this.that', 'foo.*')
|
||||||
|
for i in l:
|
||||||
|
self.assertEqual(i, '123456')
|
||||||
|
|
||||||
def test_foreach(self):
|
def test_foreach(self):
|
||||||
config = self.repo.config
|
config = self.repo.config
|
||||||
lst = {}
|
lst = {}
|
||||||
|
Reference in New Issue
Block a user