123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- #include <Python.h>
- #include <util/python/pycppwrapper_util.h>
- #include <datasrc/client.h>
- #include <datasrc/factory.h>
- #include <datasrc/database.h>
- #include <datasrc/data_source.h>
- #include <datasrc/sqlite3_accessor.h>
- #include <datasrc/iterator.h>
- #include <dns/python/name_python.h>
- #include <dns/python/rrset_python.h>
- #include <dns/python/pydnspp_common.h>
- #include "datasrc.h"
- #include "client_python.h"
- #include "finder_python.h"
- #include "iterator_python.h"
- #include "updater_python.h"
- #include "journal_reader_python.h"
- #include "client_inc.cc"
- using namespace std;
- using namespace isc::util::python;
- using namespace isc::dns::python;
- using namespace isc::datasrc;
- using namespace isc::datasrc::python;
- namespace {
- class s_DataSourceClient : public PyObject {
- public:
- s_DataSourceClient() : cppobj(NULL) {};
- DataSourceClientContainer* cppobj;
- };
- PyObject*
- DataSourceClient_findZone(PyObject* po_self, PyObject* args) {
- s_DataSourceClient* const self = static_cast<s_DataSourceClient*>(po_self);
- PyObject *name;
- if (PyArg_ParseTuple(args, "O!", &name_type, &name)) {
- try {
- DataSourceClient::FindResult find_result(
- self->cppobj->getInstance().findZone(PyName_ToName(name)));
- result::Result r = find_result.code;
- ZoneFinderPtr zfp = find_result.zone_finder;
-
- return (Py_BuildValue("IN", r, createZoneFinderObject(zfp, po_self)));
- } catch (const std::exception& exc) {
- PyErr_SetString(getDataSourceException("Error"), exc.what());
- return (NULL);
- } catch (...) {
- PyErr_SetString(getDataSourceException("Error"),
- "Unexpected exception");
- return (NULL);
- }
- } else {
- return (NULL);
- }
- }
- PyObject*
- DataSourceClient_getIterator(PyObject* po_self, PyObject* args) {
- s_DataSourceClient* const self = static_cast<s_DataSourceClient*>(po_self);
- PyObject* name_obj;
- PyObject* separate_rrs_obj = NULL;
- if (PyArg_ParseTuple(args, "O!|O", &name_type, &name_obj,
- &separate_rrs_obj)) {
- try {
- bool separate_rrs = false;
- if (separate_rrs_obj != NULL) {
-
-
- int separate_rrs_true = PyObject_IsTrue(separate_rrs_obj);
- if (separate_rrs_true == 1) {
- separate_rrs = true;
- } else if (separate_rrs_true == -1) {
- PyErr_SetString(getDataSourceException("Error"),
- "Error getting value of separate_rrs");
- return (NULL);
- }
- }
- return (createZoneIteratorObject(
- self->cppobj->getInstance().getIterator(PyName_ToName(name_obj),
- separate_rrs),
- po_self));
- } catch (const isc::NotImplemented& ne) {
- PyErr_SetString(getDataSourceException("NotImplemented"),
- ne.what());
- return (NULL);
- } catch (const DataSourceError& dse) {
- PyErr_SetString(getDataSourceException("Error"), dse.what());
- return (NULL);
- } catch (const std::exception& exc) {
- PyErr_SetString(getDataSourceException("Error"), exc.what());
- return (NULL);
- } catch (...) {
- PyErr_SetString(getDataSourceException("Error"),
- "Unexpected exception");
- return (NULL);
- }
- } else {
- return (NULL);
- }
- }
- PyObject*
- DataSourceClient_getUpdater(PyObject* po_self, PyObject* args) {
- s_DataSourceClient* const self = static_cast<s_DataSourceClient*>(po_self);
- PyObject *name_obj;
- PyObject *replace_obj = NULL;
- PyObject *journaling_obj = Py_False;
- if (PyArg_ParseTuple(args, "O!O|O", &name_type, &name_obj,
- &replace_obj, &journaling_obj) &&
- PyBool_Check(replace_obj) && PyBool_Check(journaling_obj)) {
- const bool replace = (replace_obj != Py_False);
- const bool journaling = (journaling_obj == Py_True);
- try {
- ZoneUpdaterPtr updater =
- self->cppobj->getInstance().getUpdater(PyName_ToName(name_obj),
- replace, journaling);
- if (!updater) {
- return (Py_None);
- }
- return (createZoneUpdaterObject(updater, po_self));
- } catch (const isc::NotImplemented& ne) {
- PyErr_SetString(getDataSourceException("NotImplemented"),
- ne.what());
- return (NULL);
- } catch (const DataSourceError& dse) {
- PyErr_SetString(getDataSourceException("Error"), dse.what());
- return (NULL);
- } catch (const std::exception& exc) {
- PyErr_SetString(getDataSourceException("Error"), exc.what());
- return (NULL);
- } catch (...) {
- PyErr_SetString(getDataSourceException("Error"),
- "Unexpected exception");
- return (NULL);
- }
- } else {
-
- if (replace_obj != NULL && !PyBool_Check(replace_obj)) {
- PyErr_SetString(PyExc_TypeError, "'replace' for "
- "DataSourceClient.get_updater must be boolean");
- }
- if (!PyBool_Check(journaling_obj)) {
- PyErr_SetString(PyExc_TypeError, "'journaling' for "
- "DataSourceClient.get_updater must be boolean");
- }
- return (NULL);
- }
- }
- PyObject*
- DataSourceClient_getJournalReader(PyObject* po_self, PyObject* args) {
- s_DataSourceClient* const self = static_cast<s_DataSourceClient*>(po_self);
- PyObject *name_obj;
- unsigned long begin_obj, end_obj;
- if (PyArg_ParseTuple(args, "O!kk", &name_type, &name_obj,
- &begin_obj, &end_obj)) {
- try {
- pair<ZoneJournalReader::Result, ZoneJournalReaderPtr> result =
- self->cppobj->getInstance().getJournalReader(
- PyName_ToName(name_obj), static_cast<uint32_t>(begin_obj),
- static_cast<uint32_t>(end_obj));
- PyObject* po_reader;
- if (result.first == ZoneJournalReader::SUCCESS) {
- po_reader = createZoneJournalReaderObject(result.second,
- po_self);
- } else {
- po_reader = Py_None;
- Py_INCREF(po_reader);
- }
- PyObjectContainer container(po_reader);
- return (Py_BuildValue("(iO)", result.first, container.get()));
- } catch (const isc::NotImplemented& ex) {
- PyErr_SetString(getDataSourceException("NotImplemented"),
- ex.what());
- } catch (const DataSourceError& ex) {
- PyErr_SetString(getDataSourceException("Error"), ex.what());
- } catch (const std::exception& ex) {
- PyErr_SetString(PyExc_SystemError, ex.what());
- } catch (...) {
- PyErr_SetString(PyExc_SystemError, "Unexpected exception");
- }
- }
- return (NULL);
- }
- PyMethodDef DataSourceClient_methods[] = {
- { "find_zone", DataSourceClient_findZone, METH_VARARGS,
- DataSourceClient_findZone_doc },
- { "get_iterator",
- DataSourceClient_getIterator, METH_VARARGS,
- DataSourceClient_getIterator_doc },
- { "get_updater", DataSourceClient_getUpdater,
- METH_VARARGS, DataSourceClient_getUpdater_doc },
- { "get_journal_reader", DataSourceClient_getJournalReader,
- METH_VARARGS, DataSourceClient_getJournalReader_doc },
- { NULL, NULL, 0, NULL }
- };
- int
- DataSourceClient_init(PyObject* po_self, PyObject* args, PyObject*) {
- s_DataSourceClient* self = static_cast<s_DataSourceClient*>(po_self);
- char* ds_type_str;
- char* ds_config_str;
- try {
-
-
-
- if (PyArg_ParseTuple(args, "ss", &ds_type_str, &ds_config_str)) {
- isc::data::ConstElementPtr ds_config =
- isc::data::Element::fromJSON(ds_config_str);
- self->cppobj = new DataSourceClientContainer(ds_type_str,
- ds_config);
- return (0);
- } else {
- return (-1);
- }
- } catch (const isc::data::JSONError& je) {
- const string ex_what = "JSON parse error in data source configuration "
- "data for type " +
- string(ds_type_str) + ":" + je.what();
- PyErr_SetString(getDataSourceException("Error"), ex_what.c_str());
- return (-1);
- } catch (const DataSourceError& dse) {
- const string ex_what = "Failed to create DataSourceClient of type " +
- string(ds_type_str) + ":" + dse.what();
- PyErr_SetString(getDataSourceException("Error"), ex_what.c_str());
- return (-1);
- } catch (const exception& ex) {
- const string ex_what = "Failed to construct DataSourceClient object: " +
- string(ex.what());
- PyErr_SetString(getDataSourceException("Error"), ex_what.c_str());
- return (-1);
- } catch (...) {
- PyErr_SetString(PyExc_RuntimeError,
- "Unexpected exception in constructing DataSourceClient");
- return (-1);
- }
- PyErr_SetString(PyExc_TypeError,
- "Invalid arguments to DataSourceClient constructor");
- return (-1);
- }
- void
- DataSourceClient_destroy(PyObject* po_self) {
- s_DataSourceClient* const self = static_cast<s_DataSourceClient*>(po_self);
- delete self->cppobj;
- self->cppobj = NULL;
- Py_TYPE(self)->tp_free(self);
- }
- }
- namespace isc {
- namespace datasrc {
- namespace python {
- PyTypeObject datasourceclient_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "datasrc.DataSourceClient",
- sizeof(s_DataSourceClient),
- 0,
- DataSourceClient_destroy,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- Py_TPFLAGS_DEFAULT,
- DataSourceClient_doc,
- NULL,
- NULL,
- NULL,
- 0,
- NULL,
- NULL,
- DataSourceClient_methods,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0,
- DataSourceClient_init,
- NULL,
- PyType_GenericNew,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- 0
- };
- }
- }
- }
|