/python/tdnfpyrepodata.c

https://github.com/vmware/tdnf · C · 289 lines · 246 code · 36 blank · 7 comment · 18 complexity · e3386606248212880b253fdf96b0b7b6 MD5 · raw file

  1. /*
  2. * Copyright (C) 2019-2021 VMware, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the GNU General Public License v2 (the "License");
  5. * you may not use this file except in compliance with the License. The terms
  6. * of the License are located in the COPYING file of this distribution.
  7. */
  8. #include "includes.h"
  9. #include "tdnfpyrepodata.h"
  10. static char repodata__doc__[] = "";
  11. static void
  12. TDNFPyRepoDataFree(PY_TDNF_REPODATA *self)
  13. {
  14. Py_XDECREF(self->id);
  15. Py_XDECREF(self->name);
  16. Py_XDECREF(self->baseurl);
  17. Py_XDECREF(self->metalink);
  18. Py_TYPE(self)->tp_free((PyObject*)self);
  19. }
  20. static PyObject *
  21. TDNFPyRepoDataNew(
  22. PyTypeObject *type,
  23. PyObject *args,
  24. PyObject *kwds)
  25. {
  26. uint32_t dwError = 0;
  27. PPY_TDNF_REPODATA self = NULL;
  28. self = (PPY_TDNF_REPODATA)type->tp_alloc(type, 0);
  29. if (self != NULL)
  30. {
  31. if(!(self->id = PyBytes_FromString("")))
  32. {
  33. dwError = ERROR_TDNF_OUT_OF_MEMORY;
  34. BAIL_ON_TDNF_ERROR(dwError);
  35. }
  36. if(!(self->name = PyBytes_FromString("")))
  37. {
  38. dwError = ERROR_TDNF_OUT_OF_MEMORY;
  39. BAIL_ON_TDNF_ERROR(dwError);
  40. }
  41. if(!(self->baseurl = PyBytes_FromString("")))
  42. {
  43. dwError = ERROR_TDNF_OUT_OF_MEMORY;
  44. BAIL_ON_TDNF_ERROR(dwError);
  45. }
  46. if(!(self->metalink = PyBytes_FromString("")))
  47. {
  48. dwError = ERROR_TDNF_OUT_OF_MEMORY;
  49. BAIL_ON_TDNF_ERROR(dwError);
  50. }
  51. }
  52. cleanup:
  53. return (PyObject *)self;
  54. error:
  55. Py_DECREF(self);
  56. self = NULL;
  57. goto cleanup;
  58. }
  59. static int
  60. TDNFPyRepoDataInit(
  61. PY_TDNF_REPODATA *self,
  62. PyObject *args,
  63. PyObject *kwds
  64. )
  65. {
  66. uint32_t dwError = 0;
  67. PyObject *id = NULL;
  68. PyObject *name = NULL;
  69. PyObject *baseurl = NULL;
  70. PyObject *metalink = NULL;
  71. PyObject *tmp = NULL;
  72. static char *kwlist[] = {"id", "name", "baseurl", "metalink", "enabled", NULL};
  73. if (! PyArg_ParseTupleAndKeywords(args, kwds, "|SSSI", kwlist,
  74. &id, &name, &baseurl, &metalink, &self->enabled))
  75. {
  76. dwError = ERROR_TDNF_INVALID_PARAMETER;
  77. BAIL_ON_TDNF_ERROR(dwError);
  78. }
  79. if (id)
  80. {
  81. tmp = self->id;
  82. Py_INCREF(id);
  83. self->id = id;
  84. Py_XDECREF(tmp);
  85. }
  86. if (name)
  87. {
  88. tmp = self->name;
  89. Py_INCREF(name);
  90. self->name = name;
  91. Py_XDECREF(tmp);
  92. }
  93. if (baseurl)
  94. {
  95. tmp = self->baseurl;
  96. Py_INCREF(baseurl);
  97. self->baseurl = baseurl;
  98. Py_XDECREF(tmp);
  99. }
  100. if (metalink)
  101. {
  102. tmp = self->metalink;
  103. Py_INCREF(metalink);
  104. self->metalink = metalink;
  105. Py_XDECREF(tmp);
  106. }
  107. cleanup:
  108. return dwError > 0 ? -1 : 0;
  109. error:
  110. pr_err("Error = %u\n", dwError);
  111. goto cleanup;
  112. }
  113. PyObject*
  114. TDNFPyRepoDataRepr(
  115. PyObject *self
  116. )
  117. {
  118. char *pszRepr = NULL;
  119. uint32_t dwError = 0;
  120. PyObject *pyRepr = Py_None;
  121. PPY_TDNF_REPODATA pRepoData = NULL;
  122. pRepoData = (PPY_TDNF_REPODATA)self;
  123. dwError = TDNFAllocateStringPrintf(
  124. &pszRepr,
  125. "{id: %s, name: %s, baseurl: %s, metalink: %s, enabled: %d}",
  126. pRepoData->id ? PyBytes_AsString(pRepoData->id) : "''",
  127. pRepoData->name ? PyBytes_AsString(pRepoData->name) : "''",
  128. pRepoData->baseurl ? PyBytes_AsString(pRepoData->baseurl) : "''",
  129. pRepoData->metalink ? PyBytes_AsString(pRepoData->metalink) : "''",
  130. pRepoData->enabled);
  131. BAIL_ON_TDNF_ERROR(dwError);
  132. pyRepr = Py_BuildValue("s", pszRepr);
  133. Py_INCREF(pyRepr);
  134. cleanup:
  135. TDNF_SAFE_FREE_MEMORY(pszRepr);
  136. return pyRepr;
  137. error:
  138. pr_crit("Error = %u\n", dwError);
  139. pyRepr = Py_None;
  140. goto cleanup;
  141. }
  142. PyObject*
  143. TDNFPyRepoDataStr(
  144. PyObject *self
  145. )
  146. {
  147. return TDNFPyRepoDataRepr(self);
  148. }
  149. uint32_t
  150. TDNFPyMakeRepoData(
  151. PTDNF_REPO_DATA pRepoData,
  152. PyObject **ppPyRepoData
  153. )
  154. {
  155. uint32_t dwError = 0;
  156. PPY_TDNF_REPODATA pPyRepoData = NULL;
  157. PyTypeObject *retType = &repodataType;
  158. if(!pRepoData || !ppPyRepoData)
  159. {
  160. dwError = ERROR_TDNF_INVALID_PARAMETER;
  161. BAIL_ON_TDNF_ERROR(dwError);
  162. }
  163. pPyRepoData = (PPY_TDNF_REPODATA)retType->tp_alloc(retType, 0);
  164. if(!pPyRepoData)
  165. {
  166. dwError = ERROR_TDNF_OUT_OF_MEMORY;
  167. BAIL_ON_TDNF_ERROR(dwError);
  168. }
  169. if (pRepoData->pszId)
  170. {
  171. pPyRepoData->id = PyBytes_FromString(pRepoData->pszId);
  172. }
  173. if (pRepoData->pszName)
  174. {
  175. pPyRepoData->name = PyBytes_FromString(pRepoData->pszName);
  176. }
  177. if (pRepoData->pszBaseUrl)
  178. {
  179. pPyRepoData->baseurl = PyBytes_FromString(pRepoData->pszBaseUrl);
  180. }
  181. if (pRepoData->pszMetaLink)
  182. {
  183. pPyRepoData->metalink = PyBytes_FromString(pRepoData->pszMetaLink);
  184. }
  185. pPyRepoData->enabled = pRepoData->nEnabled;
  186. *ppPyRepoData = (PyObject *)pPyRepoData;
  187. cleanup:
  188. return dwError;
  189. error:
  190. Py_XDECREF(pPyRepoData);
  191. goto cleanup;
  192. }
  193. static PyGetSetDef TDNFPyRepoDataGetSet[] = {
  194. {NULL} /* Sentinel */
  195. };
  196. static PyMethodDef TDNFPyRepoDataMethods[] = {
  197. {NULL, NULL, 0, NULL} /* Sentinel */
  198. };
  199. static PyMemberDef TDNFPyRepoDataMembers[] = {
  200. {"id", T_OBJECT_EX, offsetof(PY_TDNF_REPODATA, id), 0,
  201. "repo id"},
  202. {"name", T_OBJECT_EX, offsetof(PY_TDNF_REPODATA, name), 0,
  203. "repo name"},
  204. {"baseurl", T_OBJECT_EX, offsetof(PY_TDNF_REPODATA, baseurl), 0,
  205. "repo baseurl"},
  206. {"metalink", T_OBJECT_EX, offsetof(PY_TDNF_REPODATA, metalink), 0,
  207. "repo metalink"},
  208. {"enabled", T_INT, offsetof(PY_TDNF_REPODATA, enabled), 0,
  209. "repo enabled status"},
  210. {NULL} /* Sentinel */
  211. };
  212. PyTypeObject repodataType = {
  213. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  214. "repodata", /*tp_name*/
  215. sizeof(PY_TDNF_REPODATA), /*tp_basicsize*/
  216. 0, /*tp_itemsize*/
  217. (destructor)TDNFPyRepoDataFree, /*tp_dealloc*/
  218. 0, /*tp_print*/
  219. 0, /*tp_getattr*/
  220. 0, /*tp_setattr*/
  221. 0, /*tp_compare*/
  222. TDNFPyRepoDataRepr, /*tp_repr*/
  223. 0, /*tp_as_number*/
  224. 0, /*tp_as_sequence*/
  225. 0, /*tp_as_mapping*/
  226. 0, /*tp_hash */
  227. 0, /*tp_call*/
  228. TDNFPyRepoDataStr, /*tp_str*/
  229. 0, /*tp_getattro*/
  230. 0, /*tp_setattro*/
  231. 0, /*tp_as_buffer*/
  232. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
  233. repodata__doc__, /* tp_doc */
  234. 0, /* tp_traverse */
  235. 0, /* tp_clear */
  236. 0, /* tp_richcompare */
  237. 0, /* tp_weaklistoffset */
  238. 0, /* tp_iter */
  239. 0, /* tp_iternext */
  240. TDNFPyRepoDataMethods, /* tp_methods */
  241. TDNFPyRepoDataMembers, /* tp_members */
  242. TDNFPyRepoDataGetSet, /* tp_getset */
  243. 0, /* tp_base */
  244. 0, /* tp_dict */
  245. 0, /* tp_descr_get */
  246. 0, /* tp_descr_set */
  247. 0, /* tp_dictoffset */
  248. (initproc)TDNFPyRepoDataInit, /* tp_init */
  249. 0, /* tp_alloc */
  250. TDNFPyRepoDataNew, /* tp_new */
  251. 0, /* tp_free */
  252. 0 /* tp_is_gc */
  253. };