net: Support usage/help of subcommands implemented in Python.
authorJelmer Vernooij <jelmer@samba.org>
Mon, 28 Dec 2009 00:21:27 +0000 (01:21 +0100)
committerJelmer Vernooij <jelmer@ganieda.vernstok.nl>
Tue, 29 Dec 2009 15:26:19 +0000 (16:26 +0100)
source4/scripting/python/samba/netcmd/__init__.py
source4/utils/net/net.c

index 54f3134d2f57dff33d13520a96172d13c0aa1951..f644febba604f1096e3256ad159e27a659197dc2 100644 (file)
@@ -23,6 +23,9 @@ class Command(object):
     def _get_description(self):
         return self.__doc__
 
+    def usage(self):
+        raise NotImplementedError
+
     description = property(_get_description)
 
     def run(self):
@@ -31,3 +34,4 @@ class Command(object):
 
 
 commands = {}
+commands["foo"] = Command()
index 29d736fb15106eb48d5ac72737e02d63e94da4fc..298204b01f01bae0b514680032a96076a87bf630 100644 (file)
 #include <Python.h>
 #include "scripting/python/modules.h"
 
+static PyObject *py_commands(void)
+{
+       PyObject *netcmd_module, *py_cmds;
+       netcmd_module = PyImport_ImportModule("samba.netcmd");
+       if (netcmd_module == NULL) {
+               PyErr_Print();
+               return NULL;
+       }       
+
+       py_cmds = PyObject_GetAttrString(netcmd_module, "commands");
+       if (py_cmds == NULL) {
+               PyErr_Print();
+               return NULL;
+       }
+
+       if (!PyDict_Check(py_cmds)) {
+               fprintf(stderr, "Python net commands is not a dictionary\n");
+               return NULL;
+       }
+
+       return py_cmds;
+}
+
 /*
   run a function from a function table. If not found then
   call the specified usage function 
@@ -62,6 +85,7 @@ int net_run_function(struct net_context *ctx,
                        int (*usage_fn)(struct net_context *ctx, int argc, const char **argv))
 {
        int i;
+       PyObject *py_cmds, *py_cmd;
 
        if (argc == 0) {
                return usage_fn(ctx, argc, argv);
@@ -75,6 +99,21 @@ int net_run_function(struct net_context *ctx,
                        return functable[i].fn(ctx, argc-1, argv+1);
        }
 
+       py_cmds = py_commands();
+       if (py_cmds == NULL) {
+               return 1;
+       }
+
+       py_cmd = PyDict_GetItemString(py_cmds, argv[0]);
+       if (py_cmd != NULL) {
+               PyObject *ret = PyObject_CallMethod(py_cmd, "run", "");
+               if (ret == NULL) {
+                       PyErr_Print();
+                       return 1;
+               }
+               return PyInt_AsLong(ret);
+       }
+
        d_printf("No command: %s\n", argv[0]);
        return usage_fn(ctx, argc, argv);
 }
@@ -87,6 +126,7 @@ int net_run_usage(struct net_context *ctx,
                        const struct net_functable *functable)
 {
        int i;
+       PyObject *py_cmds, *py_cmd;
 
        for (i=0; functable[i].name; i++) {
                if (strcasecmp_m(argv[0], functable[i].name) == 0)
@@ -95,6 +135,21 @@ int net_run_usage(struct net_context *ctx,
                        }
        }
 
+       py_cmds = py_commands();
+       if (py_cmds == NULL) {
+               return 1;
+       }
+
+       py_cmd = PyDict_GetItemString(py_cmds, argv[0]);
+       if (py_cmd != NULL) {
+               PyObject *ret = PyObject_CallMethod(py_cmd, "usage", "");
+               if (ret == NULL) {
+                       PyErr_Print();
+                       return 1;
+               }
+               return PyInt_AsLong(ret);
+       }
+
        d_printf("No usage information for command: %s\n", argv[0]);
 
        return 1;
@@ -135,25 +190,12 @@ static int net_help_builtin(const struct net_functable *ftable)
 
 static int net_help_python(void)
 {
-       PyObject *netcmd_module;
        PyObject *py_cmds;
        PyObject *key, *value;
        Py_ssize_t pos = 0;
 
-       netcmd_module = PyImport_ImportModule("samba.netcmd");
-       if (netcmd_module == NULL) {
-               PyErr_Print();
-               return 1;
-       }       
-
-       py_cmds = PyObject_GetAttrString(netcmd_module, "commands");
+       py_cmds = py_commands();
        if (py_cmds == NULL) {
-               PyErr_Print();
-               return 1;
-       }
-
-       if (!PyDict_Check(py_cmds)) {
-               fprintf(stderr, "Python net commands is not a dictionary\n");
                return 1;
        }