summaryrefslogtreecommitdiffstats
path: root/src/python/python.cpp
blob: 1c8b5bf14e4dbdd6bd722017e912e861461dcaf2 (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "document.hpp"
#include "sheet.hpp"
#include "global.hpp"

#include "ixion/env.hpp"
#include "ixion/info.hpp"

#include <iostream>
#include <string>

#define IXION_DEBUG_PYTHON 0
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))

using namespace std;

namespace ixion { namespace python {

namespace {

#if IXION_DEBUG_PYTHON
void print_args(PyObject* args)
{
    string args_str;
    PyObject* repr = PyObject_Repr(args);
    if (repr)
    {
        Py_INCREF(repr);
        args_str = PyBytes_AsString(repr);
        Py_DECREF(repr);
    }
    cout << args_str << "\n";
}
#endif

PyObject* info(PyObject*, PyObject*)
{
    cout << "ixion version: "
        << ixion::get_version_major() << '.'
        << ixion::get_version_minor() << '.'
        << ixion::get_version_micro() << endl;

    Py_INCREF(Py_None);
    return Py_None;
}

PyObject* column_label(PyObject* /*module*/, PyObject* args, PyObject* kwargs)
{
    int start;
    int stop;
    int resolver_index = 1; // Excel A1 by default
    static const char* kwlist[] = { "start", "stop", "resolver", nullptr };
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i", const_cast<char**>(kwlist), &start, &stop, &resolver_index))
        return nullptr;

    if (start >= stop)
    {
        PyErr_SetString(
            PyExc_IndexError,
            "Start position is larger or equal to the stop position.");
        return nullptr;
    }

    if (start < 0)
    {
        PyErr_SetString(
            PyExc_IndexError,
            "Start position should be larger than or equal to 0.");
        return nullptr;
    }

    auto resolver = formula_name_resolver::get(
        static_cast<formula_name_resolver_t>(resolver_index), nullptr);
    if (!resolver)
    {
        PyErr_SetString(
            get_python_formula_error(), "Specified resolver type is invalid.");
        return nullptr;
    }

    int size = stop - start;
    PyObject* t = PyTuple_New(size);

    for (int i = start; i < stop; ++i)
    {
        string s = resolver->get_column_name(i);
        PyObject* o = PyUnicode_FromString(s.c_str());
        PyTuple_SetItem(t, i-start, o);
    }

    return t;
}

PyMethodDef ixion_methods[] =
{
    { "info", (PyCFunction)info, METH_NOARGS, "Print ixion module information." },
    { "column_label", (PyCFunction)column_label, METH_VARARGS | METH_KEYWORDS,
      "Return a list of column label strings based on specified column range values." },
    { nullptr, nullptr, 0, nullptr }
};

struct module_state
{
    PyObject* error;
};

int ixion_traverse(PyObject* m, visitproc visit, void* arg)
{
    Py_VISIT(GETSTATE(m)->error);
    return 0;
}

int ixion_clear(PyObject* m)
{
    Py_CLEAR(GETSTATE(m)->error);
    return 0;
}

}

struct PyModuleDef moduledef =
{
    PyModuleDef_HEAD_INIT,
    "ixion",
    nullptr,
    sizeof(struct module_state),
    ixion_methods,
    nullptr,
    ixion_traverse,
    ixion_clear,
    nullptr
};

}}

extern "C" {

IXION_DLLPUBLIC PyObject* PyInit_ixion()
{
    PyTypeObject* doc_type = ixion::python::get_document_type();
    if (PyType_Ready(doc_type) < 0)
        return nullptr;

    PyTypeObject* sheet_type = ixion::python::get_sheet_type();
    if (PyType_Ready(sheet_type) < 0)
        return nullptr;

    PyObject* m = PyModule_Create(&ixion::python::moduledef);

    Py_INCREF(doc_type);
    PyModule_AddObject(m, "Document", reinterpret_cast<PyObject*>(doc_type));

    Py_INCREF(sheet_type);
    PyModule_AddObject(m, "Sheet", reinterpret_cast<PyObject*>(sheet_type));

    PyModule_AddObject(
        m, "DocumentError", ixion::python::get_python_document_error());
    PyModule_AddObject(
        m, "SheetError", ixion::python::get_python_sheet_error());
    PyModule_AddObject(
        m, "FormulaError", ixion::python::get_python_formula_error());

    return m;
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */