DataSourceWrapper.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 "../../../Include/Rocket/Core/Log.h"
  30. #include "../../../Include/Rocket/Core/Python/Utilities.h"
  31. namespace Rocket {
  32. namespace Controls {
  33. namespace Python {
  34. // Returns the string representation of a python class name
  35. static Core::String GetPythonClassName(PyObject* object)
  36. {
  37. Core::String full_name;
  38. // Check if the object is a class; either a standard Python class or a Boost Python class metatype. If so, we can
  39. // query the name of the class object directly; otherwise, it is an object that we have to query the class type
  40. // from.
  41. bool is_class = PyClass_Check(object) ||
  42. object->ob_type == python::objects::class_metatype().get();
  43. PyObject* py_class = NULL;
  44. if (!is_class)
  45. {
  46. py_class = PyObject_GetAttrString(object, "__class__");
  47. object = py_class;
  48. if (!object)
  49. {
  50. PyErr_Clear();
  51. return full_name;
  52. }
  53. }
  54. PyObject* py_class_name = PyObject_GetAttrString(object, "__name__");
  55. if (py_class_name != NULL)
  56. {
  57. const char* class_name = PyString_AsString(py_class_name);
  58. PyObject* py_module_name = PyObject_GetAttrString(object, "__module__");
  59. const char* module_name = PyString_AsString(py_module_name);
  60. full_name.FormatString(128, "%s.%s", module_name, class_name);
  61. Py_DECREF(py_module_name);
  62. Py_DECREF(py_class_name);
  63. }
  64. else
  65. {
  66. PyErr_Clear();
  67. }
  68. if (py_class != NULL)
  69. Py_DECREF(py_class);
  70. return full_name;
  71. }
  72. DataSourceWrapper::DataSourceWrapper(PyObject* _self, const char* name) : DataSource(name)
  73. {
  74. self = _self;
  75. }
  76. DataSourceWrapper::~DataSourceWrapper()
  77. {
  78. }
  79. void DataSourceWrapper::InitialisePythonInterface()
  80. {
  81. void (DataSourceWrapper::*NotifyRowChange)(const Core::String&) = &DataSourceWrapper::NotifyRowChange;
  82. void (DataSourceWrapper::*NotifyRowChangeParams)(const Core::String&, int, int) = &DataSourceWrapper::NotifyRowChange;
  83. python::scope datasource_scope = python::class_<DataSource, DataSourceWrapper, boost::noncopyable>("DataSource", python::init< const char* >())
  84. .def("NotifyRowAdd", &DataSourceWrapper::NotifyRowAdd)
  85. .def("NotifyRowRemove", &DataSourceWrapper::NotifyRowRemove)
  86. .def("NotifyRowChange", NotifyRowChange)
  87. .def("NotifyRowChange", NotifyRowChangeParams)
  88. ;
  89. // Register the column names for use in python
  90. python::scope().attr("COLUMN_CHILD_SOURCE") = DataSource::CHILD_SOURCE;
  91. python::scope().attr("COLUMN_DEPTH") = DataSource::DEPTH;
  92. python::scope().attr("COLUMN_NUM_CHILDREN") = DataSource::NUM_CHILDREN;
  93. }
  94. void DataSourceWrapper::GetRow(Core::StringList& row, const Core::String& table, int row_index, const Core::StringList& columns)
  95. {
  96. PyObject* callable = PyObject_GetAttrString(self, "GetRow");
  97. if (!callable)
  98. {
  99. Core::String error_message(128, "Function \"GetRow\" not found on python data source %s.", GetPythonClassName(self).CString());
  100. Core::Log::Message(Core::Log::LT_WARNING, "%s", error_message.CString());
  101. PyErr_SetString(PyExc_RuntimeError, error_message.CString());
  102. python::throw_error_already_set();
  103. return;
  104. }
  105. python::tuple t = python::make_tuple(table.CString(), row_index, columns);
  106. PyObject* result = PyObject_CallObject(callable, t.ptr());
  107. Py_DECREF(callable);
  108. // If it's a list, then just get the entries out of it
  109. if (result && PyList_Check(result))
  110. {
  111. int num_entries = PyList_Size(result);
  112. for (int i = 0; i < num_entries; i++)
  113. {
  114. Core::String entry;
  115. PyObject* entry_object = PyList_GetItem(result, i);
  116. if (PyString_Check(entry_object))
  117. {
  118. entry = PyString_AS_STRING(entry_object);
  119. }
  120. else if (PyInt_Check(entry_object))
  121. {
  122. int entry_int = (int)PyInt_AS_LONG(entry_object);
  123. Core::TypeConverter< int, Core::String >::Convert(entry_int, entry);
  124. }
  125. else if (PyFloat_Check(entry_object))
  126. {
  127. float entry_float = (float)PyFloat_AS_DOUBLE(entry_object);
  128. Core::TypeConverter< float, Core::String >::Convert(entry_float, entry);
  129. }
  130. else
  131. {
  132. Core::String error_message(128, "Failed to convert row %d entry %d on data source %s.", row_index, i, GetPythonClassName(self).CString());
  133. Core::Log::Message(Core::Log::LT_WARNING, "%s", error_message.CString());
  134. PyErr_SetString(PyExc_RuntimeError, error_message.CString());
  135. python::throw_error_already_set();
  136. }
  137. row.push_back(entry);
  138. }
  139. }
  140. else
  141. {
  142. // Print the error and restore it to the caller
  143. PyObject *type, *value, *traceback;
  144. PyErr_Fetch(&type, &value, &traceback);
  145. Py_XINCREF(type);
  146. Py_XINCREF(value);
  147. Py_XINCREF(traceback);
  148. Core::String error_message(128, "Failed to get entries for table %s row %d from python data source %s.", table.CString(), row_index, GetPythonClassName(self).CString());
  149. Core::Log::Message(Core::Log::LT_WARNING, "%s", error_message.CString());
  150. if (type == NULL)
  151. PyErr_SetString(PyExc_RuntimeError, error_message.CString());
  152. else
  153. PyErr_Restore(type, value, traceback);
  154. python::throw_error_already_set();
  155. }
  156. if (result)
  157. Py_DECREF(result);
  158. }
  159. int DataSourceWrapper::GetNumRows(const Core::String& table)
  160. {
  161. int num_rows = 0;
  162. PyObject* callable = PyObject_GetAttrString(self, "GetNumRows");
  163. if (!callable)
  164. {
  165. Core::String error_message(128, "Function \"GetNumRows\" not found on python data source %s.", GetPythonClassName(self).CString());
  166. Core::Log::Message(Core::Log::LT_WARNING, "%s", error_message.CString());
  167. PyErr_SetString(PyExc_RuntimeError, error_message.CString());
  168. python::throw_error_already_set();
  169. return 0;
  170. }
  171. PyObject* result = PyObject_CallObject(callable, python::make_tuple(table.CString()).ptr());
  172. Py_DECREF(callable);
  173. if (result && PyInt_Check(result))
  174. {
  175. num_rows = PyInt_AsLong(result);
  176. }
  177. else
  178. {
  179. // Print the error and restore it to the caller
  180. PyObject *type, *value, *traceback;
  181. PyErr_Fetch(&type, &value, &traceback);
  182. Py_XINCREF(type);
  183. Py_XINCREF(value);
  184. Py_XINCREF(traceback);
  185. Core::String error_message(128, "Failed to get number of rows from python data source %s.", GetPythonClassName(self).CString());
  186. Core::Log::Message(Core::Log::LT_WARNING, "%s", error_message.CString());
  187. if (type == NULL)
  188. PyErr_SetString(PyExc_RuntimeError, error_message.CString());
  189. else
  190. PyErr_Restore(type, value, traceback);
  191. python::throw_error_already_set();
  192. }
  193. if (result)
  194. Py_DECREF(result);
  195. return num_rows;
  196. }
  197. Core::ScriptObject DataSourceWrapper::GetScriptObject() const
  198. {
  199. return self;
  200. }
  201. }
  202. }
  203. }