Mailinglist Archive: yast-commit (535 mails)

< Previous Next >
[yast-commit] r41609 - in /trunk/python-bindings/src: ./ YCPTypes/
  • From: juhliarik@xxxxxxxxxxxxxxxx
  • Date: Mon, 29 Oct 2007 11:48:10 -0000
  • Message-id: <20071029114810.DDDF92D203@xxxxxxxxxxxxxxxx>
Author: juhliarik
Date: Mon Oct 29 12:48:10 2007
New Revision: 41609

URL: http://svn.opensuse.org/viewcvs/yast?rev=41609&view=rev
Log:
added temporrary solution for calling python function via references


Modified:
trunk/python-bindings/src/Makefile.am
trunk/python-bindings/src/YCP.cc
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
trunk/python-bindings/src/YPython.cc
trunk/python-bindings/src/YPython.h
trunk/python-bindings/src/YPythonNamespace.cc

Modified: trunk/python-bindings/src/Makefile.am
URL:
http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/Makefile.am?rev=41609&r1=41608&r2=41609&view=diff
==============================================================================
--- trunk/python-bindings/src/Makefile.am (original)
+++ trunk/python-bindings/src/Makefile.am Mon Oct 29 12:48:10 2007
@@ -21,6 +21,7 @@
YCPTypes/YCPTypesInternal.cc
YCPTypes/YCPTypesInternal.h \
YCPTypes/Path.cc \
YCPTypes/Symbol.cc \
+ YCPTypes/Code.cc \
YCPTypes/Term.cc

libYCPDeclarations_la_SOURCES = YCPDeclarations.cc YCPDeclarations.h
@@ -29,7 +30,10 @@
libYCP_la_SOURCES = \
$(liby2lang_python_la_SOURCES) \
YCP.cc \
- PythonLogger.cc PythonLogger.h
+ PythonLogger.cc PythonLogger.h \
+ YPythonNamespace.cc YPythonNamespace.h \
+ YCPDeclarations.cc YCPDeclarations.
+


install-data-hook:

Modified: trunk/python-bindings/src/YCP.cc
URL:
http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCP.cc?rev=41609&r1=41608&r2=41609&view=diff
==============================================================================
--- trunk/python-bindings/src/YCP.cc (original)
+++ trunk/python-bindings/src/YCP.cc Mon Oct 29 12:48:10 2007
@@ -14,6 +14,7 @@
#include <ycp/Import.h>
#include <yui/YUIComponent.h>
#include <wfm/Y2WFMComponent.h>
+#include <ycp/Parser.h>
#include <ycp/YCPMap.h>
#include <ycp/YCPList.h>
#include <ycp/YCPPath.h>
@@ -24,6 +25,7 @@
#include "YPython.h"
#include "PythonLogger.h"

+
#include "YCPTypes.h"

/**
@@ -41,6 +43,8 @@

PyObject * SCR_Run (const char *scr_command, PyObject *args);

+PyObject * Py_ycp_code(PyObject *args);
+
void Py_y2logger(PyObject *args);

void init_wfm ();
@@ -125,6 +129,13 @@
return Py_None;
}

+static PyObject * ycp_code (PyObject *self, PyObject *args) {
+
+ Py_ycp_code(args);
+ return Py_None;
+}
+
+
/**
* This is needed for importing new module from ycp.
*/
@@ -143,6 +154,7 @@
{"SCR_Execute", ycp_scr_execute, METH_VARARGS, "SCR Execute function"},
{"SCR_Dir", ycp_scr_dir, METH_VARARGS, "SCR Dir function"},
{"y2logger", ycp_y2logger, METH_VARARGS, "Logging error, debug messages and
milestones in python"},
+ {"ycp_code", ycp_code, METH_VARARGS, "Convert Python function call to YCP
Code"},
{NULL, NULL, 0, NULL} /* Sentinel */
};

@@ -194,7 +206,6 @@
PyObject *dict = PyModule_GetDict(Self);
PyObject *code;

-
code = PyRun_String(func_y2internal, Py_single_input, dict, dict);
Py_XDECREF(code);

@@ -229,7 +240,7 @@
if (PyDict_Contains(dict, name_space) == 1)
ret = true;

-
+
Py_XDECREF(name_space);

return ret;
@@ -337,6 +348,41 @@
return PyBool_FromLong(1);
}

+
+PyObject * Py_ycp_code(PyObject *args) {
+
+ Parser *parser;
+ PyObject *temp;
+ string command;
+ YCodePtr c = 0;
+
+ temp = PyTuple_GetItem(args, 0);
+
+ if (PyString_Check(temp))
+ command = PyString_AsString(temp);
+ else
+ command = "";
+ cout << command << endl;
+ parser = new Parser(command.c_str());
+
+ //parser->setInput(command.c_str());
+ //parser->setBuffered();
+ c = parser->parse();
+
+ if (c)
+ c->evaluate();
+ else
+ cout << "eee c nejde!" << endl;
+
+ cout << c->toString() << endl;
+
+ delete(parser);
+
+ return Py_None;
+
+}
+
+
PyObject * Import_YCPNameSpace (PyObject *args) {

PyObject* pResult = PyBool_FromLong(0);
@@ -621,11 +667,12 @@
if (pPythonValue) {
ycpArg = ypython->PythonTypeToYCPType(pPythonValue);

+ /*XXX
if (fun_type->parameterType(i-2)->matchvalue(ycpArg) != 0) {
y2error ("Wrong type of argumment %d",i-2);
return PyExc_TypeError;

- }
+ }*/
bool ok = func_call->appendParameter (ycpArg);
if (!ok) {
y2error ("Problem with adding arguments of function %s",
func_name);
@@ -657,7 +704,7 @@
delete []func_name;

pReturnValue = ypython->YCPTypeToPythonType(ycpRetValue);
-
+ Py_INCREF(pReturnValue);
return pReturnValue;

} else {

Modified: trunk/python-bindings/src/YCPTypes.cc
URL:
http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCPTypes.cc?rev=41609&r1=41608&r2=41609&view=diff
==============================================================================
--- trunk/python-bindings/src/YCPTypes.cc (original)
+++ trunk/python-bindings/src/YCPTypes.cc Mon Oct 29 12:48:10 2007
@@ -10,13 +10,16 @@
return false;
if (PyType_Ready(&TermType) < 0)
return false;
-
+ if (PyType_Ready(&CodeType) < 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);
+ Py_INCREF(&CodeType);
+ PyModule_AddObject(module, "Code", (PyObject *)&CodeType);

return true;
}
@@ -36,6 +39,9 @@
return PATH;
if (isTerm(obj))
return TERM;
+ if (isCode(obj))
+ return CODE;
+
return NOT_YCP_TYPE;
}

@@ -186,3 +192,61 @@
}
/***** Term END *****/

+/***** Code *****/
+bool isCode(PyObject *obj)
+{
+ if (PyObject_IsInstance(obj, (PyObject *)&CodeType))
+ return true;
+ return false;
+}
+
+PyObject *Code_getValue(Code *obj)
+{
+ if (isCode((PyObject *)obj)){
+ return obj->value;
+ }
+ return Py_None;
+}
+
+
+PyObject *Code_New(PyObject *value)
+{
+ PyObject *ret;
+ PyObject *args;
+ int size, i;
+ PyObject *tmp;
+
+ if (!PyFunction_Check(value)){
+ return Py_None;
+ }
+
+ // create args variable
+
+ args = PyTuple_New(1);
+
+ if (PyTuple_SetItem(args, 0, value) != 0){
+ Py_XDECREF(args);
+ Py_DECREF(value);
+ return Py_None;
+ }
+
+ // create new object
+ ret = Code_new(&CodeType, Py_None, Py_None);
+ if (ret == NULL){
+ Py_XDECREF(args);
+ return Py_None;
+ }
+
+ // initialize object
+ if (Code_init((Code *)ret, args, Py_None) == -1){
+ Py_XDECREF(args);
+ return Py_None;
+ }
+
+ Py_XDECREF(args);
+ return ret;
+}
+
+
+/***** Term END *****/
+

Modified: trunk/python-bindings/src/YCPTypes.h
URL:
http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCPTypes.h?rev=41609&r1=41608&r2=41609&view=diff
==============================================================================
--- trunk/python-bindings/src/YCPTypes.h (original)
+++ trunk/python-bindings/src/YCPTypes.h Mon Oct 29 12:48:10 2007
@@ -69,9 +69,32 @@
PyObject *Term_New(PyObject *name, PyObject *list_value);
PyObject *Term_NewString(const char *name, PyObject *list_value);

+/**
+ * Code
+ * Code is composed from value (pointer to function call)
+ */
+typedef struct{
+ PyObject_HEAD
+ PyObject *value; // pointer to function
+ long hash;
+} Code;
+extern PyTypeObject CodeType;
+
+bool isCode(PyObject *);
+
+
+/**
+ * Returns list or None.
+ * Borrowed reference!
+ */
+PyObject *Code_getValue(Code *);
+
+PyObject *Code_New(PyObject *value);
+
+

/**
- * Initialize alll YCP types
+ * Initialize all YCP types
*/
bool initYCPTypes(PyObject *module);

@@ -87,7 +110,7 @@
* ...
* }
*/
-enum YCPType { NOT_YCP_TYPE, PATH, SYMBOL, TERM };
+enum YCPType { NOT_YCP_TYPE, PATH, SYMBOL, TERM, CODE };
YCPType getYCPType(PyObject *);

#endif

Modified: trunk/python-bindings/src/YCPTypes/Path.cc
URL:
http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCPTypes/Path.cc?rev=41609&r1=41608&r2=41609&view=diff
==============================================================================
--- trunk/python-bindings/src/YCPTypes/Path.cc (original)
+++ trunk/python-bindings/src/YCPTypes/Path.cc Mon Oct 29 12:48:10 2007
@@ -79,6 +79,7 @@
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."},
+ {"isCode", (PyCFunction)YCPType_isCode, METH_NOARGS, "Return true if
object is Code."},
{"isSymbol", (PyCFunction)YCPType_isSymbol, METH_NOARGS, "Return true if
object is Symbol."},
{"isPath", (PyCFunction)YCPType_isPath, METH_NOARGS, "Return true if
object is Path."},
{"isTerm", (PyCFunction)YCPType_isTerm, METH_NOARGS, "Return true if
object is Term."},

Modified: trunk/python-bindings/src/YCPTypes/Symbol.cc
URL:
http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCPTypes/Symbol.cc?rev=41609&r1=41608&r2=41609&view=diff
==============================================================================
--- trunk/python-bindings/src/YCPTypes/Symbol.cc (original)
+++ trunk/python-bindings/src/YCPTypes/Symbol.cc Mon Oct 29 12:48:10 2007
@@ -37,6 +37,7 @@

static PyMethodDef Symbol_methods[] = {
{"isSymbol", (PyCFunction)YCPType_isSymbol, METH_NOARGS, "Return true if
object is Symbol."},
+ {"isCode", (PyCFunction)YCPType_isCode, METH_NOARGS, "Return true if
object is Code."},
{"isPath", (PyCFunction)YCPType_isPath, METH_NOARGS, "Return true if
object is Path."},
{"isTerm", (PyCFunction)YCPType_isTerm, METH_NOARGS, "Return true if
object is Term."},
{NULL} /* Sentinel */

Modified: trunk/python-bindings/src/YCPTypes/Term.cc
URL:
http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCPTypes/Term.cc?rev=41609&r1=41608&r2=41609&view=diff
==============================================================================
--- trunk/python-bindings/src/YCPTypes/Term.cc (original)
+++ trunk/python-bindings/src/YCPTypes/Term.cc Mon Oct 29 12:48:10 2007
@@ -124,6 +124,7 @@

static PyMethodDef Term_methods[] = {
{"len", (PyCFunction)Term_len, METH_NOARGS, "Return number of Terms\'
parameters."},
+ {"isCode", (PyCFunction)YCPType_isCode, METH_NOARGS, "Return true if
object is Code."},
{"isSymbol", (PyCFunction)YCPType_isSymbol, METH_NOARGS, "Return true if
object is Symbol."},
{"isPath", (PyCFunction)YCPType_isPath, METH_NOARGS, "Return true if
object is Path."},
{"isTerm", (PyCFunction)YCPType_isTerm, METH_NOARGS, "Return true if
object is Term."},

Modified: trunk/python-bindings/src/YCPTypes/YCPTypesInternal.cc
URL:
http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCPTypes/YCPTypesInternal.cc?rev=41609&r1=41608&r2=41609&view=diff
==============================================================================
--- trunk/python-bindings/src/YCPTypes/YCPTypesInternal.cc (original)
+++ trunk/python-bindings/src/YCPTypes/YCPTypesInternal.cc Mon Oct 29 12:48:10
2007
@@ -111,3 +111,12 @@
Py_RETURN_FALSE;
}
}
+
+PyObject *YCPType_isCode(PyObject *self, PyObject *)
+{
+ if (isCode(self)){
+ Py_RETURN_TRUE;
+ }else{
+ Py_RETURN_FALSE;
+ }
+}

Modified: trunk/python-bindings/src/YCPTypes/YCPTypesInternal.h
URL:
http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCPTypes/YCPTypesInternal.h?rev=41609&r1=41608&r2=41609&view=diff
==============================================================================
--- trunk/python-bindings/src/YCPTypes/YCPTypesInternal.h (original)
+++ trunk/python-bindings/src/YCPTypes/YCPTypesInternal.h Mon Oct 29 12:48:10
2007
@@ -40,6 +40,11 @@
int Term_init(Term *self, PyObject *args, PyObject *kwds);


+PyObject *Code_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
+int Code_init(Code *self, PyObject *args, PyObject *kwds);
+
+
+PyObject *YCPType_isCode(PyObject *self, PyObject *);
PyObject *YCPType_isSymbol(PyObject *self, PyObject *);
PyObject *YCPType_isPath(PyObject *self, PyObject *);
PyObject *YCPType_isTerm(PyObject *self, PyObject *);

Modified: trunk/python-bindings/src/YPython.cc
URL:
http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YPython.cc?rev=41609&r1=41608&r2=41609&view=diff
==============================================================================
--- trunk/python-bindings/src/YPython.cc (original)
+++ trunk/python-bindings/src/YPython.cc Mon Oct 29 12:48:10 2007
@@ -31,7 +31,6 @@
#include <ycp/pathsearch.h>


-
#include <YPython.h>
#include <ycp/YCPValue.h>
#include <ycp/YCPBoolean.h>
@@ -49,6 +48,8 @@
#include <ycp/YCPExternal.h>

#include "YCPTypes.h"
+#include "YPythonNamespace.h"
+
#include <iostream>
#define DBG(str) \
std::cerr << __FILE__ << ": " << __LINE__ << ": " << str << std::endl; \
@@ -129,16 +130,28 @@
//delete last 3 chars from module name ".py"
module_name.erase(module_name.size()-3); //delete ".py"
//initialize python and set the path where are python modules
+
+ cout << "Searching path for modules: " << Py_GetPath()<< endl;
if (!Py_IsInitialized()) {
+ cout <<"path :" << path << endl;
setenv("PYTHONPATH", path.c_str(), 1);
+
Py_Initialize();
YPython::_pMainDicts = PyDict_New();
}

+ setenv("PYTHONPATH", path.c_str(), 1);
+ cout <<"path :" << path << endl;
+ if (!YPython::_pMainDicts)
+ YPython::_pMainDicts = PyDict_New();
//create python string for name of module
pModuleName = PyString_FromString(module_name.c_str());
//check if dictionary contain "dictionary" for module
if ( PyDict_Contains(YPython::_pMainDicts, pModuleName) == 0) {
+ cout <<"loadModule name: " << module_name << endl;
+ //char * new_path = (char *) path.c_str();
+ //PySys_SetPath(new_path);
+ cout << "Searching path for modules after init: " << Py_GetPath()<<
endl;
pMain = PyImport_ImportModule(module_name.c_str());
if (pMain == NULL){
y2error("Can't import module %s", module_name.c_str());
@@ -155,7 +168,9 @@
return YCPError("The module was not imported");
} else {

- return YCPError("The module is imported");
+ //return YCPError("The module is imported");
+ y2error("The module is imported");
+ return YCPVoid();
}


@@ -252,6 +267,14 @@
if (PyString_Check(pythonValue))
return YCPString(PyString_AsString(pythonValue));

+ //functions ->Reference
+ if (PyFunction_Check(pythonValue))
+ return fromPythonFunToReference (pythonValue);
+
+ //function
+ if (PyFunction_Check(pythonValue))
+ return YCPByteblock((const unsigned char
*)PyFunction_GetCode(pythonValue), (long)
sizeof(PyFunction_GetCode(pythonValue)));
+
//list
if (PyList_Check(pythonValue))
return fromPythonListToYCPList(pythonValue);
@@ -264,6 +287,8 @@
if (PyDict_Check(pythonValue))
return fromPythonDictToYCPMap(pythonValue);

+
+
// term, symbol, path
switch (getYCPType(pythonValue)){
case SYMBOL:
@@ -272,6 +297,9 @@
return YCPPath(Path_getValue((Path *)pythonValue));
case TERM:
return fromPythonTermToYCPTerm(pythonValue);
+ case CODE:
+ return YCPCode(new YPythonCode(Code_getValue((Code
*)pythonValue)));
+
case NOT_YCP_TYPE:
return YCPNull();
}
@@ -584,3 +612,119 @@
return Term_NewString(ycp_Term->asTerm()->name().c_str(), value);
}

+
+/**
+ * Convert Python Function to YCPCode.
+ *
+ * @param pointer to python function
+ * @return YCPCode - Referecne
+ **/
+
+YCPValue YPython::fromPythonFunToReference (PyObject* pyFun) {
+
+
+ FunctionTypePtr sym_tp = new FunctionType(Type::Any);
+ PyObject *fun_code = PyFunction_GetCode(pyFun);
+ char *fun_name = PyString_AsString(((PyCodeObject *) fun_code)->co_name);
+
+ string file_path = PyString_AsString(((PyCodeObject *)
fun_code)->co_filename);
+ //Y2Namespace* name_space = new Y2Namespace();
+
+ printf ("meno funckie %s\n", fun_name);
+ printf ("meno suboru %s\n", file_path.c_str());
+
+ //found last "/" in path
+ size_t found = file_path.find_last_of("/");
+ //extract module name from path
+ string module_name = file_path.substr(found+1);
+
+ file_path = YCPPathSearch::find (YCPPathSearch::Module, module_name);
+
+ if (file_path.empty())
+ file_path = YCPPathSearch::find (YCPPathSearch::Include, module_name);
+
+ if (file_path.empty())
+ file_path = YCPPathSearch::find (YCPPathSearch::Client, module_name);
+
+ if (file_path.empty()) {
+ y2error("Finding file where is function %s failed", fun_name);
+ return YCPNull();
+ }
+
+
+
+ YPython::loadModule (file_path);
+
+
+ //found last "/" in path
+ found = file_path.find_last_of("/");
+ //extract module name from path
+
+ module_name = file_path.substr(found+1);
+
+ //delete last 3 chars from module name ".py"
+ module_name.erase(module_name.size()-3); //delete ".py"
+
+ //found last "/" in path
+ Y2Namespace *ns = new YPythonNamespace (module_name);
+
+
+ if (ns) {
+ TableEntry *sym_te = ns->table ()->find (fun_name);
+ if (sym_te == NULL) {
+ y2error ("No such symbol %s::%s", module_name.c_str(), fun_name);
+ return YCPNull();
+ }
+ SymbolEntryPtr sym_entry = sym_te->sentry();
+ cout << "entry" << sym_entry->toString()<< endl;
+ return YCPReference(sym_entry);
+
+ } else {
+ y2error("Creating namespace for function %s failed", fun_name);
+ return YCPNull();
+
+ }
+
+ return YCPNull();
+}
+
+
+
+YPythonCode::YPythonCode (PyObject *pFunc):YCode() {
+ m_kind = YCode::yeReference;
+ _pFunc = pFunc;
+}
+
+YCPValue YPythonCode::evaluate(bool cse) {
+
+ PyObject * pReturn;
+ YCPValue result = YCPVoid();
+
+ if (Py_IsInitialized()) {
+ pReturn = PyObject_CallObject(_pFunc, NULL);
+ //convert python value to YCPValue
+ if (pReturn) {
+ result = YPython::yPython()->PythonTypeToYCPType(pReturn); // create
YCP value
+ } else {
+ y2error("pReturn == 0");
+ }
+ }
+ return result;
+
+}
+
+YCode::ykind YPythonCode::kind() const {
+
+ return m_kind;
+}
+
+std::ostream & YPythonCode::toStream (std::ostream & str) const {
+
+ return str;
+}
+
+std::ostream & YPythonCode::toXml (std::ostream & str, int indent ) const {
+
+ return str;
+
+}

Modified: trunk/python-bindings/src/YPython.h
URL:
http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YPython.h?rev=41609&r1=41608&r2=41609&view=diff
==============================================================================
--- trunk/python-bindings/src/YPython.h (original)
+++ trunk/python-bindings/src/YPython.h Mon Oct 29 12:48:10 2007
@@ -22,9 +22,35 @@
#include <Python.h>

#include <ycp/YCPList.h>
+#include <ycp/YCPValue.h>
#include <ycp/Type.h>
+#include <ycp/YCPCode.h>
+#include <ycp/YCode.h>


+
+class YPythonCode : public YCode
+{
+
+public:
+ YPythonCode (PyObject *pFunc);
+
+ YCode::ykind kind() const;
+
+ std::ostream & toStream (std::ostream & str) const;
+
+ std::ostream & toXml (std::ostream & str, int indent ) const;
+ /**
+ * Evaluates the code.
+ */
+ YCPValue evaluate (bool cse = false);
+
+private:
+ ykind m_kind;
+ PyObject *_pFunc;
+
+};
+
class YPython
{
public:
@@ -134,6 +160,8 @@
**/
PyObject* fromYCPTermToPythonTerm (YCPValue ycp_Term);

+ YCPValue fromPythonFunToReference (PyObject* pyFun);
+
protected:

/**

Modified: trunk/python-bindings/src/YPythonNamespace.cc
URL:
http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YPythonNamespace.cc?rev=41609&r1=41608&r2=41609&view=diff
==============================================================================
--- trunk/python-bindings/src/YPythonNamespace.cc (original)
+++ trunk/python-bindings/src/YPythonNamespace.cc Mon Oct 29 12:48:10 2007
@@ -171,6 +171,7 @@
return;
}

+
//keys from dictionary
fun_names = PyDict_Keys(pMainDict);


--
To unsubscribe, e-mail: yast-commit+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-commit+help@xxxxxxxxxxxx

< Previous Next >
This Thread
  • No further messages