Author: juhliarik
Date: Fri Aug 3 14:08:07 2007
New Revision: 39937
URL: http://svn.opensuse.org/viewcvs/yast?rev=39937&view=rev
Log:
added initial version
Added:
trunk/python-bindings/
trunk/python-bindings/MAINTAINER
trunk/python-bindings/Makefile.cvs
trunk/python-bindings/RPMNAME
trunk/python-bindings/SUBDIRS
trunk/python-bindings/VERSION
trunk/python-bindings/configure.in.in
trunk/python-bindings/doc/
trunk/python-bindings/doc/Makefile.am
trunk/python-bindings/doc/python-bindings
trunk/python-bindings/package/
trunk/python-bindings/package/yast2-python-bindings.changes
trunk/python-bindings/src/
trunk/python-bindings/src/Makefile.am
trunk/python-bindings/src/Y2CCPython.cc
trunk/python-bindings/src/Y2CCPython.h
trunk/python-bindings/src/Y2PythonComponent.cc
trunk/python-bindings/src/Y2PythonComponent.h
trunk/python-bindings/src/YCP.cc
trunk/python-bindings/src/YPython.cc
trunk/python-bindings/src/YPython.h
trunk/python-bindings/src/YPythonNamespace.cc
trunk/python-bindings/src/YPythonNamespace.h
trunk/python-bindings/testsuite/
trunk/python-bindings/yast2-python-bindings.spec.in
Added: trunk/python-bindings/MAINTAINER
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/MAINTAINER?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/MAINTAINER (added)
+++ trunk/python-bindings/MAINTAINER Fri Aug 3 14:08:07 2007
@@ -0,0 +1 @@
+Jozef Uhliarik
Added: trunk/python-bindings/Makefile.cvs
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/Makefile.cvs?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/Makefile.cvs (added)
+++ trunk/python-bindings/Makefile.cvs Fri Aug 3 14:08:07 2007
@@ -0,0 +1,21 @@
+#
+# Makefile.cvs
+#
+
+LIB = $(shell y2tool get-lib)
+
+configure: all
+ ./configure --libdir=/usr/$(LIB)
+
+all:
+ y2tool y2autoconf
+ y2tool y2automake
+ autoreconf --force --install
+
+install: configure
+ make
+ make install
+
+reconf: all
+ ./config.status --recheck
+ ./config.status
Added: trunk/python-bindings/RPMNAME
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/RPMNAME?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/RPMNAME (added)
+++ trunk/python-bindings/RPMNAME Fri Aug 3 14:08:07 2007
@@ -0,0 +1 @@
+yast2-python-bindings
Added: trunk/python-bindings/SUBDIRS
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/SUBDIRS?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/SUBDIRS (added)
+++ trunk/python-bindings/SUBDIRS Fri Aug 3 14:08:07 2007
@@ -0,0 +1 @@
+src doc
Added: trunk/python-bindings/VERSION
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/VERSION?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/VERSION (added)
+++ trunk/python-bindings/VERSION Fri Aug 3 14:08:07 2007
@@ -0,0 +1 @@
+2.15.0
Added: trunk/python-bindings/configure.in.in
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/configure.in.in?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/configure.in.in (added)
+++ trunk/python-bindings/configure.in.in Fri Aug 3 14:08:07 2007
@@ -0,0 +1,42 @@
+## general configure.in.in
+
+## initialize
+@YAST2-INIT-COMMON@
+@YAST2-INIT-PROGRAM@
+
+
+## some common checks
+@YAST2-CHECKS-COMMON@
+@YAST2-CHECKS-PROGRAM@
+
+AC_PROGRAM_PATH(PYTHON, python)
+if test -z "$PYTHON" ; then
+ AC_MSG_ERROR(python is missing; please install python 2.5.)
+fi
+AC_SUBST(PYTHON)
+
+## -pthread -fno-strict-aliasing -DNDEBUG -O2 -fmessage-length=0 -Wall -D_FORTIFY_SOURCE=2 -g -fPIC
+## Find out what compiler/linker flags an embedded Python interpreter needs
+PYTHON_CFLAGS=-lpython2.5
+PYTHON_LDFLAGS=-lpython2.5
+
+AC_SUBST(PYTHON_CFLAGS)
+AC_SUBST(PYTHON_LDFLAGS)
+
+CFLAGS="${CFLAGS} ${PYTHON_CFLAGS}"
+CXXFLAGS="${CXXFLAGS} ${PYTHON_CFLAGS}"
+
+## Where to install modules
+PYTHON_VENDORARCH=${libdir}/python
+AC_SUBST(PYTHON_VENDORARCH)
+
+## make a literal of yast2dir so that it can be used in python instead
+## of shell
+eval yast2dir4python='"'$yast2dir'"'
+AC_SUBST(yast2dir4python)
+
+PKG_CHECK_MODULES([ZYPP], [libzypp])
+
+## and generate the output
+##AC_OUTPUT(src/YaPI.pm)
+@YAST2-OUTPUT@
Added: trunk/python-bindings/doc/Makefile.am
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/doc/Makefile.am?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/doc/Makefile.am (added)
+++ trunk/python-bindings/doc/Makefile.am Fri Aug 3 14:08:07 2007
@@ -0,0 +1,17 @@
+#
+# Makefile.am for python-bindings/doc
+#
+
+# see examples/Makefile.am for why it is disabled
+#UBDIRS = examples
+
+
+txtdir = $(docdir)
+
+
+
+txt_DATA = python-bindings
+
+
+
+EXTRA_DIST = $(txt_DATA)
Added: trunk/python-bindings/doc/python-bindings
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/doc/python-bindings?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/doc/python-bindings (added)
+++ trunk/python-bindings/doc/python-bindings Fri Aug 3 14:08:07 2007
@@ -0,0 +1,62 @@
+Calling YCP from Python
+
+
+YCP module "sum.ycp":
+
+{
+
+
+module "sum";
+
+global integer suma(integer a, integer b) {
+
+ return a + b;
+}
+
+//y2milestone ("suma: %1", suma(1,1));
+
+global list<any> print_list(string a, integer b) {
+ list <any> any_list = [];
+ any_list = add(any_list, a);
+ any_list = add(any_list, b);
+
+ return any_list;
+
+}
+
+global map print_map(string key, any value) {
+ map result = $[];
+ result[key] = value;
+ return result;
+
+}
+
+
+calling from python...
+
+general it is called via ycp.run("name_of_module","name_of_function",arg1, arg2...)
+
+>>> import ycp
+>>>
+>>> sum = ycp.run('sum','suma',1,1)
+>>> print sum
+2
+>>>
+
+>>> list = ycp.run('sum','print_list','string_example',32.5)
+>>> print list
+['string_example', 32.5]
+>>>
+>>>
+
+>>> map = ycp.run('sum','print_map','string_key',685)
+>>> print map
+{'string_key': 685}
+>>>
+>>>
+>>> print list
+['string_example', 32.5]
+>>> map = ycp.run('sum','print_map','string_key',list)
+>>> print map
+{'string_key': ['string_example', 32.5]}
+>>>
Added: trunk/python-bindings/package/yast2-python-bindings.changes
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/package/yast2-python-bindings.changes?rev=39937&view=auto
==============================================================================
(empty)
Added: trunk/python-bindings/src/Makefile.am
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/Makefile.am?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/src/Makefile.am (added)
+++ trunk/python-bindings/src/Makefile.am Fri Aug 3 14:08:07 2007
@@ -0,0 +1,96 @@
+#
+# Makefile.am for python-bindings/src
+#
+
+AM_CXXFLAGS = -DY2LOG=\"Python\" -DMODULEDIR=\"$(moduledir)\"
+
+MY_PYTHON_VENDORARCH = $(subst /usr,$(prefix),$(PYTHON_VENDORARCH))
+pythonpydir = $(MY_PYTHON_VENDORARCH)/YaST
+pythonsodir = $(MY_PYTHON_VENDORARCH)/site-packages
+
+
+#pythonpm_DATA = YCP.pm
+
+# plugin, libtool forces 'lib' prefix
+plugin_LTLIBRARIES = libpy2lang_python.la
+noinst_LTLIBRARIES = liby2lang_python.la
+pythonso_LTLIBRARIES = libYCP.la
+
+# binary part of the python module
+libYCP_la_SOURCES = \
+ $(liby2lang_python_la_SOURCES) \
+ YCP.cc
+
+
+install-exec-hook:
+ $(LN_S) -f libYCP.so $(DESTDIR)$(pythonsodir)/ycp.so
+
+
+# are there enough yast libraries?
+# check with y2base, integrate them like y2pm does
+# Originally, of the py2* there was only py2plugin here
+# with the assumption that it would bring in the other plugins.
+# But it does not work.
+libYCP_la_LDFLAGS = $(PYTHON_LDFLAGS) \
+ -L$(libdir) -L$(plugindir) \
+ -Xlinker --whole-archive \
+ -lpy2scr \
+ -lpy2wfm \
+ -lscr -lyui \
+ -lycp -ly2 \
+ -Xlinker --no-whole-archive \
+ ${ZYPP_LIBS} -ly2util \
+ -version-info 2:0
+
+
+libpy2lang_python_la_LDFLAGS = -version-info 2:0
+
+liby2lang_python_la_LDFLAGS = -version-info 2:0
+
+
+# the yast libraries are apparently necessary when we're loaded by python.
+libpy2lang_python_la_LIBADD = $(PYTHON_LDFLAGS) \
+ -L$(libdir) -L$(plugindir) \
+ -lycp -ly2 ${ZYPP_LIBS} -ly2util
+
+
+liby2lang_python_la_LIBADD = $(PYTHON_LDFLAGS)
+
+
+liby2lang_python_la_SOURCES = \
+ YPython.cc YPython.h
+
+# Auto-generated stub for dynamic loading of python modules.
+# And also register the interface to the YCP module
+## which is linked in already and won't be in the standard python location.
+#
+# This results in a linker warning:
+# *** Warning: Linking the shared library libpy2lang_python.la against the
+# *** static library /usr/lib/python5/.../DynaLoader.a is not portable!
+#
+# According to mls@suse.de this warning can safely be disregarded:
+# The SuSE DynaLoader is compiled with -fPIC for just this situation.
+#
+# -- sh@suse.de 2003-07-24
+#pythonxsi.c:
+# python -MExtUtils::Embed -e xsinit -- -o pythonxsi.c -std
+# See "man pythonembed"
+
+
+# the plugin adds the liby2 component interface
+
+libpy2lang_python_la_SOURCES = \
+ $(liby2lang_python_la_SOURCES) \
+ Y2CCPython.cc Y2CCPython.h \
+ YPythonNamespace.cc YPythonNamespace.h \
+ Y2PythonComponent.cc Y2PythonComponent.h
+
+# CLEANFILES = \
+# pythonxsi.c
+
+INCLUDES = -I$(srcdir)/include -I$(includedir) ${ZYPP_CFLAGS} -I/usr/include/python
+
+# generated from YaPI.pm.in by configure
+# nodist_module_DATA = YaPI.pm
+
+#EXTRA_DIST = $(pythonpy_DATA) $(module_DATA)
Added: trunk/python-bindings/src/Y2CCPython.cc
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/Y2CCPython.cc?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/src/Y2CCPython.cc (added)
+++ trunk/python-bindings/src/Y2CCPython.cc Fri Aug 3 14:08:07 2007
@@ -0,0 +1,58 @@
+/*---------------------------------------------------------------------\
+| |
+| __ __ ____ _____ ____ |
+| \ \ / /_ _/ ___|_ _|___ \ |
+| \ V / _` \___ \ | | __) | |
+| | | (_| |___) || | / __/ |
+| |_|\__,_|____/ |_| |_____| |
+| |
+| core system |
+| (C) SuSE Linux AG |
+\----------------------------------------------------------------------/
+
+ File: Y2CCPython.cc
+
+
+/-*/
+
+#include
+#include "Y2CCPython.h"
+#include
+#define y2log_component "Y2Python"
+#include
+
+// This is very important: We create one global variable of
+// Y2CCPython. Its constructor will register it automatically to
+// the Y2ComponentBroker, so that will be able to find it.
+// This all happens before main() is called!
+
+Y2CCPython g_y2ccpython;
+
+Y2Component *Y2CCPython::provideNamespace (const char *name)
+{
+ y2debug ("Y2CCPython::provideNamespace %s", name);
+ if (strcmp (name, "Python") == 0)
+ {
+ // low level functions
+
+ // leave implementation to later
+ return 0;
+ }
+ else
+ {
+ // is there a python module?
+ // must be the same in Y2CCPython and Y2PythonComponent
+ string module = YCPPathSearch::find (YCPPathSearch::Module, string (name) + ".py");
+ if (!module.empty ())
+ {
+ if (!cpython)
+ {
+ cpython = new Y2PythonComponent ();
+ }
+ return cpython;
+ }
+
+ // let someone else try creating the namespace
+ return 0;
+ }
+}
Added: trunk/python-bindings/src/Y2CCPython.h
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/Y2CCPython.h?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/src/Y2CCPython.h (added)
+++ trunk/python-bindings/src/Y2CCPython.h Fri Aug 3 14:08:07 2007
@@ -0,0 +1,73 @@
+/*-----------------------------------------------------------*- c++ -*-\
+| |
+| __ __ ____ _____ ____ |
+| \ \ / /_ _/ ___|_ _|___ \ |
+| \ V / _` \___ \ | | __) | |
+| | | (_| |___) || | / __/ |
+| |_|\__,_|____/ |_| |_____| |
+| |
+| core system |
+| (C) SuSE Linux AG |
+\----------------------------------------------------------------------/
+
+ File: Y2CCPython.h
+
+/-*/
+
+#include
+#ifndef _Y2CCPython_h
+#define _Y2CCPython_h
+
+#include "Y2PythonComponent.h"
+
+/**
+ * @short Y2ComponentCreator that creates Python-from-YCP bindings.
+ *
+ * A Y2ComponentCreator is an object that can create components.
+ * It receives a component name and - if it knows how to create
+ * such a component - returns a newly created component of this
+ * type. Y2CCPython can create components with the name "Python".
+ */
+class Y2CCPython : public Y2ComponentCreator
+{
+private:
+ Y2Component *cpython;
+
+public:
+ /**
+ * Creates a Python component creator
+ */
+ Y2CCPython() : Y2ComponentCreator( Y2ComponentBroker::BUILTIN ),
+ cpython (0) {};
+
+ ~Y2CCPython () {
+ if (cpython)
+ delete cpython;
+ }
+
+ /**
+ * Returns true, since the Python component is a YaST2 server.
+ */
+ bool isServerCreator() const { return true; };
+
+ /**
+ * Creates a new Python component.
+ */
+ Y2Component *create( const char * name ) const
+ {
+ // create as many as requested, they all share the static YPython anyway
+ if ( ! strcmp( name, "python") ) return new Y2PythonComponent();
+ else return 0;
+ }
+
+ /**
+ * always returns the same component, deletes it finally
+ */
+ Y2Component *provideNamespace (const char *name);
+
+};
+
+#endif // ifndef _Y2CCPython_h
+
+
+// EOF
Added: trunk/python-bindings/src/Y2PythonComponent.cc
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/Y2PythonComponent.cc?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/src/Y2PythonComponent.cc (added)
+++ trunk/python-bindings/src/Y2PythonComponent.cc Fri Aug 3 14:08:07 2007
@@ -0,0 +1,77 @@
+/*---------------------------------------------------------------------\
+| |
+| __ __ ____ _____ ____ |
+| \ \ / /_ _/ ___|_ _|___ \ |
+| \ V / _` \___ \ | | __) | |
+| | | (_| |___) || | / __/ |
+| |_|\__,_|____/ |_| |_____| |
+| |
+| core system |
+| (C) SuSE Linux AG |
+\----------------------------------------------------------------------/
+
+ File: Y2PythonComponent.cc
+
+ Author: Stefan Hundhammer
+ Martin Vidner
+
+/-*/
+
+#define y2log_component "Y2Python"
+#include
+#include
+#include
+
+#include "Y2PythonComponent.h"
+
+//XXX -> not rewrited
+#include "YPython.h"
+#include "YPythonNamespace.h"
+using std::string;
+
+
+Y2PythonComponent::Y2PythonComponent()
+{
+ // Actual creation of a Python interpreter is postponed until one of the
+ // YPython static methods is used. They handle that.
+
+ y2milestone( "Creating Y2PythonComponent" );
+}
+
+
+Y2PythonComponent::~Y2PythonComponent()
+{
+ YPython::destroy();
+}
+
+
+void Y2PythonComponent::result( const YCPValue & )
+{
+}
+
+
+Y2Namespace *Y2PythonComponent::import (const char* name)
+{
+ // TODO where to look for it
+ // must be the same in Y2CCPython and Y2PythonComponent
+
+ string module = YCPPathSearch::find (YCPPathSearch::Module, string (name) + ".py");
+ if (module.empty ())
+ {
+ y2internal ("Couldn't find %s after Y2CCPython pointed to us", name);
+ return NULL;
+ }
+
+ module.erase (module.size () - 3 /* strlen (".py") */);
+ YCPList args;
+ args->add (YCPString(/*module*/ name));
+
+ // load it
+ //XXX -> not rewrited
+ YPython::loadModule (args);
+
+ // introspect, create data structures for the interpreter
+ Y2Namespace *ns = new YPythonNamespace (name);
+
+ return ns;
+}
Added: trunk/python-bindings/src/Y2PythonComponent.h
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/Y2PythonComponent.h?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/src/Y2PythonComponent.h (added)
+++ trunk/python-bindings/src/Y2PythonComponent.h Fri Aug 3 14:08:07 2007
@@ -0,0 +1,75 @@
+/*---------------------------------------------------------------------\
+| |
+| __ __ ____ _____ ____ |
+| \ \ / /_ _/ ___|_ _|___ \ |
+| \ V / _` \___ \ | | __) | |
+| | | (_| |___) || | / __/ |
+| |_|\__,_|____/ |_| |_____| |
+| |
+| core system |
+| (C) SuSE Linux AG |
+\----------------------------------------------------------------------/
+
+ File: Y2PythonComponent.h
+
+
+
+/-*/
+
+
+#ifndef Y2PythonComponent_h
+#define Y2PythonComponent_h
+
+#include
+#include "Y2.h"
+
+
+/**
+ * @short YaST2 Component: Python bindings
+ */
+class Y2PythonComponent : public Y2Component
+{
+public:
+ /**
+ * Constructor.
+ */
+ Y2PythonComponent();
+
+ /**
+ * Destructor.
+ */
+ ~Y2PythonComponent();
+
+ /**
+ * The name of this component.
+ */
+ string name() const { return "python"; }
+
+ /**
+ * Is called by the generic frontend when the session is finished.
+ */
+ void result( const YCPValue & result );
+
+ /**
+ * Implements the Python:: functions.
+ **/
+// not yet, prototype the transparent bindings first
+// YCPValue evaluate( const YCPValue & val );
+
+ /**
+ * Try to import a given namespace. This method is used
+ * for transparent handling of namespaces (YCP modules)
+ * through whole YaST.
+ * @param name_space the name of the required namespace
+ * @return on errors, NULL should be returned. The
+ * error reporting must be done by the component itself
+ * (typically using y2log). On success, the method
+ * should return a proper instance of the imported namespace
+ * ready to be used. The returned instance is still owned
+ * by the component, any other part of YaST will try to
+ * free it. Thus, it's possible to share the instance.
+ */
+ Y2Namespace *import (const char* name);
+};
+
+#endif // Y2PythonComponent_h
Added: trunk/python-bindings/src/YCP.cc
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YCP.cc?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/src/YCP.cc (added)
+++ trunk/python-bindings/src/YCP.cc Fri Aug 3 14:08:07 2007
@@ -0,0 +1,205 @@
+/**
+ *
+ *
+ * This is the path from Python to YCP. It defines XSUBs.
+ */
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include "YPython.h"
+
+
+
+PyObject * Call_YCPFunction (PyObject *args);
+
+/**
+ * init namespace
+ * @param char * name of module
+ * @param char * name of function
+ * @return namespace of YCP
+ */
+
+static Y2Namespace * getNs (const char * ns_name, const char * func_name) {
+ Import import(ns_name); // has a static cache
+ Y2Namespace *ns = import.nameSpace();
+ if (ns == NULL) {
+ y2error ("... for a Python call of %s", func_name);
+ //printf("... for a Python call of %s\n", func_name);
+ } else {
+ ns->initialize ();
+ }
+ return ns;
+}
+
+
+static PyObject * ycp_handle_function(PyObject *self, PyObject *args) {
+ return Call_YCPFunction (args);
+}
+
+
+
+static PyMethodDef YCPMethods[] = {
+ {"run", ycp_handle_function, METH_VARARGS, "Calling YCP from Python"},
+ {NULL, NULL, 0, NULL} /* Sentinel */
+};
+
+PyMODINIT_FUNC initycp(void) {
+ (void) Py_InitModule("ycp", YCPMethods);
+}
+
+
+
+
+
+PyObject * Call_YCPFunction (PyObject *args) {
+
+
+ int number_args = PyTuple_Size(args);
+ char * ns_name = new char[101];
+ char * func_name = new char[101];
+ PyObject * pPythonValue;
+ PyObject * pReturnValue;
+ YCPValue ycpArg = YCPNull ();
+ YCPValue ycpRetValue = YCPNull ();
+
+
+ YPython *ypython = YPython::yPython ();
+ if (number_args >= 2) {
+ //obtain name of namespace (first argument)
+ pPythonValue = PyTuple_GetItem(args, 0);
+ if (pPythonValue) {
+ if (PyString_Check(pPythonValue)) {
+ ns_name = strcpy(ns_name, PyString_AsString(pPythonValue));
+
+ } else {
+ y2error ("Wrong type of name for namespace. String is necessary.");
+ //printf("Wrong type of name for namespace. String is necessary.\n");
+
+ return PyInt_FromLong(-1);
+ }
+
+ } else {
+ y2error ("Missing name of namespace.");
+ //printf("Missing name of namespace.\n");
+ return PyInt_FromLong(-1);
+
+ }
+ //obtain name of function (second argumet)
+ pPythonValue = PyTuple_GetItem(args, 1);
+ if (pPythonValue) {
+ if (PyString_Check(pPythonValue)) {
+ func_name = strcpy(func_name, PyString_AsString(pPythonValue));
+
+ } else {
+ y2error ("Wrong type of name for function. String is necessary.");
+ //printf("Wrong type of name for function. String is necessary..\n");
+ return PyInt_FromLong(-1);
+ }
+
+ } else {
+ y2error ("Missing name of function.");
+ //printf("Missing name of function.\n");
+ return PyInt_FromLong(-1);
+
+ }
+ //printf("namespace: %s\n", ns_name);
+ //printf("function: %s\n", func_name);
+ // create namespace
+ Y2Namespace *ns = getNs (ns_name, func_name);
+ if (ns == NULL) {
+ y2error ("Creating namespace fault.");
+ //printf("Creating namespace fault..\n");
+ return PyInt_FromLong(-1);
+ }
+ // we want either a function or a variable
+ // so find a symbol of an unspecified category
+ TableEntry *sym_te = ns->table ()->find (func_name);
+ if (sym_te == NULL) {
+ y2error ("No such symbol %s::%s", ns_name, func_name);
+ //printf("No such symbol %s::%s\n", ns_name, func_name);
+ return PyInt_FromLong(-1);
+ }
+
+ Y2Function *func_call = ns->createFunctionCall (func_name, NULL);
+ if (func_call == NULL) {
+ y2error ("No such function %s::%s", ns_name, func_name);
+ //printf("No such symbol %s::%s\n", ns_name, func_name);
+ return PyInt_FromLong(-1);
+ }
+
+ for (int i=2; i< number_args; i++) {
+ pPythonValue = PyTuple_GetItem(args, i);
+ if (pPythonValue) {
+ if (!(ypython->PythonTypeToYCPSimpleType(pPythonValue,ycpArg))) {
+ ycpArg = ypython->fromPythonListToYCPList (pPythonValue);
+ if (ycpArg.isNull())
+ ycpArg = ypython->fromPythonDictToYCPMap (pPythonValue);
+ }
+ bool ok = func_call->appendParameter (ycpArg);
+ if (!ok) {
+ y2error ("Problem with adding arguments of function %s", func_name);
+ //printf("Problem with adding arguments of function %s\n", func_name);
+ return PyInt_FromLong(-1);
+ }
+ } else {
+ y2error ("Missing argument of function.");
+ //printf("Missing argument of function.\n");
+ return PyInt_FromLong(-1);
+
+ }
+ }
+ bool ok = func_call->finishParameters ();
+ if (!ok) {
+ y2error ("Problem with finishing arguments for adding arguments of function %s", func_name);
+ //printf("Problem with finishing arguments for adding arguments of function %s\n", func_name);
+ return PyInt_FromLong(-1);
+ }
+ ycpRetValue = func_call->evaluateCall ();
+ delete func_call;
+ if (ycpRetValue.isNull ()) {
+ y2error ("Return value of function %s is NULL", func_name);
+ //printf("Return value of function %s is NULL\n", func_name);
+ return PyInt_FromLong(-1);
+ }
+ delete []ns_name;
+ delete []func_name;
+ pReturnValue = ypython->YCPTypeToPythonSimpleType(ycpRetValue);
+ if (!pReturnValue)
+ pReturnValue = ypython->fromYCPListToPythonList(ycpRetValue);
+ if (!pReturnValue)
+ pReturnValue = ypython->fromYCPMapToPythonDict(ycpRetValue);
+ return pReturnValue;
+
+ } else {
+ y2error ("Number of arguments is not enough.");
+ //printf("Number of arguments is not enough.\n");
+ return PyInt_FromLong(-1);
+ }
+
+}
+
+int
+main(int argc, char *argv[])
+{
+ /* Pass argv[0] to the Python interpreter */
+ Py_SetProgramName(argv[0]);
+
+ /* Initialize the Python interpreter. Required. */
+ Py_Initialize();
+
+ /* Add a static module */
+ initycp();
+
+ Py_Finalize();
+}
Added: trunk/python-bindings/src/YPython.cc
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YPython.cc?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/src/YPython.cc (added)
+++ trunk/python-bindings/src/YPython.cc Fri Aug 3 14:08:07 2007
@@ -0,0 +1,415 @@
+/*---------------------------------------------------------------------\
+| |
+| __ __ ____ _____ ____ |
+| \ \ / /_ _/ ___|_ _|___ \ |
+| \ V / _` \___ \ | | __) | |
+| | | (_| |___) || | / __/ |
+| |_|\__,_|____/ |_| |_____| |
+| |
+| core system |
+| (C) SuSE Linux AG |
+\----------------------------------------------------------------------/
+
+ File: YPython.cc
+
+
+
+/-*/
+
+#include
+#include
+#include <list>
+#include <iosfwd>
+#include <sstream>
+#include <iomanip>
+
+
+
+
+#define y2log_component "Y2Python"
+#include
+#include
+
+
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+YPython * YPython::_yPython = 0;
+
+YPython::YPython()
+{
+
+}
+
+
+YPython::~YPython()
+{
+
+}
+
+
+YPython *
+YPython::yPython()
+{
+ if ( ! _yPython )
+ _yPython = new YPython();
+
+ return _yPython;
+}
+
+
+
+YCPValue
+YPython::destroy()
+{
+ y2milestone( "Shutting down embedded Python interpreter." );
+
+ if ( _yPython )
+ delete _yPython;
+
+ _yPython = 0;
+ Py_Finalize();
+ return YCPVoid();
+}
+
+
+
+/**
+ * Loads a module.
+ */
+YCPValue
+YPython::loadModule( YCPList argList )
+{
+ Py_Initialize();
+
+ return YCPVoid();
+}
+
+/**
+ * evaluate of function from python
+ * @param string name of module
+ * @param string name of function
+ * @param bool is it method?
+ * @param YCPList argumnets from YCP to python function(0 - dummy)
+ * @return YCPValue return value from python's function
+ */
+YCPValue
+YPython::callInner (string module, string function, bool method,
+ YCPList argList)
+{
+
+
+
+ PyObject* pMain;
+ PyObject* pMainDict;
+ PyObject* pFunc;
+ PyObject* pArgs;
+ PyObject* pReturn;
+
+ YCPValue result = YCPNull ();
+
+
+ pMain = PyImport_AddModule("__main__");
+ //PyRun_SimpleString("import __main__");
+ pMainDict = PyModule_GetDict(pMain);
+
+
+
+ pFunc = PyDict_GetItemString(pMainDict, function.c_str());
+
+
+ pArgs = PyTuple_New(argList->size()-1);
+
+ PyObject *pArg;
+ //Parsing argumments
+
+ y2milestone ("name of function %s and nuber of arguments %d", function.c_str(), argList->size()-1);
+
+ for ( int i=1; i < argList->size(); i++ ) {
+ //FIXME add checking for all types from YCP and conversion to python types...
+
+ pArg = YCPTypeToPythonSimpleType(argList->value(i));
+
+ if (!pArg)
+ pArg = fromYCPListToPythonList(argList->value(i));
+ if (!pArg)
+ pArg = fromYCPMapToPythonDict(argList->value(i));
+
+ PyTuple_SetItem(pArgs,i-1, pArg);
+
+ }
+
+
+ pReturn = PyObject_CallObject(pFunc, pArgs);
+
+ Py_CLEAR(pArgs);
+
+
+
+ if (pReturn) {
+ //result = YCPInteger(PyInt_AsLong(pReturn)); //new YCPValue();
+
+ if (!PythonTypeToYCPSimpleType(pReturn, result))
+ result = fromPythonListToYCPList (pReturn);
+
+ if (result.isNull())
+ result = fromPythonDictToYCPMap (pReturn);
+
+
+ }
+ Py_CLEAR(pReturn);
+
+ if (result.isNull ()) {
+ y2error ("Result is NULL when returning from %s", function.c_str());
+ result = YCPVoid ();
+ }
+
+ return result;
+}
+
+
+ /**
+ * Transfer from python simple type to ycp simple type
+ * transfered are: boolean, integer, string, float,
+ **/
+bool YPython::PythonTypeToYCPSimpleType(PyObject* pPythonValue, YCPValue &out) {
+
+ // boolean value handling
+ if (PyBool_Check(pPythonValue)) {
+ int compare = PyObject_Compare(pPythonValue, Py_True);
+ if (compare == 0) {
+ out = YCPBoolean (true);
+ } else if (compare < 0) {
+ out = YCPBoolean (false);
+ } else {
+ return false;
+ }
+ return true;
+ }
+
+ //integer value handling
+ if (PyInt_Check(pPythonValue)) {
+
+ out = YCPInteger(PyInt_AsLong(pPythonValue));
+ return true;
+ }
+
+ //float value handling
+ if (PyFloat_Check(pPythonValue)) {
+
+ out = YCPFloat(PyFloat_AsDouble(pPythonValue));
+ return true;
+ }
+
+ //string value handling
+ if (PyString_Check(pPythonValue)) {
+
+ out = YCPString(PyString_AsString(pPythonValue));
+ return true;
+ }
+
+ return false;
+}
+
+ /**
+ * Transfer from ycp simple type to python simple type
+ * transfered are: boolean, integer, string, float,
+ **/
+PyObject* YPython::YCPTypeToPythonSimpleType(YCPValue in) {
+ if (in->isBoolean()) {
+ long val = 0;
+ bool true_false = in->asBoolean()->value();
+ true_false ? val = 1 : val =0;
+ return PyBool_FromLong(val);
+
+ } else if (in->isInteger()) {
+ return PyInt_FromLong(in->asInteger()->value());
+
+ } else if (in->isFloat()) {
+ double val = in->asFloat()->value();
+ return PyFloat_FromDouble(val);
+
+ } else if (in->isString()) {
+ return PyString_FromString((in->asString()->value()).c_str());
+
+ } else
+ return NULL;
+}
+
+ /**
+ * Convert a Python list to a YCPList.
+ **/
+YCPList YPython::fromPythonListToYCPList (PyObject* pPythonList) {
+ YCPList ycp_List;
+ PyObject * pItem;
+ YCPValue ycp_value;
+ //TODO add checking for list (is it really list?)
+ if (PyList_Check(pPythonList) > 0) {
+ int list_size = PyList_Size(pPythonList);
+ for (int i = 0; i < list_size; i++) {
+ pItem = PyList_GetItem(pPythonList, i);
+ if (PythonTypeToYCPSimpleType(pItem, ycp_value)) {
+ if (!ycp_value.isNull ()) {
+ ycp_List->add(ycp_value);
+ } else {
+ return YCPNull ();
+ }
+ } else { //end of if (PythonTypeToYCPSimpleType(item, ycp_value))
+ return YCPNull ();
+ } // end of else for if (PythonTypeToYCPSimpleType(item, ycp_value))
+ } //end of for (int i = 0; i < list_size; i++)
+ } else { //end if (PyList_Check(pPythonList) > 0)
+ y2milestone ("Value is not Python List");
+ return YCPNull ();
+ } //end else of if (PyList_Check(pPythonList) > 0)
+
+ return ycp_List;
+}
+
+ /**
+ * Convert a YCPList to a Python list.
+ **/
+PyObject* YPython::fromYCPListToPythonList (YCPValue ycp_List) {
+
+ PyObject* pPythonList;
+ PyObject* pItem;
+ int ret = 0;
+ if (ycp_List->isList()) {
+ if (ycp_List->asList()->size()>0)
+ pPythonList = PyList_New(ycp_List->asList()->size());
+ else {
+ y2error("YCP list is empty.");
+ return NULL;
+ }
+ y2milestone ("Size of list %d",ycp_List->asList()->size());
+ for ( int i = 0; i < ycp_List->asList()->size(); i++ ) {
+ pItem = YCPTypeToPythonSimpleType(ycp_List->asList()->value(i));
+ ret = PyList_SetItem(pPythonList, i, pItem);
+
+ if (ret <0)
+ y2error("PyList_SetItem doesn't add item into python list.");
+
+ }
+ Py_INCREF(pPythonList);
+ return pPythonList;
+
+ } else {
+ y2milestone ("Value is not YCPList");
+ return NULL;
+ }
+}
+
+
+ /**
+ * Convert a YCPMap to a Python Dictionary.
+ **/
+PyObject* YPython::fromYCPMapToPythonDict (YCPValue ycp_Map) {
+
+ PyObject* pPythonDict;
+ PyObject* pKey;
+ PyObject* pValue;
+ int ret = -1;
+ if (ycp_Map->isMap()) {
+ if (ycp_Map->asMap()->size()>0) {
+ pPythonDict = PyDict_New();
+ for (YCPMapIterator it = ycp_Map->asMap()->begin(); it != ycp_Map->asMap()->end(); ++it ) {
+ pKey = YCPTypeToPythonSimpleType(it.key());
+ if (!pKey) {
+ y2error("Transfer key YCPMap to PyObject falsed.");
+ return NULL;
+ }
+ pValue = YCPTypeToPythonSimpleType(it.value());
+ if (!pValue)
+ pValue = fromYCPListToPythonList(it.value());
+ if (!pValue)
+ pValue = fromYCPMapToPythonDict(it.value());
+ if (pValue) {
+ ret = PyDict_SetItem(pPythonDict, pKey, pValue);
+ if (ret < 0) {
+ y2error("Adding value and key from YCPMap to Python Dictionary falsed.");
+ return NULL;
+ }
+ } else {
+ y2error("Transfer value from YCPMap to PyObject falsed.");
+ return NULL;
+
+ }
+ } // end of for (YCPMapIterator it = ycp_Map->begin(); it != ycp_Map->end(); ++it )
+
+ } else { // end if (ycp_Map->asMap()->size()>0)
+ y2error("YCP map is empty.");
+ return NULL;
+ } // end else of if (ycp_Map->asMap()->size()>0)
+
+ } else { // end if (ycp_Map->isMap())
+ y2milestone ("Value is not YCPMap");
+ return NULL;
+ } // end else of if (ycp_Map->isMap())
+ Py_INCREF(pPythonDict);
+ return pPythonDict;
+}
+
+
+ /**
+ * Convert a Python Dictionary to a YCPMap.
+ **/
+YCPMap YPython::fromPythonDictToYCPMap (PyObject* pPythonDict) {
+
+ PyObject* pListKeys;
+ PyObject* pListValues;
+ YCPValue ycp_key;
+ YCPValue ycp_value;
+ YCPMap ycp_Map;
+ if (PyDict_Check(pPythonDict)>0) {
+ if (PyDict_Size(pPythonDict)>0) {
+ pListKeys = PyDict_Keys(pPythonDict);
+ pListValues = PyDict_Values(pPythonDict);
+ for (int i=0; i < PyList_Size(pListKeys); i++) {
+ if (PythonTypeToYCPSimpleType(PyList_GetItem(pListKeys, i),ycp_key)) {
+ if (PythonTypeToYCPSimpleType(PyList_GetItem(pListValues, i),ycp_value)) {
+ ycp_Map->add (ycp_key, ycp_value);
+ } else {
+ ycp_value = fromPythonListToYCPList(PyList_GetItem(pListValues, i));
+ if (ycp_value.isNull ())
+ ycp_value =fromPythonDictToYCPMap (PyList_GetItem(pListValues, i));
+ ycp_Map->add (ycp_key, ycp_value);
+ }
+
+ } else {
+ y2error("Cannot convert key from python dictionary");
+ return YCPNull ();
+ }
+ }
+
+ } else {
+ y2milestone ("Python dictionary is empty");
+ return YCPNull ();
+ }
+ } else {
+ y2milestone ("Value is not python dictionary");
+ return YCPNull ();
+ }
+ return ycp_Map;
+}
+
+
+
+
+
+
+
+
+
Added: trunk/python-bindings/src/YPython.h
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YPython.h?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/src/YPython.h (added)
+++ trunk/python-bindings/src/YPython.h Fri Aug 3 14:08:07 2007
@@ -0,0 +1,121 @@
+/*-----------------------------------------------------------*- c++ -*-\
+| |
+| __ __ ____ _____ ____ |
+| \ \ / /_ _/ ___|_ _|___ \ |
+| \ V / _` \___ \ | | __) | |
+| | | (_| |___) || | / __/ |
+| |_|\__,_|____/ |_| |_____| |
+| |
+| core system |
+| (C) SuSE Linux AG |
+\----------------------------------------------------------------------/
+
+ File: YPython.h
+
+
+/-*/
+
+
+#ifndef YPython_h
+#define YPython_h
+
+#include
+
+#include
+#include
+
+
+class YPython
+{
+public:
+
+ /**
+ * Load a Perl module - equivalent to "use" in Python.
+ *
+ * Returns a YCPError on failure, YCPVoid on success.
+ **/
+ static YCPValue loadModule( YCPList argList );
+
+ /**
+ * Access the static (singleton) YPython object. Create it if it isn't
+ * created yet.
+ *
+ * Returns 0 on error.
+ **/
+ static YPython * yPython();
+
+
+
+ /**
+ * Destroy the static (singleton) YPython object and unload the embedded Perl
+ * interpreter.
+ *
+ * Returns YCPVoid().
+ **/
+ static YCPValue destroy();
+
+ /**
+ * Generic Python call.
+ **/
+ YCPValue callInner (string module, string function, bool method,
+ YCPList argList);
+
+ static YPython * _yPython;
+
+
+ /**
+ * Transfer from python simple type to ycp simple type
+ * transfered are: boolean, integer, string, float,
+ **/
+ bool PythonTypeToYCPSimpleType(PyObject* pPythonValue, YCPValue &out);
+
+ /**
+ * Transfer from ycp simple type to python simple type
+ * transfered are: boolean, integer, string, float,
+ **/
+ PyObject* YCPTypeToPythonSimpleType(YCPValue in);
+
+ /**
+ * Convert a Python list to a YCPList.
+ **/
+ YCPList fromPythonListToYCPList (PyObject* pPythonList);
+
+ /**
+ * Convert a YCPList to a Python list.
+ **/
+ PyObject* fromYCPListToPythonList (YCPValue ycp_List);
+
+ /**
+ * Convert a Python Dictionary to a YCPMap.
+ **/
+ YCPMap fromPythonDictToYCPMap (PyObject* pPythonDict);
+
+
+ /**
+ * Convert a YCPMap to a Python Dictionary.
+ **/
+ PyObject* fromYCPMapToPythonDict (YCPValue ycp_Map);
+
+
+
+protected:
+
+ /**
+ * Protected constructor. Use one of the static methods rather than
+ * instantiate an object of this class yourself.
+ **/
+ YPython();
+
+ /**
+ * Destructor.
+ **/
+ ~YPython();
+
+
+
+
+
+};
+
+
+#endif // YPython_h
Added: trunk/python-bindings/src/YPythonNamespace.cc
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YPythonNamespace.cc?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/src/YPythonNamespace.cc (added)
+++ trunk/python-bindings/src/YPythonNamespace.cc Fri Aug 3 14:08:07 2007
@@ -0,0 +1,282 @@
+/**
+ *
+ *
+ * This is the path from YCP to Python.
+ */
+
+#include
+#include "YPythonNamespace.h"
+
+#define y2log_component "Y2PythonNamespace"
+#include
+
+#include
+#include
+#include
+
+#include
+#include
+
+
+
+/**
+ * The definition of a function that is implemented in Perl
+ */
+class Y2PythonFunctionCall : public Y2Function
+{
+ //! module name
+ string m_module_name;
+ //! function name, excluding module name
+ string m_local_name;
+ //! function type
+ constFunctionTypePtr m_type;
+ //! data prepared for the inner call
+ YCPList m_call;
+
+public:
+ Y2PythonFunctionCall (const string &module_name,
+ const string &local_name,
+ constFunctionTypePtr function_type
+ ) :
+ m_module_name (module_name),
+ m_local_name (local_name),
+ m_type (function_type)
+ {
+ // placeholder, formerly function name
+ m_call->add (YCPVoid ());
+ }
+
+ //! if true, the perl function is passed the module name
+ virtual bool isMethod () = 0;
+
+ //! called by YEFunction::evaluate
+ virtual YCPValue evaluateCall ()
+ {
+ return YPython::yPython()->callInner (
+ m_module_name, m_local_name, isMethod (),
+ m_call);
+ }
+
+ /**
+ * Attaches a parameter to a given position to the call.
+ * @return false if there was a type mismatch
+ */
+ virtual bool attachParameter (const YCPValue& arg, const int position)
+ {
+ m_call->set (position+1, arg);
+ return true;
+ }
+
+ /**
+ * What type is expected for the next appendParameter (val) ?
+ * (Used when calling from Perl, to be able to convert from the
+ * simple type system of Perl to the elaborate type system of YCP)
+ * @return Type::Any if number of parameters exceeded
+ */
+ virtual constTypePtr wantedParameterType () const
+ {
+ // -1 for the function name
+ int params_so_far = m_call->size ()-1;
+ return m_type->parameterType (params_so_far);
+ }
+
+ /**
+ * Appends a parameter to the call.
+ * @return false if there was a type mismatch
+ */
+ virtual bool appendParameter (const YCPValue& arg)
+ {
+ m_call->add (arg);
+ return true;
+ }
+
+ /**
+ * Signal that we're done adding parameters.
+ * @return false if there was a parameter missing
+ */
+ virtual bool finishParameters () { return true; }
+
+
+ virtual bool reset ()
+ {
+ m_call = YCPList ();
+ // placeholder, formerly function name
+ m_call->add (YCPVoid ());
+ return true;
+ }
+
+ /**
+ * Something for remote namespaces
+ */
+ virtual string name () const { return m_local_name; }
+};
+
+class Y2PythonSubCall : public Y2PythonFunctionCall {
+public:
+ Y2PythonSubCall (const string &module_name,
+ const string &local_name,
+ constFunctionTypePtr function_type
+ ) :
+ Y2PythonFunctionCall (module_name, local_name, function_type)
+ {}
+ virtual bool isMethod () { return false; }
+};
+
+class Y2PythonMethodCall : public Y2PythonFunctionCall {
+public:
+ Y2PythonMethodCall (const string &module_name,
+ const string &local_name,
+ constFunctionTypePtr function_type
+ ) :
+ Y2PythonFunctionCall (module_name, local_name, function_type)
+ {}
+ virtual bool isMethod () { return true; }
+};
+
+
+
+YPythonNamespace::YPythonNamespace (string name)
+ : m_name (name),
+ m_all_methods (true)
+{
+
+ const char * c_name = m_name.c_str ();
+
+ //Objects for Python API
+ PyObject* pMain; //main module
+ PyObject* pMainDict; //global dictionary of variables from __main__
+ PyObject* pFunc; //pionter for function from python
+ 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__
+
+ FILE* file;
+
+ int num_fun_names = 0;
+ int count = 0;
+ long num = 0;
+ // args = inspect.getargs(name_function.func_code)
+ char * command = new char[201]; //command for obtain arguments of function
+
+ //add __main__ module
+ pMain = PyImport_AddModule("__main__");
+ PyRun_SimpleString("import __main__");
+ //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");
+ if (file) {
+ PyRun_SimpleFile(file, strcat((char *) c_name, ".py"));
+ }
+ //symbols from __main__
+ PyRun_SimpleString("fun_names = dir(__main__)");
+ fun_names = PyDict_GetItemString(pMainDict, "fun_names");
+ //number of symbols
+ num_fun_names = PyList_Size(fun_names);
+
+ //check each symbol and try to find function names
+ for (int i = 0; i < num_fun_names; i++) {
+ //y2milestone ("YPythonNamespace iteration %d from all %d", i, num_fun_names);
+ item = PyList_GetItem(fun_names, i); /* Canât fail */
+ //y2milestone ("YPythonNamespace item: %s", PyString_AsString(item));
+ if (!PyString_Check(item)) continue; /* Skip non-string */
+ //y2milestone ("item: %s", PyString_AsString(item));
+ pFunc = PyDict_GetItemString(pMainDict,PyString_AsString(item));
+ //check if symbol is callable
+
+ if (PyFunction_Check(pFunc)) {
+ FunctionTypePtr sym_tp = new FunctionType (Type::Any);
+
+ //build command for obtaining number of function
+ strcpy(command, "");
+ strcpy(command, "args = inspect.getargs(");
+ command = strcat(command, PyString_AsString(item));
+ command = strcat(command, ".func_code)");
+ //run the command: args = inspect.getargs(name_function.func_code)
+ PyRun_SimpleString(command);
+ PyRun_SimpleString("num_args = len(args[0])");
+ num_args = PyDict_GetItemString(pMainDict, "num_args");
+ num = PyInt_AsLong(num_args);
+ //y2milestone ("Number of parameters: %d", num);
+ //add types and number of arguments into SymbolEntry table
+ for (long j = 0; j < num; j++) {
+ sym_tp->concat(Type::Any);
+ }
+ //y2milestone ("Callable function %s", PyString_AsString(item));
+ // symbol entry for the function
+ SymbolEntry *fun_se = new SymbolEntry (
+ this,
+ count++, // position. arbitrary numbering. must stay consistent when?
+ PyString_AsString(item), // passed to Ustring, no need to strdup
+ SymbolEntry::c_function,
+ sym_tp);
+ fun_se->setGlobal (true);
+
+ // enter it to the symbol table
+ enterSymbol (fun_se, 0);
+ }
+ } // end of for (int i = 0; i < num_fun_names; i++)
+
+ delete []command;
+ Py_CLEAR(fun_names);
+
+
+
+ y2milestone ("YPythonNamespace finish");
+
+}
+
+YPythonNamespace::~YPythonNamespace ()
+{
+}
+
+const string YPythonNamespace::filename () const
+{
+ // TODO improve
+ return ".../" + m_name;
+}
+
+// this is for error reporting only?
+string YPythonNamespace::toString () const
+{
+ y2error ("TODO");
+ return "{\n"
+ "/* this namespace is provided in Python */\n"
+ "}\n";
+}
+
+// called when running and the import statement is encountered
+// does initialization of variables
+// constructor is handled separately after this
+YCPValue YPythonNamespace::evaluate (bool cse)
+{
+ // so we don't need to do anything
+ y2debug ("Doing nothing");
+ return YCPNull ();
+}
+
+// It seems that this is the standard implementation. why would we
+// ever want it to be different?
+Y2Function* YPythonNamespace::createFunctionCall (const string name, constFunctionTypePtr required_type)
+{
+ y2debug ("Python creating function call for %s", name.c_str ());
+ TableEntry *func_te = table ()->find (name.c_str (), SymbolEntry::c_function);
+ if (func_te)
+ {
+ constTypePtr t = required_type ? required_type : (constFunctionTypePtr)func_te->sentry()->type ();
+ if (m_all_methods)
+ {
+ return new Y2PythonMethodCall (m_name, name, t);
+ }
+ else
+ {
+ return new Y2PythonSubCall (m_name, name, t);
+ }
+ }
+ y2error ("No such function %s", name.c_str ());
+ return NULL;
+}
Added: trunk/python-bindings/src/YPythonNamespace.h
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/src/YPythonNamespace.h?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/src/YPythonNamespace.h (added)
+++ trunk/python-bindings/src/YPythonNamespace.h Fri Aug 3 14:08:07 2007
@@ -0,0 +1,36 @@
+// -*- c++ -*-
+#include
+#include
+#include
+#include
+
+/**
+ * YaST interface to a Python module
+ */
+class YPythonNamespace : public Y2Namespace
+{
+private:
+ string m_name; //! this namespace's name, eg. XML::Writer
+ bool m_all_methods; //! add the class name to all calls
+public:
+ /**
+ * Construct an interface. The module must be already loaded
+ * @param name eg "XML::Writer"
+ */
+ YPythonNamespace (string name);
+
+ virtual ~YPythonNamespace ();
+
+ //! what namespace do we implement
+ virtual const string name () const { return m_name; }
+ //! used for error reporting
+ virtual const string filename () const;
+
+ //! unparse. useful only for YCP namespaces??
+ virtual string toString () const;
+ //! called when evaluating the import statement
+ // constructor is handled separately
+ virtual YCPValue evaluate (bool cse = false);
+
+ virtual Y2Function* createFunctionCall (const string name, constFunctionTypePtr requiredType);
+};
Added: trunk/python-bindings/yast2-python-bindings.spec.in
URL: http://svn.opensuse.org/viewcvs/yast/trunk/python-bindings/yast2-python-bindings.spec.in?rev=39937&view=auto
==============================================================================
--- trunk/python-bindings/yast2-python-bindings.spec.in (added)
+++ trunk/python-bindings/yast2-python-bindings.spec.in Fri Aug 3 14:08:07 2007
@@ -0,0 +1,37 @@
+@HEADER-COMMENT@
+
+@HEADER@
+BuildRequires: gcc-c++ yast2-core-devel yast2-devtools python-devel
+
+
+
+# YCPValue::valuetype_str()
+Requires: yast2-core >= 2.13.28
+Requires: python
+
+
+Summary: -
+
+%description
+-
+
+@PREP@
+
+@BUILD@
+
+@INSTALL@
+rm $RPM_BUILD_ROOT/@plugindir@/libpy2lang_python.la
+
+@CLEAN@
+
+%files
+%defattr (-, root, root)
+@plugindir@/libpy2lang_python.so.*
+@plugindir@/libpy2lang_python.so
+
+
+# libYCP goes elsewhere
+%dir @libdir@/python
+@libdir@/python/site-packages
+%dir @moduledir@
+%doc @docdir@
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org