I thought that it would be nice to be able to pass
authoroetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa>
Mon, 6 Apr 2009 14:49:03 +0000 (14:49 +0000)
committeroetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa>
Mon, 6 Apr 2009 14:49:03 +0000 (14:49 +0000)
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
bindings/python/rrdtoolmodule.c
doc/rrdpython.pod

index 29bbcae..9fcddbd 100644 (file)
@@ -79,10 +79,10 @@ Wim Heirman <wim.heirman elis.ugent.be> --units=si option
 Wolfgang Schrimm <wschrimm with uni-hd.de> xport function
 Wrolf Courtney <wrolf with wrolf.net> (HP-UX)
 hendrik visage <hvisage with is.co.za>
+Vytautas Zdanavicius <vytaszd@yahoo.com> -- python argument list exander
 Martin Sperl <rrdtool martin.sperl.org> (CDEF prediction functions, libdbi)
 Philippe Simonet <philippe.simonet with swisscom.ch> (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 <heds@metahusky.net>
 Nicola Worthington <nicolaw@cpan.org>
 Wegmann, Christof <Christof.Wegmann@exitgames.com> 1.3/trunk win32 port
index bd16da1..6cc22ad 100644 (file)
@@ -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;
index f041b0b..b6f9075 100644 (file)
@@ -13,13 +13,15 @@ The B<rrdtool> 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<rrdtool> 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<rrdtool> 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<rrdtool> 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<site-python-install> 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 E<lt>perky@i18n.orgE<gt>
 
 Alan Milligan E<lt>alan.milligan@last-bastion.netE<gt>
-