Author: juhliarik
Date: Wed Sep 26 11:39:15 2007
New Revision: 41103
URL: http://svn.opensuse.org/viewcvs/yast?rev=41103&view=rev
Log:
new version of python-bindings
-added logging
-added type classes into ycp module (before were global)
-added correction for importing namespace (import modules written in ycp)
Modified:
trunk/python-bindings/VERSION
trunk/python-bindings/src/YCP.cc
trunk/python-bindings/src/YPython.cc
trunk/python-bindings/src/YPythonNamespace.cc
Modified: trunk/python-bindings/VERSION
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/VERSION?rev=41103&r1=41102&r2=41103&view=diff
==============================================================================
--- trunk/python-bindings/VERSION (original)
+++ trunk/python-bindings/VERSION Wed Sep 26 11:39:15 2007
@@ -1 +1 @@
-2.15.3
+2.16.0
Modified: trunk/python-bindings/src/YCP.cc
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCP.cc?rev=41103&r1=41102&r2=41103&view=diff
==============================================================================
--- trunk/python-bindings/src/YCP.cc (original)
+++ trunk/python-bindings/src/YCP.cc Wed Sep 26 11:39:15 2007
@@ -1,6 +1,5 @@
/**
*
- *
* This is the path from Python to YCP. It defines XSUBs.
*/
@@ -25,8 +24,12 @@
#include "YPython.h"
#include "PythonLogger.h"
+/**
+ * Store pointer to ycp module itself.
+ */
+static PyObject *Self;
-YCPList ycp_ListFunctions;
+YCPList * ycp_ListFunctions;
PyObject * Call_YCPFunction (PyObject *args);
@@ -36,10 +39,13 @@
PyObject * SCR_Run (const char *scr_command, PyObject *args);
+void Py_y2logger(PyObject *args);
+
void init_wfm ();
static bool HandleSymbolTable (const SymbolEntry & se) {
+
if (se.isFunction ()) {
ycp_ListFunctions->add(YCPString(se.name()));
@@ -111,6 +117,20 @@
return SCR_Run ("SCR::Dir", args);
}
+static PyObject * ycp_y2logger (PyObject *self, PyObject *args) {
+
+ Py_y2logger(args);
+ return Py_None;
+}
+
+/**
+ * This is needed for importing new module from ycp.
+ */
+static PyMethodDef new_module_methods[] = {
+ {"__run", ycp_handle_function, METH_VARARGS, "Calling YCP from Python"},
+ {NULL, NULL, 0, NULL}
+};
+
static PyMethodDef YCPMethods[] = {
{"run", ycp_handle_function, METH_VARARGS, "Calling YCP from Python"},
@@ -120,16 +140,13 @@
{"SCR_Write", ycp_scr_write, METH_VARARGS, "SCR Write function"},
{"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"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC initycp(void) {
- char func_code[] =
- "def _factory(module, name):\n\
- def FunctionCall(*args):\n\
- return ycp.run(module, name, *args)\n\
- return FunctionCall";
+ PyObject *traceback;
char class_type_code[] =
"class YCPType:\n\
@@ -159,87 +176,141 @@
self.name = name\n\
self.value = args";
+ char func_y2internal[] =
+ "def y2internal(message):\n\
+ file, line, func, txt = traceback.extract_stack(None, 2)[0]\n\
+ y2logger(5, file, line, func, message)";
+
+ char func_y2security[] =
+ "def y2security(message):\n\
+ file, line, func, txt = traceback.extract_stack(None, 2)[0]\n\
+ y2logger(4, file, line, func, message)";
+
+ char func_y2error[] =
+ "def y2error(message):\n\
+ file, line, func, txt = traceback.extract_stack(None, 2)[0]\n\
+ y2logger(3, file, line, func, message)";
+
+ char func_y2warning[] =
+ "def y2warning(message):\n\
+ file, line, func, txt = traceback.extract_stack(None, 2)[0]\n\
+ y2logger(2, file, line, func, message)";
+
+ char func_y2milestone[] =
+ "def y2milestone(message):\n\
+ file, line, func, txt = traceback.extract_stack(None, 2)[0]\n\
+ y2logger(1, file, line, func, message)";
+
+ char func_y2debug[] =
+ "def y2debug(message):\n\
+ file, line, func, txt = traceback.extract_stack(None, 2)[0]\n\
+ y2logger(0, file, line, func, message)";
+ PyRun_SimpleString("import sys, traceback");
+ Self = Py_InitModule("ycp", YCPMethods);
- (void) Py_InitModule("ycp", YCPMethods);
+ traceback = PyImport_AddModule("traceback");
+ PyModule_AddObject(Self,"traceback",traceback);
init_wfm ();
- PyRun_SimpleString(func_code);
+ PyObject *dict = PyModule_GetDict(Self);
+ PyObject *code;
- PyRun_SimpleString(class_type_code);
+
- PyRun_SimpleString(class_symbol_type_code);
- PyRun_SimpleString(class_path_type_code);
+ code = PyRun_String(class_type_code, Py_single_input, dict, dict);
+ Py_XDECREF(code);
- PyRun_SimpleString(class_term_type_code);
-}
+ code = PyRun_String(class_symbol_type_code, Py_single_input, dict, dict);
+ Py_XDECREF(code);
-bool RegFunctions(char *NameSpace, YCPList list_functions) {
- string nameSpace(NameSpace);
- string function;
- string python_code;
- string postfix("Static"); // here can be random generated string
+ code = PyRun_String(class_path_type_code, Py_single_input, dict, dict);
+ Py_XDECREF(code);
- python_code = "class " + nameSpace + postfix + ":";
- for (int i=0; ivalue(i)->asString()->value();
+ code = PyRun_String(class_term_type_code, Py_single_input, dict, dict);
+ Py_XDECREF(code);
- python_code += "\n\tdef " + function + "(self, *args):";
- python_code += "\n\t\treturn ycp.run(\"" + nameSpace + "\", \"" + function + "\", *args)";
- }
+ code = PyRun_String(func_y2internal, Py_single_input, dict, dict);
+ Py_XDECREF(code);
- // registration into ycp module with nameSpace as name
- python_code += "\nycp.__dict__['" + nameSpace + "'] = " + nameSpace + postfix + "()";
+ code = PyRun_String(func_y2security, Py_single_input, dict, dict);
+ Py_XDECREF(code);
- // debug:
- //std::cout << "==============================" << std::endl;
- //std::cout << python_code << std::endl;
- //std::cout << "==============================" << std::endl;
+ code = PyRun_String(func_y2error, Py_single_input, dict, dict);
+ Py_XDECREF(code);
- PyRun_SimpleString(python_code.c_str());
+ code = PyRun_String(func_y2warning, Py_single_input, dict, dict);
+ Py_XDECREF(code);
+
+ code = PyRun_String(func_y2milestone, Py_single_input, dict, dict);
+ Py_XDECREF(code);
+
+ code = PyRun_String(func_y2debug, Py_single_input, dict, dict);
+ Py_XDECREF(code);
- return true;
}
-/*
+
+
+/**
+ * Returns true if NameSpace is registered (is key) in dictionary dict.
+ * Otherwise returns false;
+ */
+bool isRegistered(PyObject *dict, const char *NameSpace)
+{
+ bool ret = false;
+ PyObject *name_space = PyString_FromString(NameSpace);
+
+ if (PyDict_Contains(dict, name_space) == 1)
+ ret = true;
+
+
+ Py_XDECREF(name_space);
+
+ return ret;
+}
bool RegFunctions(char *NameSpace, YCPList list_functions) {
- string reg_line_dict = "ycp.__dict__['";
- string reg_line_name;
- string reg_line_factory = " = _factory(\"";
- reg_line_factory += NameSpace;
- reg_line_factory += "\",\"";
- reg_line_name.insert(0,NameSpace);
- reg_line_name += "_";
- string function;
- string command;
+ // Dictionary of ycp module
+ PyObject *ycp_dict = PyModule_GetDict(Self);
+ if (ycp_dict == NULL) return false;
+ // If already registered return true
+ if (isRegistered(ycp_dict, NameSpace)) return true;
- for (int i=0; ivalue(i)->asString()->value();
- command = reg_line_dict;
- command += reg_line_name;
- command += function;
- command += "']";
- command +=reg_line_factory;
- command += function;
- command += "\")";
+ // Init new module with name NameSpace and method __run (see new_module_methods)
+ PyObject *new_module = Py_InitModule(NameSpace, new_module_methods);
+ if (new_module == NULL) return false;
- PyRun_SimpleString(command.c_str());
- //printf("command for ycp %s\n", command.c_str());
-
- }
+ // Add new initialized module into ycp dictionary (can be accessed via ycp.NameSpace)
+ PyDict_SetItemString(ycp_dict, NameSpace, new_module);
- return true;
+ // Dictionary of new_module - there will be registered all functions
+ PyObject *new_module_dict = PyModule_GetDict(new_module);
+ if (new_module_dict == NULL) return false;
+
+ PyObject *code;
+ string func_def;
+ string function;
+ for (int i=0; ivalue(i)->asString()->value();
+ func_def = "def " + function + "(*args):";
+ func_def += "\n\treturn __run(\"" + string(NameSpace) + "\", \"" + function + "\", *args)";
+
+ // Register function into dictionary of new module. Returns new reference - must be decremented
+ code = PyRun_String(func_def.c_str(), Py_single_input, new_module_dict, new_module_dict);
+ Py_XDECREF(code);
+ }
+ return true;
}
-*/
+
Y2Component *owned_wfmc = 0;
@@ -334,14 +405,14 @@
ns->initialize ();
}
+ ycp_ListFunctions = new YCPList();
- //printf("List of function:\n");
- ns->table()->forEach (&HandleSymbolTable);
- //printf("End list\n");
+ ns->table()->forEach (&HandleSymbolTable);
+ RegFunctions(ns_name, *ycp_ListFunctions);
- RegFunctions(ns_name, ycp_ListFunctions);
delete [] ns_name;
+ delete ycp_ListFunctions;
pResult = PyBool_FromLong(1);
}
@@ -637,6 +708,7 @@
}
delete []ns_name;
delete []func_name;
+
pReturnValue = ypython->YCPTypeToPythonSimpleType(ycpRetValue);
if (!pReturnValue)
pReturnValue = ypython->fromYCPListToPythonList(ycpRetValue);
@@ -658,6 +730,66 @@
}
+void Py_y2logger(PyObject *args) {
+
+ int number_args = PyTuple_Size(args);
+ PyObject * pPythonValue;
+ loglevel_t level = LOG_DEBUG;
+ string file;
+ int line = 0;
+ string function;
+ string message;
+ if (number_args == 5) {
+ //obtain name of namespace (first argument)
+ pPythonValue = PyTuple_GetItem(args, 0);
+ if (pPythonValue) {
+ if (PyInt_Check(pPythonValue)) {
+ level = (loglevel_t)PyInt_AsLong(pPythonValue);
+ } else {
+ y2error("Wrong type of argument");
+ }
+ }
+ pPythonValue = PyTuple_GetItem(args, 1);
+ if (pPythonValue) {
+ if (PyString_Check(pPythonValue)) {
+ file = PyString_AsString(pPythonValue);
+ } else {
+ y2error("Wrong type of argument");
+ }
+ }
+
+ pPythonValue = PyTuple_GetItem(args, 2);
+ if (pPythonValue) {
+ if (PyInt_Check(pPythonValue)) {
+ line = (int)PyInt_AsLong(pPythonValue);
+ } else {
+ y2error("Wrong type of argument");
+ }
+ }
+ pPythonValue = PyTuple_GetItem(args, 3);
+ if (pPythonValue) {
+ if (PyString_Check(pPythonValue)) {
+ function = PyString_AsString(pPythonValue);
+ } else {
+ y2error("Wrong type of argument");
+ }
+ }
+ pPythonValue = PyTuple_GetItem(args, 4);
+ if (pPythonValue) {
+ if (PyString_Check(pPythonValue)) {
+ message = PyString_AsString(pPythonValue);
+ } else {
+ y2error("Wrong type of argument");
+ }
+ }
+
+ y2_logger_function(level, Y2LOG, file.c_str(), line, function.c_str(),"%s", message.c_str());
+ } else {
+ y2error("Wrong number of arguments");
+ }
+}
+
+
void delete_all () {
if (owned_uic != 0) {
Modified: trunk/python-bindings/src/YPython.cc
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YPython.cc?rev=41103&r1=41102&r2=41103&view=diff
==============================================================================
--- trunk/python-bindings/src/YPython.cc (original)
+++ trunk/python-bindings/src/YPython.cc Wed Sep 26 11:39:15 2007
@@ -52,10 +52,9 @@
YPython::YPython() {
PyObject* pMain;
- PyObject* pYCP;
PyObject* pMainDict;
pMain = PyImport_AddModule("__main__");
- pYCP = PyImport_AddModule("ycp");
+ PyRun_SimpleString("from ycp import *");
pMainDict = PyModule_GetDict(pMain);
pPathClass = PyDict_GetItemString(pMainDict, "Path");
pSymbolClass = PyDict_GetItemString(pMainDict, "Symbol");
Modified: trunk/python-bindings/src/YPythonNamespace.cc
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YPythonNamespace.cc?rev=41103&r1=41102&r2=41103&view=diff
==============================================================================
--- trunk/python-bindings/src/YPythonNamespace.cc (original)
+++ trunk/python-bindings/src/YPythonNamespace.cc Wed Sep 26 11:39:15 2007
@@ -9,6 +9,7 @@
#define y2log_component "Y2PythonNamespace"
#include
+#include
#include
#include
@@ -149,7 +150,7 @@
PyObject* item; //item from list of globals symbols from __main__
PyObject* num_args; //number of function argumets in python
PyObject * fun_names; //list of globals symbols from __main__
-
+ string module;
FILE* file;
int num_fun_names = 0;
@@ -161,16 +162,21 @@
//add __main__ module
pMain = PyImport_AddModule("__main__");
PyRun_SimpleString("import __main__");
+ //PyRun_SimpleString("import ycp");
//import inspect module - necessary for number of function arguments
PyRun_SimpleString("import inspect");
//obtain main dictionary of globals variables
pMainDict = PyModule_GetDict(pMain);
-
//Open file and his running in main module
- file = fopen(strcat((char *) c_name, ".py"), "r");
+
+ module = YCPPathSearch::find (YCPPathSearch::Module, name + ".py");
+
+
+ file = fopen(module.c_str(), "r");
if (file) {
- PyRun_SimpleFile(file, strcat((char *) c_name, ".py"));
+ y2milestone("module name %s", module.c_str());
+ PyRun_SimpleFile(file, module.c_str());
}
//symbols from __main__
PyRun_SimpleString("fun_names = dir(__main__)");
@@ -222,7 +228,7 @@
} // end of for (int i = 0; i < num_fun_names; i++)
delete []command;
- Py_CLEAR(fun_names);
+ //Py_CLEAR(fun_names);
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org