~elementary-os/ubuntu-package-imports/python-apt-trusty

« back to all changes in this revision

Viewing changes to doc/client-example.cc

  • Committer: Sergey "Shnatsel" Davidoff
  • Date: 2014-03-19 22:46:01 UTC
  • Revision ID: shnatsel@gmail.com-20140319224601-rlt4y8n267qfqtc2
Initial import, version 0.9.3.3ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * client-example.cc - A simple example for using the python-apt C++ API.
 
3
 *
 
4
 * Copyright 2009 Julian Andres Klode <jak@debian.org>
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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,
 
19
 * MA 02110-1301, USA.
 
20
 */
 
21
 
 
22
#include <python-apt/python-apt.h>
 
23
#include <apt-pkg/hashes.h>
 
24
 
 
25
// The module initialization.
 
26
extern "C" void initclient() {
 
27
    if (import_apt_pkg() < 0)
 
28
        return;
 
29
    // Initialize a module.
 
30
    PyObject *Module = Py_InitModule("client", NULL);
 
31
 
 
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));
 
36
 
 
37
    // Another example: Add the HashString type to the module.
 
38
    Py_INCREF(&PyHashString_Type);
 
39
    PyModule_AddObject(Module, "HashString", (PyObject*)(&PyHashString_Type));
 
40
}
 
41
 
 
42
int main(int argc, char *argv[]) {
 
43
    // Return value.
 
44
    int ret = 0;
 
45
    // Initialize python 
 
46
    Py_Initialize();
 
47
    // Make the client module importable
 
48
    PyImport_AppendInittab("client", &initclient);
 
49
    // Set the commandline arguments.
 
50
    PySys_SetArgv(argc, argv);
 
51
 
 
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)
 
55
        ret = 1;
 
56
        goto end;
 
57
    }
 
58
 
 
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");
 
62
    else
 
63
        Py_Main(argc, argv);
 
64
 
 
65
end:
 
66
    Py_Finalize();
 
67
    return ret;
 
68
}