summaryrefslogtreecommitdiff
path: root/python/configuration.cc
diff options
context:
space:
mode:
Diffstat (limited to 'python/configuration.cc')
-rw-r--r--python/configuration.cc177
1 files changed, 73 insertions, 104 deletions
diff --git a/python/configuration.cc b/python/configuration.cc
index 21f70bc1..299e06ec 100644
--- a/python/configuration.cc
+++ b/python/configuration.cc
@@ -5,11 +5,8 @@
Configuration - Binding for the configuration object.
- There are three seperate classes..
- Configuration - A stand alone configuration instance
- ConfigurationPtr - A pointer to a configuration instance, used only
- for the global instance (_config)
- ConfigurationSub - A subtree - has a reference to its owner.
+ The Configuration object can have an owner (a parent Configuration object),
+ and it always uses a pointer.
The wrapping is mostly 1:1 with the C++ code, but there are additions to
wrap the linked tree walking into nice flat sequence walking.
@@ -25,33 +22,13 @@
#include <Python.h>
/*}}}*/
-/* If we create a sub tree then it is of this type, the Owner is used
- to manage reference counting. */
-struct SubConfiguration : public CppPyObject<Configuration>
-{
- PyObject *Owner;
-};
-
- /*}}}*/
-// CnfSubFree - Free a sub configuration /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-void CnfSubFree(PyObject *Obj)
-{
- SubConfiguration *Self = (SubConfiguration *)Obj;
- Py_DECREF(Self->Owner);
- CppDealloc<Configuration>(Obj);
-}
- /*}}}*/
// GetSelf - Convert PyObject to Configuration /*{{{*/
// ---------------------------------------------------------------------
/* */
static inline Configuration &GetSelf(PyObject *Obj)
{
- if (Obj->ob_type == &ConfigurationPtrType)
- return *GetCpp<Configuration *>(Obj);
- return GetCpp<Configuration>(Obj);
+ return *GetCpp<Configuration*>(Obj);
}
/*}}}*/
@@ -128,6 +105,11 @@ static PyObject *CnfExists(PyObject *Self,PyObject *Args)
return Py_BuildValue("i",(int)GetSelf(Self).Exists(Name));
}
+static int CnfContains(PyObject *Self,PyObject *Arg)
+{
+ return (int)GetSelf(Self).Exists(PyString_AsString(Arg));
+}
+
static char *doc_Clear = "Clear(Name) -> None";
static PyObject *CnfClear(PyObject *Self,PyObject *Args)
{
@@ -155,12 +137,8 @@ static PyObject *CnfSubTree(PyObject *Self,PyObject *Args)
return 0;
}
- // Create a new sub configuration.
- SubConfiguration *New = PyObject_NEW(SubConfiguration,&ConfigurationSubType);
- new (&New->Object) Configuration(Itm);
- New->Owner = Self;
- Py_INCREF(Self);
- return New;
+ return CppPyObject_NEW<Configuration*>(Self,&PyConfiguration_Type,
+ new Configuration(Itm));
}
// Return a list of items at a specific level
@@ -174,6 +152,8 @@ static PyObject *CnfList(PyObject *Self,PyObject *Args)
// Convert the whole configuration space into a list
PyObject *List = PyList_New(0);
const Configuration::Item *Top = GetSelf(Self).Tree(RootName);
+ if (!GetSelf(Self).Tree(0))
+ return List;
const Configuration::Item *Root = GetSelf(Self).Tree(0)->Parent;
if (Top != 0 && RootName != 0)
Top = Top->Child;
@@ -238,7 +218,7 @@ static PyObject *CnfKeys(PyObject *Self,PyObject *Args)
const Configuration::Item *Root = 0;
if (RootName == 0)
Stop = 0;
- if (Top != 0)
+ if (Top != 0 && GetSelf(Self).Tree(0))
Root = GetSelf(Self).Tree(0)->Parent;
for (; Top != 0;)
{
@@ -300,7 +280,7 @@ PyObject *LoadConfig(PyObject *Self,PyObject *Args)
char *Name = 0;
if (PyArg_ParseTuple(Args,"Os",&Self,&Name) == 0)
return 0;
- if (Configuration_Check(Self)== 0)
+ if (PyConfiguration_Check(Self)== 0)
{
PyErr_SetString(PyExc_TypeError,"argument 1: expected Configuration.");
return 0;
@@ -318,7 +298,7 @@ PyObject *LoadConfigISC(PyObject *Self,PyObject *Args)
char *Name = 0;
if (PyArg_ParseTuple(Args,"Os",&Self,&Name) == 0)
return 0;
- if (Configuration_Check(Self)== 0)
+ if (PyConfiguration_Check(Self)== 0)
{
PyErr_SetString(PyExc_TypeError,"argument 1: expected Configuration.");
return 0;
@@ -336,7 +316,7 @@ PyObject *LoadConfigDir(PyObject *Self,PyObject *Args)
char *Name = 0;
if (PyArg_ParseTuple(Args,"Os",&Self,&Name) == 0)
return 0;
- if (Configuration_Check(Self)== 0)
+ if (PyConfiguration_Check(Self)== 0)
{
PyErr_SetString(PyExc_TypeError,"argument 1: expected Configuration.");
return 0;
@@ -368,7 +348,7 @@ PyObject *ParseCommandLine(PyObject *Self,PyObject *Args)
if (PyArg_ParseTuple(Args,"OO!O!",&Self,
&PyList_Type,&POList,&PyList_Type,&Pargv) == 0)
return 0;
- if (Configuration_Check(Self)== 0)
+ if (PyConfiguration_Check(Self)== 0)
{
PyErr_SetString(PyExc_TypeError,"argument 1: expected Configuration.");
return 0;
@@ -383,7 +363,11 @@ PyObject *ParseCommandLine(PyObject *Self,PyObject *Args)
for (int I = 0; I != Length; I++)
{
char *Type = 0;
+ #if PY_MAJOR_VERSION >= 3
+ if (PyArg_ParseTuple(PySequence_GetItem(POList,I),"Czs|s",
+ #else
if (PyArg_ParseTuple(PySequence_GetItem(POList,I),"czs|s",
+ #endif
&OList[I].ShortOpt,&OList[I].LongOpt,
&OList[I].ConfName,&Type) == 0)
{
@@ -448,95 +432,80 @@ PyObject *ParseCommandLine(PyObject *Self,PyObject *Args)
static PyMethodDef CnfMethods[] =
{
// Query
- {"Find",CnfFind,METH_VARARGS,doc_Find},
- {"FindFile",CnfFindFile,METH_VARARGS,doc_FindFile},
- {"FindDir",CnfFindDir,METH_VARARGS,doc_FindDir},
- {"FindI",CnfFindI,METH_VARARGS,doc_FindI},
- {"FindB",CnfFindB,METH_VARARGS,doc_FindB},
+ {"find",CnfFind,METH_VARARGS,doc_Find},
+ {"find_file",CnfFindFile,METH_VARARGS,doc_FindFile},
+ {"find_dir",CnfFindDir,METH_VARARGS,doc_FindDir},
+ {"find_i",CnfFindI,METH_VARARGS,doc_FindI},
+ {"find_b",CnfFindB,METH_VARARGS,doc_FindB},
// Others
- {"Set",CnfSet,METH_VARARGS,doc_Set},
- {"Exists",CnfExists,METH_VARARGS,doc_Exists},
- {"SubTree",CnfSubTree,METH_VARARGS,doc_SubTree},
- {"List",CnfList,METH_VARARGS,doc_List},
- {"ValueList",CnfValueList,METH_VARARGS,doc_ValueList},
- {"MyTag",CnfMyTag,METH_VARARGS,doc_MyTag},
- {"Clear",CnfClear,METH_VARARGS,doc_Clear},
-
+ {"set",CnfSet,METH_VARARGS,doc_Set},
+ {"exists",CnfExists,METH_VARARGS,doc_Exists},
+ {"subtree",CnfSubTree,METH_VARARGS,doc_SubTree},
+ {"list",CnfList,METH_VARARGS,doc_List},
+ {"value_list",CnfValueList,METH_VARARGS,doc_ValueList},
+ {"my_tag",CnfMyTag,METH_VARARGS,doc_MyTag},
+ {"clear",CnfClear,METH_VARARGS,doc_Clear},
// Python Special
{"keys",CnfKeys,METH_VARARGS,doc_Keys},
+ #if PY_MAJOR_VERSION < 3
{"has_key",CnfExists,METH_VARARGS,doc_Exists},
+ #endif
{"get",CnfFind,METH_VARARGS,doc_Find},
{}
};
-// CnfGetAttr - Get an attribute - variable/method /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-static PyObject *CnfGetAttr(PyObject *Self,char *Name)
-{
- return Py_FindMethod(CnfMethods,Self,Name);
+static PyObject *CnfNew(PyTypeObject *type, PyObject *args, PyObject *kwds) {
+ char *kwlist[] = {NULL};
+ if (PyArg_ParseTupleAndKeywords(args,kwds,"",kwlist) == 0)
+ return 0;
+ return CppPyObject_NEW<Configuration*>(NULL, type, new Configuration());
}
// Type for a Normal Configuration object
+static PySequenceMethods ConfigurationSeq = {0,0,0,0,0,0,0,CnfContains,0,0};
static PyMappingMethods ConfigurationMap = {0,CnfMap,CnfMapSet};
-PyTypeObject ConfigurationType =
-{
- PyObject_HEAD_INIT(&PyType_Type)
- 0, // ob_size
- "Configuration", // tp_name
- sizeof(CppPyObject<Configuration>), // tp_basicsize
- 0, // tp_itemsize
- // Methods
- CppDealloc<Configuration>, // tp_dealloc
- 0, // tp_print
- CnfGetAttr, // tp_getattr
- 0, // tp_setattr
- 0, // tp_compare
- 0, // tp_repr
- 0, // tp_as_number
- 0, // tp_as_sequence
- &ConfigurationMap, // tp_as_mapping
- 0, // tp_hash
-};
-
-PyTypeObject ConfigurationPtrType =
-{
- PyObject_HEAD_INIT(&PyType_Type)
- 0, // ob_size
- "ConfigurationPtr", // tp_name
- sizeof(CppPyObject<Configuration *>), // tp_basicsize
- 0, // tp_itemsize
- // Methods
- (destructor)PyObject_Free, // tp_dealloc
- 0, // tp_print
- CnfGetAttr, // tp_getattr
- 0, // tp_setattr
- 0, // tp_compare
- 0, // tp_repr
- 0, // tp_as_number
- 0, // tp_as_sequence
- &ConfigurationMap, // tp_as_mapping
- 0, // tp_hash
-};
-
-PyTypeObject ConfigurationSubType =
+PyTypeObject PyConfiguration_Type =
{
- PyObject_HEAD_INIT(&PyType_Type)
- 0, // ob_size
- "ConfigurationSub", // tp_name
- sizeof(SubConfiguration), // tp_basicsize
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "apt_pkg.Configuration", // tp_name
+ sizeof(CppPyObject<Configuration*>), // tp_basicsize
0, // tp_itemsize
// Methods
- CnfSubFree, // tp_dealloc
+ CppDeallocPtr<Configuration*>, // tp_dealloc
0, // tp_print
- CnfGetAttr, // tp_getattr
+ 0, // tp_getattr
0, // tp_setattr
0, // tp_compare
0, // tp_repr
0, // tp_as_number
- 0, // tp_as_sequence
+ &ConfigurationSeq, // tp_as_sequence
&ConfigurationMap, // tp_as_mapping
0, // tp_hash
+ 0, // tp_call
+ 0, // tp_str
+ _PyAptObject_getattro, // tp_getattro
+ 0, // tp_setattro
+ 0, // tp_as_buffer
+ (Py_TPFLAGS_DEFAULT | // tp_flags
+ Py_TPFLAGS_BASETYPE),
+ "Configuration Object", // tp_doc
+ 0, // tp_traverse
+ 0, // tp_clear
+ 0, // tp_richcompare
+ 0, // tp_weaklistoffset
+ 0, // tp_iter
+ 0, // tp_iternext
+ CnfMethods, // 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
+ 0, // tp_init
+ 0, // tp_alloc
+ CnfNew, // tp_new
};