zone_loader_python.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // Enable this if you use s# variants with PyArg_ParseTuple(), see
  15. // http://docs.python.org/py3k/c-api/arg.html#strings-and-buffers
  16. //#define PY_SSIZE_T_CLEAN
  17. // Python.h needs to be placed at the head of the program file, see:
  18. // http://docs.python.org/py3k/extending/extending.html#a-simple-example
  19. #include <Python.h>
  20. #include <util/python/pycppwrapper_util.h>
  21. #include <datasrc/client.h>
  22. #include <datasrc/database.h>
  23. #include <datasrc/data_source.h>
  24. #include <datasrc/sqlite3_accessor.h>
  25. #include <datasrc/iterator.h>
  26. #include <datasrc/zone.h>
  27. #include <datasrc/zone_loader.h>
  28. #include <dns/python/name_python.h>
  29. //#include <dns/python/rrset_python.h>
  30. //#include <dns/python/rrclass_python.h>
  31. //#include <dns/python/rrtype_python.h>
  32. #include <dns/python/pydnspp_common.h>
  33. #include "client_python.h"
  34. #include "datasrc.h"
  35. #include "zone_loader_inc.cc"
  36. using namespace std;
  37. using namespace isc::util::python;
  38. using namespace isc::dns::python;
  39. using namespace isc::datasrc;
  40. using namespace isc::datasrc::python;
  41. namespace {
  42. // The s_* Class simply covers one instantiation of the object
  43. class s_ZoneLoader : public PyObject {
  44. public:
  45. s_ZoneLoader() : cppobj(NULL), client(NULL) {};
  46. ZoneLoader* cppobj;
  47. // a zoneloader should not survive its associated client,
  48. // so add a ref to it at init
  49. PyObject* client;
  50. };
  51. // Shortcut type which would be convenient for adding class variables safely.
  52. typedef CPPPyObjectContainer<s_ZoneLoader, ZoneLoader> ZoneLoaderContainer;
  53. // General creation and destruction
  54. int
  55. ZoneLoader_init(PyObject* po_self, PyObject* args, PyObject*) {
  56. s_ZoneLoader* self = static_cast<s_ZoneLoader*>(po_self);
  57. PyObject *po_ds_client;
  58. PyObject *po_name;
  59. char* master_file;
  60. if (PyArg_ParseTuple(args, "O!O!s", &datasourceclient_type, &po_ds_client, &name_type, &po_name, &master_file)) {
  61. try {
  62. Py_INCREF(po_ds_client);
  63. self->client = po_ds_client;
  64. self->cppobj = new ZoneLoader(PyDataSourceClient_ToDataSourceClient(po_ds_client), PyName_ToName(po_name), master_file);
  65. return (0);
  66. } catch (const isc::datasrc::MasterFileError& mfe) {
  67. PyErr_SetString(getDataSourceException("MasterFileError"), mfe.what());
  68. } catch (const isc::datasrc::DataSourceError& dse) {
  69. PyErr_SetString(getDataSourceException("Error"), dse.what());
  70. } catch (const isc::NotImplemented& ni) {
  71. PyErr_SetString(getDataSourceException("NotImplemented"), ni.what());
  72. } catch (const std::exception& stde) {
  73. PyErr_SetString(getDataSourceException("Error"), stde.what());
  74. } catch (...) {
  75. PyErr_SetString(getDataSourceException("Error"),
  76. "Unexpected exception");
  77. }
  78. }
  79. return (-1);
  80. }
  81. void
  82. ZoneLoader_destroy(PyObject* po_self) {
  83. s_ZoneLoader* self = static_cast<s_ZoneLoader*>(po_self);
  84. delete self->cppobj;
  85. self->cppobj = NULL;
  86. if (self->client != NULL) {
  87. Py_DECREF(self->client);
  88. }
  89. Py_TYPE(self)->tp_free(self);
  90. }
  91. PyObject* ZoneLoader_load(PyObject* po_self, PyObject*) {
  92. s_ZoneLoader* self = static_cast<s_ZoneLoader*>(po_self);
  93. try {
  94. self->cppobj->load();
  95. Py_RETURN_NONE;
  96. } catch (const isc::datasrc::MasterFileError& mfe) {
  97. PyErr_SetString(getDataSourceException("MasterFileError"), mfe.what());
  98. return (NULL);
  99. } catch (const std::exception& exc) {
  100. PyErr_SetString(getDataSourceException("Error"), exc.what());
  101. return (NULL);
  102. } catch (...) {
  103. PyErr_SetString(getDataSourceException("Error"),
  104. "Unexpected exception");
  105. return (NULL);
  106. }
  107. }
  108. // This list contains the actual set of functions we have in
  109. // python. Each entry has
  110. // 1. Python method name
  111. // 2. Our static function here
  112. // 3. Argument type
  113. // 4. Documentation
  114. PyMethodDef ZoneLoader_methods[] = {
  115. /*
  116. { "get_origin", ZoneLoader_getOrigin, METH_NOARGS,
  117. ZoneLoader_getOrigin_doc },
  118. { "get_class", ZoneLoader_getClass, METH_NOARGS, ZoneLoader_getClass_doc },
  119. { "find", ZoneLoader_find, METH_VARARGS, ZoneLoader_find_doc },
  120. { "find_all", ZoneLoader_find_all, METH_VARARGS, ZoneLoader_findAll_doc },
  121. */
  122. { "load", ZoneLoader_load, METH_NOARGS, ZoneLoader_load_doc },
  123. { NULL, NULL, 0, NULL }
  124. };
  125. } // end of unnamed namespace
  126. namespace isc {
  127. namespace datasrc {
  128. namespace python {
  129. PyTypeObject zone_loader_type = {
  130. PyVarObject_HEAD_INIT(NULL, 0)
  131. "datasrc.ZoneLoader",
  132. sizeof(s_ZoneLoader), // tp_basicsize
  133. 0, // tp_itemsize
  134. ZoneLoader_destroy, // tp_dealloc
  135. NULL, // tp_print
  136. NULL, // tp_getattr
  137. NULL, // tp_setattr
  138. NULL, // tp_reserved
  139. NULL, // tp_repr
  140. NULL, // tp_as_number
  141. NULL, // tp_as_sequence
  142. NULL, // tp_as_mapping
  143. NULL, // tp_hash
  144. NULL, // tp_call
  145. NULL, // tp_str
  146. NULL, // tp_getattro
  147. NULL, // tp_setattro
  148. NULL, // tp_as_buffer
  149. Py_TPFLAGS_DEFAULT, // tp_flags
  150. ZoneLoader_doc,
  151. NULL, // tp_traverse
  152. NULL, // tp_clear
  153. NULL, // tp_richcompare
  154. 0, // tp_weaklistoffset
  155. NULL, // tp_iter
  156. NULL, // tp_iternext
  157. ZoneLoader_methods, // tp_methods
  158. NULL, // tp_members
  159. NULL, // tp_getset
  160. NULL, // tp_base
  161. NULL, // tp_dict
  162. NULL, // tp_descr_get
  163. NULL, // tp_descr_set
  164. 0, // tp_dictoffset
  165. ZoneLoader_init, // tp_init
  166. NULL, // tp_alloc
  167. PyType_GenericNew, // tp_new
  168. NULL, // tp_free
  169. NULL, // tp_is_gc
  170. NULL, // tp_bases
  171. NULL, // tp_mro
  172. NULL, // tp_cache
  173. NULL, // tp_subclasses
  174. NULL, // tp_weaklist
  175. NULL, // tp_del
  176. 0 // tp_version_tag
  177. };
  178. PyObject* po_MasterFileError;
  179. } // namespace python
  180. } // namespace datasrc
  181. } // namespace isc