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
|
/* -*- 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 "orcus/env.hpp"
#include "orcus/info.hpp"
#include "root.hpp"
#include "xlsx.hpp"
#include "ods.hpp"
#include "csv.hpp"
#include "xls_xml.hpp"
#include "gnumeric.hpp"
#ifdef __ORCUS_SPREADSHEET_MODEL
#include "document.hpp"
#include "sheet.hpp"
#include "sheet_rows.hpp"
#include "cell.hpp"
#include "named_expression.hpp"
#include "named_expressions.hpp"
#include "formula_token.hpp"
#include "formula_tokens.hpp"
#endif
#include <iostream>
#include <sstream>
#include <string>
#include <Python.h>
#define ORCUS_DEBUG_PYTHON 0
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
using namespace std;
namespace orcus { namespace python {
namespace {
#if ORCUS_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
PyMethodDef orcus_methods[] =
{
{ "detect_format", (PyCFunction)detect_format, METH_VARARGS | METH_KEYWORDS,
"Detect the format type of a spreadsheet file." },
{ "_csv_read", (PyCFunction)csv_read, METH_VARARGS | METH_KEYWORDS,
"Load specified csv file into a document model." },
{ "_ods_read", (PyCFunction)ods_read, METH_VARARGS | METH_KEYWORDS,
"Load specified ods file into a document model." },
{ "_xlsx_read", (PyCFunction)xlsx_read, METH_VARARGS | METH_KEYWORDS,
"Load specified xlsx file into a document model." },
{ "_xls_xml_read", (PyCFunction)xls_xml_read, METH_VARARGS | METH_KEYWORDS,
"Load specified xls-xml file into a document model." },
{ "_gnumeric_read", (PyCFunction)gnumeric_read, METH_VARARGS | METH_KEYWORDS,
"Load specified gnumeric file into a document model." },
{ nullptr, nullptr, 0, nullptr }
};
struct module_state
{
PyObject* error;
};
int orcus_traverse(PyObject* m, visitproc visit, void* arg)
{
Py_VISIT(GETSTATE(m)->error);
return 0;
}
int orcus_clear(PyObject* m)
{
Py_CLEAR(GETSTATE(m)->error);
return 0;
}
[[maybe_unused]]
bool add_type_to_module(PyObject* m, PyTypeObject* typeobj, const char* type_name)
{
if (PyType_Ready(typeobj))
return false;
Py_INCREF(typeobj);
if (PyModule_AddObject(m, type_name, reinterpret_cast<PyObject*>(typeobj)) < 0)
{
Py_DECREF(m);
Py_DECREF(typeobj);
return false;
}
return true;
}
bool populate_module_attributes(PyObject* m)
{
std::ostringstream os;
os << orcus::get_version_major() << '.'
<< orcus::get_version_minor() << '.'
<< orcus::get_version_micro();
PyObject* version = PyUnicode_FromString(os.str().data());
if (PyModule_AddObject(m, "__version__", version) < 0)
return false;
return true;
}
}
struct PyModuleDef moduledef =
{
PyModuleDef_HEAD_INIT,
"_orcus",
nullptr,
sizeof(struct module_state),
orcus_methods,
nullptr,
orcus_traverse,
orcus_clear,
nullptr
};
}}
extern "C" {
ORCUS_DLLPUBLIC PyObject* PyInit__orcus()
{
PyObject* m = PyModule_Create(&orcus::python::moduledef);
if (!orcus::python::populate_module_attributes(m))
return nullptr;
#ifdef __ORCUS_SPREADSHEET_MODEL
if (!orcus::python::add_type_to_module(m, orcus::python::get_document_type(), "Document"))
return nullptr;
if (!orcus::python::add_type_to_module(m, orcus::python::get_sheet_type(), "Sheet"))
return nullptr;
if (!orcus::python::add_type_to_module(m, orcus::python::get_sheet_rows_type(), "SheetRows"))
return nullptr;
if (!orcus::python::add_type_to_module(m, orcus::python::get_cell_type(), "Cell"))
return nullptr;
if (!orcus::python::add_type_to_module(m, orcus::python::get_named_exp_type(), "NamedExpression"))
return nullptr;
if (!orcus::python::add_type_to_module(m, orcus::python::get_named_exps_type(), "NamedExpressions"))
return nullptr;
if (!orcus::python::add_type_to_module(m, orcus::python::get_formula_token_type(), "FormulaToken"))
return nullptr;
if (!orcus::python::add_type_to_module(m, orcus::python::get_formula_tokens_type(), "FormulaTokens"))
return nullptr;
#endif
return m;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|