summaryrefslogtreecommitdiffstats
path: root/src/python/csv.cpp
blob: 97c5d298a56d36bd0ce8d830d5effff6073ed64b (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
/* -*- 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 "csv.hpp"
#include "global.hpp"

#ifdef __ORCUS_PYTHON_CSV
#include "document.hpp"
#include "orcus/orcus_csv.hpp"
#include "orcus/spreadsheet/document.hpp"
#include "orcus/spreadsheet/factory.hpp"
#endif

namespace orcus { namespace python {

#ifdef __ORCUS_PYTHON_CSV

namespace {

py_unique_ptr read_stream_object_from_string(PyObject* args, PyObject* kwargs)
{
    static const char* kwlist[] = { "stream", nullptr };

    py_unique_ptr ret;
    PyObject* file = nullptr;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", const_cast<char**>(kwlist), &file))
        return ret;

    if (!file)
    {
        PyErr_SetString(PyExc_RuntimeError, "Invalid file object has been passed.");
        return ret;
    }

    PyObject* obj_str = nullptr;

    if (PyObject_HasAttrString(file, "read"))
    {
        PyObject* func_read = PyObject_GetAttrString(file, "read"); // new reference
        obj_str = PyObject_CallFunction(func_read, nullptr);
        Py_XDECREF(func_read);
    }

    if (!obj_str)
    {
        if (PyObject_TypeCheck(file, &PyUnicode_Type))
            obj_str = PyUnicode_FromObject(file); // new reference
    }

    if (!obj_str)
    {
        PyErr_SetString(PyExc_RuntimeError, "failed to extract bytes from this object.");
        return ret;
    }

    ret.reset(obj_str);
    return ret;
}

} // anonymous namespace

PyObject* csv_read(PyObject* /*module*/, PyObject* args, PyObject* kwargs)
{
    py_unique_ptr str = read_stream_object_from_string(args, kwargs);
    if (!str)
        return nullptr;

    try
    {
        spreadsheet::range_size_t ss{1048576, 16384};
        std::unique_ptr<spreadsheet::document> doc = std::make_unique<spreadsheet::document>(ss);
        spreadsheet::import_factory fact(*doc);
        orcus_csv app(&fact);

        Py_ssize_t n = 0;
        const char* p = PyUnicode_AsUTF8AndSize(str.get(), &n);
        app.read_stream({p, static_cast<std::string_view::size_type>(n)});

        return create_document(std::move(doc));
    }
    catch (const std::exception& e)
    {
        set_python_exception(PyExc_RuntimeError, e);
        return nullptr;
    }
}

#else

PyObject* csv_read(PyObject*, PyObject*, PyObject*)
{
    PyErr_SetString(PyExc_RuntimeError, "The csv module is not enabled.");
    return nullptr;
}

#endif

}}

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