From ae63d5e69f892d180ee0ce5f3e424b4b4f79cbd8 Mon Sep 17 00:00:00 2001 From: oetiker Date: Mon, 6 Apr 2009 14:49:03 +0000 Subject: [PATCH] I thought that it would be nice to be able to pass parameters to python bindings as a list (array) of strings. -- Vytautas Zdanavicius vytaszd at yahoo.com git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk/program@1771 a5681a0c-68f1-0310-ab6d-d61299d08faa --- CONTRIBUTORS | 4 +-- bindings/python/rrdtoolmodule.c | 59 +++++++++++++++++++++++++-------- doc/rrdpython.pod | 28 +++++++++++++--- 3 files changed, 70 insertions(+), 21 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 29bbcae..9fcddbd 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -79,10 +79,10 @@ Wim Heirman --units=si option Wolfgang Schrimm xport function Wrolf Courtney (HP-UX) hendrik visage +Vytautas Zdanavicius -- python argument list exander Martin Sperl (CDEF prediction functions, libdbi) Philippe Simonet (Windows Binaries) -Alexander Lucke (lucke with dns-net.de) - of DNS:NET Internet Services (www.dns-net.de) http://rrdtool.org +Alexander Lucke (lucke with dns-net.de) of DNS:NET Internet Services (www.dns-net.de) http://rrdtool.org Hedley Simons Nicola Worthington Wegmann, Christof 1.3/trunk win32 port diff --git a/bindings/python/rrdtoolmodule.c b/bindings/python/rrdtoolmodule.c index bd16da1..6cc22ad 100644 --- a/bindings/python/rrdtoolmodule.c +++ b/bindings/python/rrdtoolmodule.c @@ -58,28 +58,59 @@ static int create_args( int *argc, char ***argv) { - PyObject *o; - int size, i; - - size = PyTuple_Size(args); + PyObject *o, *lo; + int args_count, + argv_count, + element_count, + i, j; + + args_count = PyTuple_Size(args); + element_count = 0; + for (i = 0; i < args_count; i++) { + o = PyTuple_GET_ITEM(args, i); + if (PyString_Check(o)) + element_count++; + else if (PyList_CheckExact(o)) + element_count += PyList_Size(o); + else { + PyErr_Format(PyExc_TypeError, "argument %d must be string or list of strings", i); + return -1; + } + } + *argv = PyMem_New(char *, - size + 1); + element_count + 1); if (*argv == NULL) return -1; - for (i = 0; i < size; i++) { + argv_count = 0; + for (i = 0; i < args_count; i++) { o = PyTuple_GET_ITEM(args, i); - if (PyString_Check(o)) - (*argv)[i + 1] = PyString_AS_STRING(o); - else { - PyMem_Del(*argv); - PyErr_Format(PyExc_TypeError, "argument %d must be string", i); - return -1; - } + if (PyString_Check(o)) { + argv_count++; + (*argv)[argv_count] = PyString_AS_STRING(o); + } else if (PyList_CheckExact(o)) + for (j = 0; j < PyList_Size(o); j++) { + lo = PyList_GetItem(o, j); + if (PyString_Check(lo)) { + argv_count++; + (*argv)[argv_count] = PyString_AS_STRING(lo); + } else { + PyMem_Del(*argv); + PyErr_Format(PyExc_TypeError, "element %d in argument %d must be string", j, i); + return -1; + } + } + else { + PyMem_Del(*argv); + PyErr_Format(PyExc_TypeError, "argument %d must be string or list of strings", i); + return -1; + } } + (*argv)[0] = command; - *argc = size + 1; + *argc = element_count + 1; /* reset getopt state */ opterr = optind = 0; diff --git a/doc/rrdpython.pod b/doc/rrdpython.pod index f041b0b..b6f9075 100644 --- a/doc/rrdpython.pod +++ b/doc/rrdpython.pod @@ -13,13 +13,15 @@ The B functions are directly callable via the Python programming language. This wrapper implementation has been written from the scratch (without SWIG) -The API's simply expects string parameters to the functions. Please refer -to the other B documentation for functions and valid arguments. +The API's expects strings and/or list of strings as parameters to the functions. +Please refer to the other B documentation for functions and valid arguments. -=head1 EXAMPLE +=head1 EXAMPLES + +=head2 Example 1 import sys - sys.path.append('/path/to/rrdtool/lib/python2.3/site-packages/') + sys.path.append('/path/to/rrdtool/lib/python2.6/site-packages/') import rrdtool, tempfile DAY = 86400 @@ -42,6 +44,23 @@ to the other B documentation for functions and valid arguments. print info['last_update'] print info['ds']['downloads']['minimal_heartbeat'] +=head2 Example 2 + + import sys + sys.path.append('/path/to/rrdtool/lib/python2.6/site-packages/') + import rrdtool + + # in real life data_sources would be populated in loop or something similar + data_sources=[ 'DS:speed1:COUNTER:600:U:U', + 'DS:speed2:COUNTER:600:U:U', + 'DS:speed3:COUNTER:600:U:U' ] + + rrdtool.create( 'speed.rrd', + '--start', '920804400', + data_sources, + 'RRA:AVERAGE:0.5:1:24', + 'RRA:AVERAGE:0.5:6:10' ) + If you use the B make target you can drop to first sys.path.append line since the rrdtool module will be available everywhere. @@ -57,4 +76,3 @@ rrdxport, rrdinfo Hye-Shik Chang Eperky@i18n.orgE Alan Milligan Ealan.milligan@last-bastion.netE - -- 2.30.2