[yast-commit] r41102 - in /trunk/python-bindings/src: YCPTypes.cc YCPTypes.h YCPTypes/ YCPTypes/Path.cc YCPTypes/Symbol.cc YCPTypes/Term.cc YCPTypes/YCPTypesInternal.cc YCPTypes/YCPTypesInternal.h
![](https://seccdn.libravatar.org/avatar/63797a3a1edb765c207a8bc1cdc132e5.jpg?s=120&d=mm&r=g)
Author: dfiser Date: Wed Sep 26 11:35:28 2007 New Revision: 41102 URL: http://svn.opensuse.org/viewcvs/yast?rev=41102&view=rev Log: Added files with defined YCP types (Term, Symbol, Path) for usage in Python. Added: trunk/python-bindings/src/YCPTypes/ trunk/python-bindings/src/YCPTypes.cc trunk/python-bindings/src/YCPTypes.h trunk/python-bindings/src/YCPTypes/Path.cc trunk/python-bindings/src/YCPTypes/Symbol.cc trunk/python-bindings/src/YCPTypes/Term.cc trunk/python-bindings/src/YCPTypes/YCPTypesInternal.cc trunk/python-bindings/src/YCPTypes/YCPTypesInternal.h Added: trunk/python-bindings/src/YCPTypes.cc URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCPTypes.cc?r... ============================================================================== --- trunk/python-bindings/src/YCPTypes.cc (added) +++ trunk/python-bindings/src/YCPTypes.cc Wed Sep 26 11:35:28 2007 @@ -0,0 +1,105 @@ +#include "YCPTypes.h" +using std::string; + +bool initYCPTypes(PyObject *module) +{ + if (PyType_Ready(&SymbolType) < 0) + return false; + if (PyType_Ready(&PathType) < 0) + return false; + if (PyType_Ready(&TermType) < 0) + return false; + + Py_INCREF(&SymbolType); + PyModule_AddObject(module, "Symbol", (PyObject *)&SymbolType); + Py_INCREF(&PathType); + PyModule_AddObject(module, "Path", (PyObject *)&PathType); + Py_INCREF(&TermType); + PyModule_AddObject(module, "Term", (PyObject *)&TermType); + + return true; +} + +bool isYCPType(PyObject *obj) +{ + if (isSymbol(obj) || isPath(obj) || isTerm(obj)) + return true; + return false; +} + +YCPType getType(PyObject *obj) +{ + if (isSymbol(obj)) + return SYMBOL; + if (isPath(obj)) + return PATH; + if (isTerm(obj)) + return TERM; + return NOT_YCP_TYPE; +} + + +/***** Symbol *****/ +bool isSymbol(PyObject *obj) +{ + if (PyInstance_Check(obj) + && PyObject_IsInstance(obj, (PyObject *)&SymbolType)) + return true; + return false; +} + +string Symbol_getValue(Symbol *obj) +{ + if (isSymbol((PyObject *)obj)){ + return string(PyString_AsString(obj->value)); + } + return string(); +} +/***** Symbol END *****/ + + +/***** Path *****/ +bool isPath(PyObject *obj) +{ + if (PyInstance_Check(obj) + && PyObject_IsInstance(obj, (PyObject *)&PathType)) + return true; + return false; +} + +string Path_getValue(Path *obj) +{ + if (isPath((PyObject *)obj)){ + return string(PyString_AsString(obj->value)); + } + return string(); +} +/***** Path END *****/ + + +/***** Term *****/ +bool isTerm(PyObject *obj) +{ + if (PyInstance_Check(obj) + && PyObject_IsInstance(obj, (PyObject *)&TermType)) + return true; + return false; +} + +string Term_getName(Term *obj) +{ + if (isTerm((PyObject *)obj)){ + return string(PyString_AsString(obj->name)); + } + return string(); +} + +PyObject *Term_getValue(Term *obj) +{ + if (isTerm((PyObject *)obj)){ + return obj->value; + } + return Py_None; +} +/***** Term END *****/ + Added: trunk/python-bindings/src/YCPTypes.h URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCPTypes.h?re... ============================================================================== --- trunk/python-bindings/src/YCPTypes.h (added) +++ trunk/python-bindings/src/YCPTypes.h Wed Sep 26 11:35:28 2007 @@ -0,0 +1,80 @@ +#ifndef _YCPTYPES_H_ +#define _YCPTYPES_H_ + +#include <string> +#include "Python.h" +#include "structmember.h" + +/** + * Generic YCP type which has one string member named value + * and one member for storing computed hash. + */ +typedef struct { + PyObject_HEAD + PyObject *value; //string + long hash; +} YCPTypeString; + +/** + * Symbol + */ +typedef YCPTypeString Symbol; +extern PyTypeObject SymbolType; + +bool isSymbol(PyObject *); +std::string Symbol_getValue(Symbol *); + + +/** + * Path + */ +typedef YCPTypeString Path; +extern PyTypeObject PathType; + +bool isPath(PyObject *); +std::string Path_getValue(Path *); + + +/** + * Term + * Term is composed from name (string) and value (tuple of various types) + */ +typedef struct{ + PyObject_HEAD + PyObject *name; // string + PyObject *value; // tuple of various types + long hash; +} Term; +extern PyTypeObject TermType; + +bool isTerm(PyObject *); +std::string Term_getName(Term *); + +/** + * Returns list or None. + * Borrowed reference! + */ +PyObject *Term_getValue(Term *); + + +/** + * Initialize alll YCP types + */ +bool initYCPTypes(PyObject *module); + +bool isYCPType(PyObject *); + + +/** + * Usage: + * switch (getType(obj)){ + * case PATH: + * ... + * case SYMBOL: + * ... + * } + */ +enum YCPType { NOT_YCP_TYPE, PATH, SYMBOL, TERM }; +YCPType getType(PyObject *); + +#endif Added: trunk/python-bindings/src/YCPTypes/Path.cc URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCPTypes/Path... ============================================================================== --- trunk/python-bindings/src/YCPTypes/Path.cc (added) +++ trunk/python-bindings/src/YCPTypes/Path.cc Wed Sep 26 11:35:28 2007 @@ -0,0 +1,158 @@ +#include "YCPTypesInternal.h" +using std::string; + +/** + * Returns new Path object created from string str. + * Returns New Reference! + */ +static PyObject *Path_StringNew(const char *str) +{ + PyObject *ret; + PyObject *args; + + // create args variable + args = Py_BuildValue("(s)", str); + + // create new Path object + ret = YCPTypeString_new(&PathType, Py_None, Py_None); + if (ret == NULL){ + Py_XDECREF(args); + return Py_None; + } + + // initialize Path object + if (YCPTypeString_init((YCPTypeString *)ret, args, Py_None) == -1){ + Py_XDECREF(args); + return Py_None; + } + + Py_XDECREF(args); + return ret; +} + + + +/** + * Compare two Path objects. + * If obj1 or obj2 are not Paths returns -1 (it means not equal) + */ +static int Path_cmp(PyObject *obj1, PyObject *obj2) +{ + char *str1, *str2; + int ret = 0; + + // check is obj1 and obj2 are Paths + if (PyObject_IsInstance(obj1, (PyObject *)&PathType) != 1 || + PyObject_IsInstance(obj2, (PyObject *)&PathType) != 1) + return -1; + + str1 = PyString_AsString(((Path *)obj1)->value); + str2 = PyString_AsString(((Path *)obj2)->value); + + ret = strcmp(str1, str2); + if (ret != 0) + return ret < 0 ? -1 : 1; + return ret; +} + + +/** + * Returns New Reference! + */ +static PyObject *Path_append(Path *self, PyObject *arg) +{ + const char *str = PyString_AsString(self->value); + string new_value(str); + + // check if arg is string + if (!PyString_CheckExact(arg)){ + PyErr_SetString(PyExc_TypeError, ": argument 1 must be string"); + return Py_None; + } + + // concate strings + str = PyString_AsString(arg); + new_value += "."; + new_value += str; + + return Path_StringNew(new_value.c_str()); +} + +/** + * Returns New Reference! + */ +static PyObject *Path_prepend(Path *self, PyObject *arg) +{ + string new_value; + + // check if arg is string + if (!PyString_CheckExact(arg)){ + PyErr_SetString(PyExc_TypeError, ": argument 1 must be string"); + return Py_None; + } + + // concate strings + new_value = PyString_AsString(arg); + new_value += "."; + new_value += PyString_AsString(self->value); + + return Path_StringNew(new_value.c_str()); +} + +static PyMethodDef Path_methods[] = { + {"append", (PyCFunction)Path_append, METH_O, "Return new Path object with appended path given in argument."}, + {"prepend", (PyCFunction)Path_prepend, METH_O, "Return new Path object with perpended path given in argument."}, + {NULL} /* Sentinel */ +}; + +/** + * List of accessible members of Path. + */ +static PyMemberDef Path_members[] = { + // value is accessible only for read + {"value", T_OBJECT_EX, offsetof(Path, value), READONLY, "Value of symbol"}, + {NULL} /* Sentinel */ +}; + +PyTypeObject PathType = { + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "ycp.Path", /*tp_name*/ + sizeof(Path), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)YCPTypeString_dealloc,/*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + (cmpfunc)Path_cmp, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + (hashfunc)YCPTypeString_hash,/*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + "YCP Paths", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + Path_methods, /* tp_methods */ + Path_members, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)YCPTypeString_init,/* tp_init */ + 0, /* tp_alloc */ + YCPTypeString_new, /* tp_new */ +}; + Added: trunk/python-bindings/src/YCPTypes/Symbol.cc URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCPTypes/Symb... ============================================================================== --- trunk/python-bindings/src/YCPTypes/Symbol.cc (added) +++ trunk/python-bindings/src/YCPTypes/Symbol.cc Wed Sep 26 11:35:28 2007 @@ -0,0 +1,81 @@ +#include "YCPTypesInternal.h" + +/** + * Compare two Symbol objects. + * If obj1 or obj2 are not Symbols returns -1 (it means not equal) + */ +static int Symbol_cmp(PyObject *obj1, PyObject *obj2) +{ + char *str1, *str2; + int ret = 0; + + // check is obj1 and obj2 are Symbols + if (PyObject_IsInstance(obj1, (PyObject *)&SymbolType) != 1 || + PyObject_IsInstance(obj2, (PyObject *)&SymbolType) != 1) + return -1; + + str1 = PyString_AsString(((Symbol *)obj1)->value); + str2 = PyString_AsString(((Symbol *)obj2)->value); + + ret = strcmp(str1, str2); + if (ret != 0) + return ret < 0 ? -1 : 1; + return ret; +} + + +static PyMethodDef Symbol_methods[] = { + {NULL} /* Sentinel */ +}; + +/** + * List of accessible members of Symbol. + */ +static PyMemberDef Symbol_members[] = { + // value is accessible only for read + {"value", T_OBJECT_EX, offsetof(Symbol, value), READONLY, "Value of symbol"}, + {NULL} /* Sentinel */ +}; + +PyTypeObject SymbolType = { + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "ycp.Symbol", /*tp_name*/ + sizeof(Symbol), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)YCPTypeString_dealloc,/*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + (cmpfunc)Symbol_cmp, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + (hashfunc)YCPTypeString_hash, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + "YCP Symbols", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + Symbol_methods, /* tp_methods */ + Symbol_members, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)YCPTypeString_init, /* tp_init */ + 0, /* tp_alloc */ + YCPTypeString_new, /* tp_new */ +}; + Added: trunk/python-bindings/src/YCPTypes/Term.cc URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCPTypes/Term... ============================================================================== --- trunk/python-bindings/src/YCPTypes/Term.cc (added) +++ trunk/python-bindings/src/YCPTypes/Term.cc Wed Sep 26 11:35:28 2007 @@ -0,0 +1,170 @@ +#include "YCPTypesInternal.h" + +/** + * Deallocate Term object + */ +static void Term_dealloc(Term *self) +{ + Py_XDECREF(self->name); + Py_XDECREF(self->value); + self->ob_type->tp_free((PyObject *)self); +} + +/** + * This function is called before Term_init. + * In this function is set only default values. + */ +static PyObject *Term_new(PyTypeObject *type, PyObject *args, + PyObject *kwds) +{ + Term *self; + + self = (Term *)type->tp_alloc(type, 0); + if (self != NULL){ + self->name = PyString_FromString(""); + if (self->name == NULL){ + Py_DECREF(self); + return NULL; + } + self->value = PyTuple_New(0); + if (self->value == NULL){ + Py_DECREF(self); + return NULL; + } + + self->hash = -1; + } + + return (PyObject *)self; +} + +/** + * Initialize Term object. + */ +int Term_init(Term *self, PyObject *args, PyObject *kwds) +{ + PyObject *name = NULL; + PyObject *value = NULL; + PyObject *tmp; + int args_size; + + // check number of arguments + args_size = PyTuple_Size(args); + if (args_size < 1){ + PyErr_SetString(PyExc_TypeError, ": function takes exactly 1 argument (0 given)"); + return -1; + } + + // name: + name = PyTuple_GetItem(args, 0); + if (PyString_CheckExact(name)){ + tmp = self->name; + Py_INCREF(name); + self->name = name; + Py_XDECREF(tmp); + }else{ + PyErr_SetString(PyExc_TypeError, ": argument 1 must be string"); + return -1; + } + + // value: + if (args_size > 1){ + value = PyTuple_GetSlice(args, 1, args_size); // return new reference + if (value != NULL){ + tmp = self->value; + self->value = value; + Py_XDECREF(tmp); + } + } + + return 0; +} + +/** + * Returns hash of Term object. + */ +long Term_hash(Term *self) +{ + if (self->hash != -1){ + self->hash = PyObject_Hash(self->name) + PyObject_Hash(self->value); + } + return self->hash; +} + +/** + * Compare two Term objects. + * If obj1 or obj2 are not Terms returns -1 (it means not equal) + */ +static int Term_cmp(PyObject *obj1, PyObject *obj2) +{ + // check if obj1 and obj2 are Terms + if (PyObject_IsInstance(obj1, (PyObject *)&TermType) != 1 || + PyObject_IsInstance(obj2, (PyObject *)&TermType) != 1) + return -1; + + if (PyObject_Compare(((Term *)obj1)->name, ((Term *)obj2)->name) == 0 && + PyObject_Compare(((Term *)obj1)->value, ((Term *)obj2)->value) == 0){ + return 0; + } + return 1; +} + + + +static PyMethodDef Term_methods[] = { + {NULL} /* Sentinel */ +}; + +/** + * List of accessible members of Term. + */ +static PyMemberDef Term_members[] = { + // value is accessible only for read + {"name", T_OBJECT_EX, offsetof(Term, name), READONLY, "Name of the Term"}, + {"value", T_OBJECT_EX, offsetof(Term, value), READONLY, "Value of the Term"}, + {NULL} /* Sentinel */ +}; + +PyTypeObject TermType = { + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "ycp.Term", /*tp_name*/ + sizeof(Term), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)Term_dealloc,/*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + (cmpfunc)Term_cmp, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + (hashfunc)Term_hash,/*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + "YCP Terms", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + Term_methods, /* tp_methods */ + Term_members, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)Term_init,/* tp_init */ + 0, /* tp_alloc */ + Term_new, /* tp_new */ +}; + + Added: trunk/python-bindings/src/YCPTypes/YCPTypesInternal.cc URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCPTypes/YCPT... ============================================================================== --- trunk/python-bindings/src/YCPTypes/YCPTypesInternal.cc (added) +++ trunk/python-bindings/src/YCPTypes/YCPTypesInternal.cc Wed Sep 26 11:35:28 2007 @@ -0,0 +1,52 @@ +#include "YCPTypesInternal.h" + +/********** YCPTypeString fucntions **********/ +void YCPTypeString_dealloc(YCPTypeString *self) +{ + Py_XDECREF(self->value); + self->ob_type->tp_free((PyObject *)self); +} + +PyObject *YCPTypeString_new(PyTypeObject *type, PyObject *args, + PyObject *kwds) +{ + YCPTypeString *self; + + self = (YCPTypeString *)type->tp_alloc(type, 0); + if (self != NULL){ + self->value = PyString_FromString(""); + if (self->value == NULL){ + Py_DECREF(self); + return NULL; + } + + self->hash = -1; + } + + return (PyObject *)self; +} + +int YCPTypeString_init(YCPTypeString *self, PyObject *args, PyObject *kwds) +{ + const char *value=NULL; + PyObject *tmp; + + if (!PyArg_ParseTuple(args, "s", &value)) + return -1; + + if (value != NULL) { + tmp = self->value; + self->value = Py_BuildValue("s", value); + Py_XDECREF(tmp); + } + + return 0; +} + +long YCPTypeString_hash(YCPTypeString *self) +{ + if (self->hash != -1){ + self->hash = PyObject_Hash(self->value); + } + return self->hash; +} Added: trunk/python-bindings/src/YCPTypes/YCPTypesInternal.h URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCPTypes/YCPT... ============================================================================== --- trunk/python-bindings/src/YCPTypes/YCPTypesInternal.h (added) +++ trunk/python-bindings/src/YCPTypes/YCPTypesInternal.h Wed Sep 26 11:35:28 2007 @@ -0,0 +1,32 @@ +#ifndef _YCP_TYPES_INTERNAL_H_ +#define _YCP_TYPES_INTERNAL_H_ + +#include <iostream> +#define DBG(str) \ + std::cerr << str << std::endl + +#include "YCPTypes.h" + +/********** YCPTypeString fucntions **********/ +/** + * Function which deallocate YCPTypeString type + */ +void YCPTypeString_dealloc(YCPTypeString *self); + +/** + * Create new YCPTypeString object. This function is called before _init function. + */ +PyObject *YCPTypeString_new(PyTypeObject *type, PyObject *args, + PyObject *kwds); + +/** + * Initialize YCPTypeString object. + */ +int YCPTypeString_init(YCPTypeString *self, PyObject *args, PyObject *kwds); + +/** + * Returns hash value of YCPTypeString object + */ +long YCPTypeString_hash(YCPTypeString *self); + +#endif -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org
participants (1)
-
dfiser@svn.opensuse.org