1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
// $Id: configuration.cc,v 1.4 2003/06/03 03:22:27 mdz Exp $
/* ######################################################################
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 wrapping is mostly 1:1 with the C++ code, but there are additions to
wrap the linked tree walking into nice flat sequence walking.
##################################################################### */
/*}}}*/
// Include Files /*{{{*/
#include "generic.h"
#include "apt_pkgmodule.h"
#include <apt-pkg/configuration.h>
#include <apt-pkg/cmndline.h>
#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);
}
/*}}}*/
// Method Wrappers /*{{{*/
static char *doc_Find = "Find(Name[,default]) -> String/None";
static PyObject *CnfFind(PyObject *Self,PyObject *Args)
{
char *Name = 0;
char *Default = 0;
if (PyArg_ParseTuple(Args,"s|s",&Name,&Default) == 0)
return 0;
return CppPyString(GetSelf(Self).Find(Name,Default));
}
static char *doc_FindFile = "FindFile(Name[,default]) -> String/None";
static PyObject *CnfFindFile(PyObject *Self,PyObject *Args)
{
char *Name = 0;
char *Default = 0;
if (PyArg_ParseTuple(Args,"s|s",&Name,&Default) == 0)
return 0;
return CppPyString(GetSelf(Self).FindFile(Name,Default));
}
static char *doc_FindDir = "FindDir(Name[,default]) -> String/None";
static PyObject *CnfFindDir(PyObject *Self,PyObject *Args)
{
char *Name = 0;
char *Default = 0;
if (PyArg_ParseTuple(Args,"s|s",&Name,&Default) == 0)
return 0;
return CppPyString(GetSelf(Self).FindDir(Name,Default));
}
static char *doc_FindI = "FindI(Name[,default]) -> Integer";
static PyObject *CnfFindI(PyObject *Self,PyObject *Args)
{
char *Name = 0;
int Default = 0;
if (PyArg_ParseTuple(Args,"s|i",&Name,&Default) == 0)
return 0;
return Py_BuildValue("i",GetSelf(Self).FindI(Name,Default));
}
static char *doc_FindB = "FindB(Name[,default]) -> Integer";
static PyObject *CnfFindB(PyObject *Self,PyObject *Args)
{
char *Name = 0;
int Default = 0;
if (PyArg_ParseTuple(Args,"s|i",&Name,&Default) == 0)
return 0;
return Py_BuildValue("i",(int)GetSelf(Self).FindB(Name,(Default == 0?false:true)));
}
static char *doc_Set = "Set(Name,Value) -> None";
static PyObject *CnfSet(PyObject *Self,PyObject *Args)
{
char *Name = 0;
char *Value = 0;
if (PyArg_ParseTuple(Args,"ss",&Name,&Value) == 0)
return 0;
GetSelf(Self).Set(Name,Value);
Py_INCREF(Py_None);
return Py_None;
}
static char *doc_Exists = "Exists(Name) -> Integer";
static PyObject *CnfExists(PyObject *Self,PyObject *Args)
{
char *Name = 0;
if (PyArg_ParseTuple(Args,"s",&Name) == 0)
return 0;
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)
{
char *Name = 0;
if (PyArg_ParseTuple(Args,"s",&Name) == 0)
return 0;
GetSelf(Self).Clear(Name);
Py_INCREF(Py_None);
return Py_None;
}
// The amazing narrowing search ability!
static char *doc_SubTree = "SubTree(Name) -> Configuration";
static PyObject *CnfSubTree(PyObject *Self,PyObject *Args)
{
char *Name;
if (PyArg_ParseTuple(Args,"s",&Name) == 0)
return 0;
const Configuration::Item *Itm = GetSelf(Self).Tree(Name);
if (Itm == 0)
{
PyErr_SetString(PyExc_KeyError,Name);
return 0;
}
// Create a new sub configuration.
SubConfiguration *New = (SubConfiguration*)(&ConfigurationSubType)
->tp_alloc(&ConfigurationSubType,0);
new (&New->Object) Configuration(Itm);
New->Owner = Self;
Py_INCREF(Self);
return New;
}
// Return a list of items at a specific level
static char *doc_List = "List([root]) -> List";
static PyObject *CnfList(PyObject *Self,PyObject *Args)
{
char *RootName = 0;
if (PyArg_ParseTuple(Args,"|s",&RootName) == 0)
return 0;
// Convert the whole configuration space into a list
PyObject *List = PyList_New(0);
const Configuration::Item *Top = GetSelf(Self).Tree(RootName);
const Configuration::Item *Root = GetSelf(Self).Tree(0)->Parent;
if (Top != 0 && RootName != 0)
Top = Top->Child;
for (; Top != 0; Top = Top->Next)
{
PyObject *Obj;
PyList_Append(List,Obj = CppPyString(Top->FullTag(Root)));
Py_DECREF(Obj);
}
return List;
}
/* Return a list of values of items at a specific level.. This is used to
get out value lists */
static char *doc_ValueList = "ValueList([root]) -> List";
static PyObject *CnfValueList(PyObject *Self,PyObject *Args)
{
char *RootName = 0;
if (PyArg_ParseTuple(Args,"|s",&RootName) == 0)
return 0;
// Convert the whole configuration space into a list
PyObject *List = PyList_New(0);
const Configuration::Item *Top = GetSelf(Self).Tree(RootName);
if (Top != 0 && RootName != 0)
Top = Top->Child;
for (; Top != 0; Top = Top->Next)
{
PyObject *Obj;
PyList_Append(List,Obj = CppPyString(Top->Value));
Py_DECREF(Obj);
}
return List;
}
static char *doc_MyTag = "MyTag() -> String";
static PyObject *CnfMyTag(PyObject *Self,PyObject *Args)
{
if (PyArg_ParseTuple(Args,"") == 0)
return 0;
const Configuration::Item *Top = GetSelf(Self).Tree(0);
if (Top == 0)
return Py_BuildValue("s","");
return CppPyString(Top->Parent->Tag);
}
// Look like a mapping
static char *doc_Keys = "keys([root]) -> List";
static PyObject *CnfKeys(PyObject *Self,PyObject *Args)
{
char *RootName = 0;
if (PyArg_ParseTuple(Args,"|s",&RootName) == 0)
return 0;
// Convert the whole configuration space into a list
PyObject *List = PyList_New(0);
const Configuration::Item *Top = GetSelf(Self).Tree(RootName);
const Configuration::Item *Stop = Top;
const Configuration::Item *Root = 0;
if (RootName == 0)
Stop = 0;
if (Top != 0)
Root = GetSelf(Self).Tree(0)->Parent;
for (; Top != 0;)
{
PyObject *Obj;
PyList_Append(List,Obj = CppPyString(Top->FullTag(Root)));
Py_DECREF(Obj);
if (Top->Child != 0)
{
Top = Top->Child;
continue;
}
while (Top != 0 && Top->Next == 0 && Top != Root &&
Top->Parent != Stop)
Top = Top->Parent;
if (Top != 0)
Top = Top->Next;
}
return List;
}
// Map access, operator []
static PyObject *CnfMap(PyObject *Self,PyObject *Arg)
{
if (PyString_Check(Arg) == 0)
{
PyErr_SetNone(PyExc_TypeError);
return 0;
}
if (GetSelf(Self).Exists(PyString_AsString(Arg)) == false)
{
PyErr_SetString(PyExc_KeyError,PyString_AsString(Arg));
return 0;
}
return CppPyString(GetSelf(Self).Find(PyString_AsString(Arg)));
}
// Assignment with operator []
static int CnfMapSet(PyObject *Self,PyObject *Arg,PyObject *Val)
{
if (PyString_Check(Arg) == 0 || PyString_Check(Val) == 0)
{
PyErr_SetNone(PyExc_TypeError);
return -1;
}
GetSelf(Self).Set(PyString_AsString(Arg),PyString_AsString(Val));
return 0;
}
/*}}}*/
// Config file loaders /*{{{*/
char *doc_LoadConfig = "LoadConfig(Configuration,FileName) -> None";
PyObject *LoadConfig(PyObject *Self,PyObject *Args)
{
char *Name = 0;
if (PyArg_ParseTuple(Args,"Os",&Self,&Name) == 0)
return 0;
if (Configuration_Check(Self)== 0)
{
PyErr_SetString(PyExc_TypeError,"argument 1: expected Configuration.");
return 0;
}
if (ReadConfigFile(GetSelf(Self),Name,false) == false)
return HandleErrors();
Py_INCREF(Py_None);
return HandleErrors(Py_None);
}
char *doc_LoadConfigISC = "LoadConfigISC(Configuration,FileName) -> None";
PyObject *LoadConfigISC(PyObject *Self,PyObject *Args)
{
char *Name = 0;
if (PyArg_ParseTuple(Args,"Os",&Self,&Name) == 0)
return 0;
if (Configuration_Check(Self)== 0)
{
PyErr_SetString(PyExc_TypeError,"argument 1: expected Configuration.");
return 0;
}
if (ReadConfigFile(GetSelf(Self),Name,true) == false)
return HandleErrors();
Py_INCREF(Py_None);
return HandleErrors(Py_None);
}
char *doc_LoadConfigDir = "LoadConfigDir(Configuration,DirName) -> None";
PyObject *LoadConfigDir(PyObject *Self,PyObject *Args)
{
char *Name = 0;
if (PyArg_ParseTuple(Args,"Os",&Self,&Name) == 0)
return 0;
if (Configuration_Check(Self)== 0)
{
PyErr_SetString(PyExc_TypeError,"argument 1: expected Configuration.");
return 0;
}
if (ReadConfigDir(GetSelf(Self),Name,false) == false)
return HandleErrors();
Py_INCREF(Py_None);
return HandleErrors(Py_None);
}
/*}}}*/
// ParseCommandLine - Wrapper for the command line interface /*{{{*/
// ---------------------------------------------------------------------
char *doc_ParseCommandLine =
"ParseCommandLine(Configuration,ListOfOptions,List-argv) -> List\n"
"\n"
"This function is like getopt except it manipulates a configuration space.\n"
"output is a list of non-option arguments (filenames, etc).\n"
"ListOfOptions is a list of tuples of the form:\n"
" ('c',\"long-opt or None\",\"Configuration::Variable\",\"optional type\")\n"
"Where type may be one of HasArg, IntLevel, Boolean, InvBoolean,\n"
"ConfigFile, or ArbItem. The default is Boolean.";
PyObject *ParseCommandLine(PyObject *Self,PyObject *Args)
{
PyObject *POList;
PyObject *Pargv;
if (PyArg_ParseTuple(Args,"OO!O!",&Self,
&PyList_Type,&POList,&PyList_Type,&Pargv) == 0)
return 0;
if (Configuration_Check(Self)== 0)
{
PyErr_SetString(PyExc_TypeError,"argument 1: expected Configuration.");
return 0;
}
// Convert the option list
int Length = PySequence_Length(POList);
CommandLine::Args *OList = new CommandLine::Args[Length+1];
OList[Length].ShortOpt = 0;
OList[Length].LongOpt = 0;
for (int I = 0; I != Length; I++)
{
char *Type = 0;
if (PyArg_ParseTuple(PySequence_GetItem(POList,I),"czs|s",
&OList[I].ShortOpt,&OList[I].LongOpt,
&OList[I].ConfName,&Type) == 0)
{
delete [] OList;
return 0;
}
OList[I].Flags = 0;
// Convert the type over to flags..
if (Type != 0)
{
if (strcasecmp(Type,"HasArg") == 0)
OList[I].Flags = CommandLine::HasArg;
else if (strcasecmp(Type,"IntLevel") == 0)
OList[I].Flags = CommandLine::IntLevel;
else if (strcasecmp(Type,"Boolean") == 0)
OList[I].Flags = CommandLine::Boolean;
else if (strcasecmp(Type,"InvBoolean") == 0)
OList[I].Flags = CommandLine::InvBoolean;
else if (strcasecmp(Type,"ConfigFile") == 0)
OList[I].Flags = CommandLine::ConfigFile;
else if (strcasecmp(Type,"ArbItem") == 0)
OList[I].Flags = CommandLine::ArbItem;
}
}
// Convert the argument list into a char **
const char **argv = ListToCharChar(Pargv);
if (argv == 0)
{
delete [] OList;
return 0;
}
// Do the command line processing
PyObject *List = 0;
{
CommandLine CmdL(OList,&GetSelf(Self));
if (CmdL.Parse(PySequence_Length(Pargv),argv) == false)
{
delete [] argv;
delete [] OList;
return HandleErrors();
}
// Convert the file listing into a python sequence
for (Length = 0; CmdL.FileList[Length] != 0; Length++);
List = PyList_New(Length);
for (int I = 0; CmdL.FileList[I] != 0; I++)
{
PyList_SetItem(List,I,PyString_FromString(CmdL.FileList[I]));
}
}
delete [] argv;
delete [] OList;
return HandleErrors(List);
}
/*}}}*/
// Method table for the Configuration object
static PyMethodDef CnfMethods[] =
{
// Query
{"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},
{"value_list",CnfValueList,METH_VARARGS,doc_ValueList},
{"my_tag",CnfMyTag,METH_VARARGS,doc_MyTag},
{"clear",CnfClear,METH_VARARGS,doc_Clear},
#ifdef COMPAT_0_7
{"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},
{"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},
#endif
// 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},
{}
};
static PyObject *CnfNew(PyTypeObject *type, PyObject *args, PyObject *kwds) {
static char *kwlist[] = {};
if (PyArg_ParseTupleAndKeywords(args,kwds,"",kwlist) == 0)
return 0;
return CppPyObject_NEW<Configuration>(type);
}
// 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)
#if PY_MAJOR_VERSION < 3
0, // ob_size
#endif
"apt_pkg.Configuration", // tp_name
sizeof(CppPyObject<Configuration>), // tp_basicsize
0, // tp_itemsize
// Methods
CppDealloc<Configuration>, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
0, // tp_repr
0, // tp_as_number
&ConfigurationSeq, // tp_as_sequence
&ConfigurationMap, // 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, // tp_flags
"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
};
PyTypeObject ConfigurationPtrType =
{
PyObject_HEAD_INIT(&PyType_Type)
#if PY_MAJOR_VERSION < 3
0, // ob_size
#endif
"apt_pkg.ConfigurationPtr", // tp_name
sizeof(CppPyObject<Configuration *>), // tp_basicsize
0, // tp_itemsize
// Methods
(destructor)PyObject_Free, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
0, // tp_repr
0, // tp_as_number
&ConfigurationSeq, // tp_as_sequence
&ConfigurationMap, // 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, // tp_flags
"ConfigurationPtr Object", // tp_doc
0, // tp_traverse
0, // tp_clear
0, // tp_richcompare
0, // tp_weaklistoffset
0, // tp_iter
0, // tp_iternext
CnfMethods, // tp_methods
};
PyTypeObject ConfigurationSubType =
{
PyObject_HEAD_INIT(&PyType_Type)
#if PY_MAJOR_VERSION < 3
0, // ob_size
#endif
"apt_pkg.ConfigurationSub", // tp_name
sizeof(SubConfiguration), // tp_basicsize
0, // tp_itemsize
// Methods
CnfSubFree, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
0, // tp_repr
0, // tp_as_number
&ConfigurationSeq, // tp_as_sequence
&ConfigurationMap, // 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, // tp_flags
"ConfigurationSub Object", // tp_doc
0, // tp_traverse
0, // tp_clear
0, // tp_richcompare
0, // tp_weaklistoffset
0, // tp_iter
0, // tp_iternext
CnfMethods, // tp_methods
};
|