2
* client-example.cc - A simple example for using the python-apt C++ API.
4
* Copyright 2009 Julian Andres Klode <jak@debian.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22
#include <python-apt/python-apt.h>
23
#include <apt-pkg/hashes.h>
25
// The module initialization.
26
extern "C" void initclient() {
27
if (import_apt_pkg() < 0)
29
// Initialize a module.
30
PyObject *Module = Py_InitModule("client", NULL);
32
// Create a HashString, which will be added to the module.
33
HashString *hash = new HashString("0966a120bb936bdc6fdeac445707aa6b");
34
// Create a Python object for the hashstring and add it to the module
35
PyModule_AddObject(Module, "hash", PyHashString_FromCpp(hash, false, NULL));
37
// Another example: Add the HashString type to the module.
38
Py_INCREF(&PyHashString_Type);
39
PyModule_AddObject(Module, "HashString", (PyObject*)(&PyHashString_Type));
42
int main(int argc, char *argv[]) {
47
// Make the client module importable
48
PyImport_AppendInittab("client", &initclient);
49
// Set the commandline arguments.
50
PySys_SetArgv(argc, argv);
52
// Import the module, so the user does not have to import it.
53
if (PyRun_SimpleString("import client\n") < 0) {
54
// Failure (should never be reached)
59
// Run IPython if available, otherwise a normal interpreter.
60
if (PyRun_SimpleString("from IPython.Shell import start\n") == 0)
61
PyRun_SimpleString("start(user_ns=dict(client=client)).mainloop()\n");