py3: ported Spreadsheet to python3

This commit is contained in:
wmayer 2016-01-23 19:52:37 +01:00
parent f9cb89a9cb
commit 3d3a8d0141
3 changed files with 72 additions and 7 deletions

View File

@ -19,6 +19,7 @@
#include <CXX/Objects.hxx>
#include <Base/Console.h>
#include <Base/PyObjectBase.h>
#include "Sheet.h"
namespace Spreadsheet {
@ -34,10 +35,16 @@ public:
private:
};
PyObject* initModule()
{
return (new Module)->module().ptr();
}
} // namespace Spreadsheet
/* Python entry */
PyMODINIT_FUNC initSpreadsheet() {
PyMOD_INIT_FUNC(Spreadsheet)
{
Spreadsheet::PropertySpreadsheetQuantity::init();
Spreadsheet::PropertyColumnWidths::init();
Spreadsheet::PropertyRowHeights::init();
@ -46,6 +53,7 @@ PyMODINIT_FUNC initSpreadsheet() {
Spreadsheet::Sheet::init();
Spreadsheet::SheetPython::init();
new Spreadsheet::Module();
PyObject* mod = Spreadsheet::initModule();
Base::Console().Log("Loading Spreadsheet module... done\n");
PyMOD_Return(mod);
}

View File

@ -289,8 +289,13 @@ PyObject* SheetPy::setStyle(PyObject *args)
PyObject * item = PySet_Pop(copy);
// check on the key:
#if PY_MAJOR_VERSION >= 3
if (PyBytes_Check(item))
style.insert(PyBytes_AsString(item));
#else
if (PyString_Check(item))
style.insert(PyString_AsString(item));
#endif
else {
std::string error = std::string("type of the set need to be a string, not ") + item->ob_type->tp_name;
PyErr_SetString(PyExc_TypeError, error.c_str());
@ -300,11 +305,19 @@ PyObject* SheetPy::setStyle(PyObject *args)
}
Py_DECREF(copy);
}
#if PY_MAJOR_VERSION >= 3
else if (PyBytes_Check(value)) {
#else
else if (PyString_Check(value)) {
#endif
using namespace boost;
escaped_list_separator<char> e('\0', '|', '\0');
#if PY_MAJOR_VERSION >= 3
std::string line = PyBytes_AsString(value);
#else
std::string line = PyString_AsString(value);
#endif
tokenizer<escaped_list_separator<char> > tok(line, e);
for(tokenizer<escaped_list_separator<char> >::iterator i = tok.begin(); i != tok.end();++i)
@ -416,7 +429,11 @@ PyObject* SheetPy::getStyle(PyObject *args)
PyObject * s = PySet_New(NULL);
for (std::set<std::string>::const_iterator i = style.begin(); i != style.end(); ++i)
#if PY_MAJOR_VERSION >= 3
PySet_Add(s, PyBytes_FromString((*i).c_str()));
#else
PySet_Add(s, PyString_FromString((*i).c_str()));
#endif
return s;
}
@ -568,8 +585,13 @@ PyObject* SheetPy::setAlignment(PyObject *args)
while (n-- > 0) {
PyObject * item = PySet_Pop(copy);
#if PY_MAJOR_VERSION >= 3
if (PyBytes_Check(item))
alignment = Cell::decodeAlignment(PyBytes_AsString(item), alignment);
#else
if (PyString_Check(item))
alignment = Cell::decodeAlignment(PyString_AsString(item), alignment);
#endif
else {
std::string error = std::string("type of the key need to be a string, not") + item->ob_type->tp_name;
PyErr_SetString(PyExc_TypeError, error.c_str());
@ -580,12 +602,20 @@ PyObject* SheetPy::setAlignment(PyObject *args)
Py_DECREF(copy);
}
#if PY_MAJOR_VERSION >= 3
else if (PyBytes_Check(value)) {
#else
else if (PyString_Check(value)) {
#endif
// Argument is a string, combination of alignments, separated by the pipe character
using namespace boost;
escaped_list_separator<char> e('\0', '|', '\0');
#if PY_MAJOR_VERSION >= 3
std::string line = PyBytes_AsString(value);
#else
std::string line = PyString_AsString(value);
#endif
tokenizer<escaped_list_separator<char> > tok(line, e);
for(tokenizer<escaped_list_separator<char> >::iterator i = tok.begin(); i != tok.end();++i)
@ -652,6 +682,20 @@ PyObject* SheetPy::getAlignment(PyObject *args)
if (cell && cell->getAlignment(alignment)) {
PyObject * s = PySet_New(NULL);
#if PY_MAJOR_VERSION >= 3
if (alignment & Cell::ALIGNMENT_LEFT)
PySet_Add(s, PyBytes_FromString("left"));
if (alignment & Cell::ALIGNMENT_HCENTER)
PySet_Add(s, PyBytes_FromString("center"));
if (alignment & Cell::ALIGNMENT_RIGHT)
PySet_Add(s, PyBytes_FromString("right"));
if (alignment & Cell::ALIGNMENT_TOP)
PySet_Add(s, PyBytes_FromString("top"));
if (alignment & Cell::ALIGNMENT_VCENTER)
PySet_Add(s, PyBytes_FromString("vcenter"));
if (alignment & Cell::ALIGNMENT_BOTTOM)
PySet_Add(s, PyBytes_FromString("bottom"));
#else
if (alignment & Cell::ALIGNMENT_LEFT)
PySet_Add(s, PyString_FromString("left"));
if (alignment & Cell::ALIGNMENT_HCENTER)
@ -664,6 +708,7 @@ PyObject* SheetPy::getAlignment(PyObject *args)
PySet_Add(s, PyString_FromString("vcenter"));
if (alignment & Cell::ALIGNMENT_BOTTOM)
PySet_Add(s, PyString_FromString("bottom"));
#endif
return s;
}
@ -677,8 +722,13 @@ static float decodeFloat(const PyObject * obj)
{
if (PyFloat_Check(obj))
return PyFloat_AsDouble((PyObject *)obj);
#if PY_MAJOR_VERSION >= 3
else if (PyLong_Check(obj))
return PyLong_AsLong((PyObject *)obj);
#else
else if (PyInt_Check(obj))
return PyInt_AsLong((PyObject *)obj);
#endif
throw Base::TypeError("Float or integer expected");
}
@ -858,7 +908,7 @@ PyObject* SheetPy::getColumnWidth(PyObject *args)
try {
CellAddress address(std::string(columnStr) + "1");
return Py::new_reference_to( Py::Int( getSheetPtr()->getColumnWidth(address.col()) ) );
return Py::new_reference_to( Py::Long( getSheetPtr()->getColumnWidth(address.col()) ) );
}
catch (const Base::Exception & e) {
PyErr_SetString(PyExc_ValueError, e.what());
@ -896,7 +946,7 @@ PyObject* SheetPy::getRowHeight(PyObject *args)
try {
CellAddress address("A" + std::string(rowStr));
return Py::new_reference_to( Py::Int( getSheetPtr()->getRowHeight(address.row()) ) );
return Py::new_reference_to( Py::Long( getSheetPtr()->getRowHeight(address.row()) ) );
}
catch (const Base::Exception & e) {
PyErr_SetString(PyExc_ValueError, e.what());

View File

@ -93,15 +93,21 @@ private:
return Py::None();
}
};
PyObject* initModule()
{
return (new Module)->module().ptr();
}
} // namespace SpreadsheetGui
/* Python entry */
PyMODINIT_FUNC initSpreadsheetGui()
PyMOD_INIT_FUNC(SpreadsheetGui)
{
if (!Gui::Application::Instance) {
PyErr_SetString(PyExc_ImportError, "Cannot load Gui module in console application.");
return;
PyMOD_Return(0);
}
// instantiating the commands
@ -114,6 +120,7 @@ PyMODINIT_FUNC initSpreadsheetGui()
// add resources and reloads the translators
loadSpreadsheetResource();
new SpreadsheetGui::Module();
PyObject* mod = SpreadsheetGui::initModule();
Base::Console().Log("Loading GUI of Spreadsheet module... done\n");
PyMOD_Return(mod);
}