summaryrefslogtreecommitdiffstats
path: root/src/python/document.cpp
blob: 48d1073fd2c6527acd49d3dd277761651d94221c (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
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
/* -*- 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/formula.hpp"
#include "ixion/exceptions.hpp"
#include "ixion/config.hpp"

#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>

using namespace std;

namespace ixion { namespace python {

namespace {

/** non-python part of the document data */
struct document_data
{
    document_global m_global;
    vector<PyObject*> m_sheets;

    ~document_data();
};

struct free_pyobj
{
    void operator() (PyObject* p)
    {
        Py_XDECREF(p);
    }
};

document_data::~document_data()
{
    for_each(m_sheets.begin(), m_sheets.end(), free_pyobj());
}

/**
 * Python Document object.
 */
struct pyobj_document
{
    PyObject_HEAD

    document_data* m_data;
};

void document_dealloc(pyobj_document* self)
{
    delete self->m_data;
    Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
}

PyObject* document_new(PyTypeObject* type, PyObject* /*args*/, PyObject* /*kwargs*/)
{
    pyobj_document* self = (pyobj_document*)type->tp_alloc(type, 0);
    self->m_data = new document_data;
    return reinterpret_cast<PyObject*>(self);
}

int document_init(pyobj_document* self, PyObject* /*args*/, PyObject* /*kwargs*/)
{
    return 0;
}

PyObject* document_append_sheet(pyobj_document* self, PyObject* args)
{
    char* sheet_name = nullptr;
    if (!PyArg_ParseTuple(args, "s", &sheet_name))
    {
        PyErr_SetString(PyExc_TypeError, "The method must be given a sheet name string");
        return nullptr;
    }

    assert(sheet_name);

    PyTypeObject* sheet_type = get_sheet_type();
    if (!sheet_type)
        return nullptr;

    PyObject* obj_sheet = sheet_type->tp_new(sheet_type, args, 0);
    if (!obj_sheet)
    {
        PyErr_SetString(PyExc_RuntimeError,
            "Failed to allocate memory for the new sheet object.");
        return nullptr;
    }

    sheet_type->tp_init(obj_sheet, args, 0);

    // Pass model_context to the sheet object.
    sheet_data* sd = get_sheet_data(obj_sheet);
    sd->m_global = &self->m_data->m_global;
    ixion::model_context& cxt = sd->m_global->m_cxt;
    try
    {
        sd->m_sheet_index = cxt.append_sheet(sheet_name);
    }
    catch (const model_context_error& e)
    {
        // Most likely the sheet name already exists in this document.
        Py_XDECREF(obj_sheet);
        switch (e.get_error_type())
        {
            case model_context_error::sheet_name_conflict:
                PyErr_SetString(get_python_document_error(), e.what());
            break;
            default:
                PyErr_SetString(get_python_document_error(),
                    "Sheet insertion failed for unknown reason.");
        }
        return nullptr;
    }
    catch (const general_error& e)
    {
        Py_XDECREF(obj_sheet);
        ostringstream os;
        os << "Sheet insertion failed and the reason is '" << e.what() << "'";
        PyErr_SetString(get_python_document_error(), os.str().c_str());
        return nullptr;
    }

    // Append this sheet instance to the document.
    Py_INCREF(obj_sheet);
    self->m_data->m_sheets.push_back(obj_sheet);

    return obj_sheet;
}

const char* doc_document_calculate =
"Document.calculate([threads])\n"
"\n"
"(Re-)calculate all modified formula cells in the document.\n"
"\n"
"Keyword arguments:\n"
"\n"
"threads -- number of threads to use besides the main thread, or 0 if all\n"
"           calculations are to be performed on the main thread. (default 0)\n"
;

PyObject* document_calculate(pyobj_document* self, PyObject* args, PyObject* kwargs)
{
    static const char* kwlist[] = { "threads", nullptr };

    long threads = 0;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", const_cast<char**>(kwlist), &threads))
    {
        PyErr_SetString(PyExc_TypeError, "Failed to parse the arguments for Document.calculate()");
        return nullptr;
    }

    document_global& dg = self->m_data->m_global;

    // Query additional dirty formula cells and add them to the current set.
    std::vector<abs_range_t> sorted = ixion::query_and_sort_dirty_cells(
        dg.m_cxt, dg.m_modified_cells, &dg.m_dirty_formula_cells);
    ixion::calculate_sorted_cells(dg.m_cxt, sorted, threads);

    dg.m_modified_cells.clear();
    dg.m_dirty_formula_cells.clear();

    Py_INCREF(Py_None);
    return Py_None;
}

PyObject* document_get_sheet(pyobj_document* self, PyObject* arg)
{
    const vector<PyObject*>& sheets = self->m_data->m_sheets;
    if (PyLong_Check(arg))
    {
        long index = PyLong_AsLong(arg);
        if (index == -1 && PyErr_Occurred())
            return nullptr;

        if (index < 0 || static_cast<size_t>(index) >= sheets.size())
        {
            PyErr_SetString(PyExc_IndexError, "Out-of-bound sheet index");
            return nullptr;
        }

        PyObject* sheet_obj = sheets[index];
        Py_INCREF(sheet_obj);
        return sheet_obj;
    }

    // Not a python int object.  See if it's a string object.
    const char* name = PyUnicode_AsUTF8(arg);
    if (!name)
    {
        PyErr_SetString(PyExc_TypeError,
            "The 'arg' value must be either of type int or type str.");
        return nullptr;
    }

    // Iterate through all sheets to find a match.
    // TODO : Use string hash to speed up the lookup.
    for (PyObject* sh : sheets)
    {
        PyObject* obj = get_sheet_name(sh);
        if (!obj)
            continue;

        const char* this_name = PyUnicode_AsUTF8(obj);
        if (!this_name)
            continue;

        if (!strcmp(name, this_name))
        {
            Py_INCREF(sh);
            return sh;
        }
    }

    ostringstream os;
    os << "No sheet named '" << name << "' found";
    PyErr_SetString(PyExc_IndexError, os.str().c_str());
    return nullptr;
}

PyObject* document_getter_sheet_names(pyobj_document* self, void* closure)
{
    model_context& cxt = self->m_data->m_global.m_cxt;
    const vector<PyObject*>& sheets = self->m_data->m_sheets;
    size_t n = sheets.size();
    PyObject* t = PyTuple_New(n);
    for (size_t i = 0; i < n; ++i)
    {
        std::string name = cxt.get_sheet_name(i);
        PyObject* o = PyUnicode_FromString(name.c_str());
        PyTuple_SetItem(t, i, o);
    }

    return t;
}

PyMethodDef document_methods[] =
{
    { "append_sheet", (PyCFunction)document_append_sheet, METH_VARARGS, "append new sheet to the document" },
    { "calculate", (PyCFunction)document_calculate, METH_VARARGS | METH_KEYWORDS, doc_document_calculate },
    { "get_sheet", (PyCFunction)document_get_sheet, METH_O, "get a sheet object either by index or name" },
    { nullptr }
};

PyGetSetDef document_getset[] =
{
    { "sheet_names", (getter)document_getter_sheet_names, (setter)nullptr, "A tuple of sheet names", nullptr },
    { nullptr }
};

PyTypeObject document_type =
{
    PyVarObject_HEAD_INIT(nullptr, 0)
    "ixion.Document",                         // tp_name
    sizeof(pyobj_document),                         // tp_basicsize
    0,                                        // tp_itemsize
    (destructor)document_dealloc,             // tp_dealloc
    0,                                        // tp_print
    0,                                        // tp_getattr
    0,                                        // tp_setattr
    0,                                        // tp_compare
    0,                                        // tp_repr
    0,                                        // tp_as_number
    0,                                        // tp_as_sequence
    0,                                        // 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 | Py_TPFLAGS_BASETYPE, // tp_flags
    "ixion document object",                  // tp_doc
    0,		                                  // tp_traverse
    0,		                                  // tp_clear
    0,		                                  // tp_richcompare
    0,		                                  // tp_weaklistoffset
    0,		                                  // tp_iter
    0,		                                  // tp_iternext
    document_methods,                         // tp_methods
    0,                                        // tp_members
    document_getset,                          // tp_getset
    0,                                        // tp_base
    0,                                        // tp_dict
    0,                                        // tp_descr_get
    0,                                        // tp_descr_set
    0,                                        // tp_dictoffset
    (initproc)document_init,                  // tp_init
    0,                                        // tp_alloc
    document_new,                             // tp_new
};

}

PyTypeObject* get_document_type()
{
    return &document_type;
}

}}

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