DataSourceWrapper.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "DataSourceWrapper.h"
  29. #include <EMP/Core/Python/Python.h>
  30. #include <EMP/Core/Python/Utilities.h>
  31. namespace EMP {
  32. namespace Core {
  33. namespace Python {
  34. DataSourceWrapper::DataSourceWrapper(PyObject* _self, const char* name) : DataSource(name)
  35. {
  36. self = _self;
  37. }
  38. DataSourceWrapper::~DataSourceWrapper()
  39. {
  40. }
  41. void DataSourceWrapper::InitialisePythonInterface()
  42. {
  43. void (DataSourceWrapper::*NotifyRowChange)(const String&) = &DataSourceWrapper::NotifyRowChange;
  44. void (DataSourceWrapper::*NotifyRowChangeParams)(const String&, int, int) = &DataSourceWrapper::NotifyRowChange;
  45. python::scope datasource_scope = python::class_<DataSource, DataSourceWrapper, boost::noncopyable>("DataSource", python::init< const char* >())
  46. .def("NotifyRowAdd", &DataSourceWrapper::NotifyRowAdd)
  47. .def("NotifyRowRemove", &DataSourceWrapper::NotifyRowRemove)
  48. .def("NotifyRowChange", NotifyRowChange)
  49. .def("NotifyRowChange", NotifyRowChangeParams)
  50. ;
  51. // Register the column names for use in python
  52. python::scope().attr("COLUMN_CHILD_SOURCE") = DataSource::CHILD_SOURCE;
  53. python::scope().attr("COLUMN_DEPTH") = DataSource::DEPTH;
  54. python::scope().attr("COLUMN_NUM_CHILDREN") = DataSource::NUM_CHILDREN;
  55. }
  56. void DataSourceWrapper::GetRow(StringList& row, const String& table, int row_index, const StringList& columns)
  57. {
  58. PyObject* callable = PyObject_GetAttrString(self, "GetRow");
  59. if (!callable)
  60. {
  61. Core::String error_message(128, "Function \"GetRow\" not found on python data source %s.", Utilities::GetPythonClassName(self).CString());
  62. Log::Message(LC_COREPYTHON, Log::LT_WARNING, "%s", error_message.CString());
  63. PyErr_SetString(PyExc_RuntimeError, error_message.CString());
  64. python::throw_error_already_set();
  65. return;
  66. }
  67. python::tuple t = python::make_tuple(table.CString(), row_index, columns);
  68. PyObject* result = PyObject_CallObject(callable, t.ptr());
  69. Py_DECREF(callable);
  70. // If it's a list, then just get the entries out of it
  71. if (result && PyList_Check(result))
  72. {
  73. int num_entries = PyList_Size(result);
  74. for (int i = 0; i < num_entries; i++)
  75. {
  76. Core::String entry;
  77. PyObject* entry_object = PyList_GetItem(result, i);
  78. if (PyString_Check(entry_object))
  79. {
  80. entry = PyString_AS_STRING(entry_object);
  81. }
  82. else if (PyInt_Check(entry_object))
  83. {
  84. int entry_int = (int)PyInt_AS_LONG(entry_object);
  85. Core::TypeConverter< int, Core::String >::Convert(entry_int, entry);
  86. }
  87. else if (PyFloat_Check(entry_object))
  88. {
  89. float entry_float = (float)PyFloat_AS_DOUBLE(entry_object);
  90. Core::TypeConverter< float, Core::String >::Convert(entry_float, entry);
  91. }
  92. else
  93. {
  94. Core::String error_message(128, "Failed to convert row %d entry %d on data source %s.", row_index, i, Utilities::GetPythonClassName(self).CString());
  95. Log::Message(LC_COREPYTHON, Log::LT_WARNING, "%s", error_message.CString());
  96. PyErr_SetString(PyExc_RuntimeError, error_message.CString());
  97. python::throw_error_already_set();
  98. }
  99. row.push_back(entry);
  100. }
  101. }
  102. else
  103. {
  104. // Print the error and restore it to the caller
  105. PyObject *type, *value, *traceback;
  106. PyErr_Fetch(&type, &value, &traceback);
  107. Py_XINCREF(type);
  108. Py_XINCREF(value);
  109. Py_XINCREF(traceback);
  110. Core::String error_message(128, "Failed to get entries for table %s row %d from python data source %s.", table.CString(), row_index, Utilities::GetPythonClassName(self).CString());
  111. Log::Message(LC_COREPYTHON, Log::LT_WARNING, "%s", error_message.CString());
  112. if (type == NULL)
  113. PyErr_SetString(PyExc_RuntimeError, error_message.CString());
  114. else
  115. PyErr_Restore(type, value, traceback);
  116. python::throw_error_already_set();
  117. }
  118. if (result)
  119. Py_DECREF(result);
  120. }
  121. int DataSourceWrapper::GetNumRows(const String& table)
  122. {
  123. int num_rows = 0;
  124. PyObject* callable = PyObject_GetAttrString(self, "GetNumRows");
  125. if (!callable)
  126. {
  127. Core::String error_message(128, "Function \"GetNumRows\" not found on python data source %s.", Utilities::GetPythonClassName(self).CString());
  128. Log::Message(LC_COREPYTHON, Log::LT_WARNING, "%s", error_message.CString());
  129. PyErr_SetString(PyExc_RuntimeError, error_message.CString());
  130. python::throw_error_already_set();
  131. return 0;
  132. }
  133. PyObject* result = PyObject_CallObject(callable, python::make_tuple(table.CString()).ptr());
  134. Py_DECREF(callable);
  135. if (result && PyInt_Check(result))
  136. {
  137. num_rows = PyInt_AsLong(result);
  138. }
  139. else
  140. {
  141. // Print the error and restore it to the caller
  142. PyObject *type, *value, *traceback;
  143. PyErr_Fetch(&type, &value, &traceback);
  144. Py_XINCREF(type);
  145. Py_XINCREF(value);
  146. Py_XINCREF(traceback);
  147. Core::String error_message(128, "Failed to get number of rows from python data source %s.", Utilities::GetPythonClassName(self).CString());
  148. Log::Message(LC_COREPYTHON, Log::LT_WARNING, "%s", error_message.CString());
  149. if (type == NULL)
  150. PyErr_SetString(PyExc_RuntimeError, error_message.CString());
  151. else
  152. PyErr_Restore(type, value, traceback);
  153. python::throw_error_already_set();
  154. }
  155. if (result)
  156. Py_DECREF(result);
  157. return num_rows;
  158. }
  159. ScriptObject DataSourceWrapper::GetScriptObject() const
  160. {
  161. return self;
  162. }
  163. }
  164. }
  165. }