python: Provide way to iterate over available shares.
authorJelmer Vernooij <jelmer@samba.org>
Tue, 16 Jun 2009 00:24:43 +0000 (02:24 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Tue, 16 Jun 2009 00:28:10 +0000 (02:28 +0200)
source4/bin/python/samba/tests/shares.py
source4/param/pyparam.c
source4/scripting/python/samba/shares.py

index 0a771b1754ff48d4d68f827a721b58a81472a733..9130c36780125d85deb51220827dae4b1d8945cf 100644 (file)
@@ -20,15 +20,47 @@ from samba.shares import SharesContainer
 from unittest import TestCase
 
 
+class MockService(object):
+
+    def __init__(self, data):
+        self.data = data
+
+    def __getitem__(self, name):
+        return self.data[name]
+
+
+class MockLoadParm(object):
+
+    def __init__(self, data):
+        self.data = data
+
+    def __getitem__(self, name):
+        return MockService(self.data[name])
+
+    def __contains__(self, name):
+        return name in self.data
+
+    def __len__(self):
+        return len(self.data)
+
+    def services(self):
+        return self.data.keys()
+
+
 class ShareTests(TestCase):
 
     def _get_shares(self, conf):
-        return SharesContainer(conf)
+        return SharesContainer(MockLoadParm(conf))
 
     def test_len_no_global(self):
         shares = self._get_shares({})
         self.assertEquals(0, len(shares))
 
+    def test_iter(self):
+        self.assertEquals([], list(self._get_shares({})))
+        self.assertEquals([], list(self._get_shares({"global":{}})))
+        self.assertEquals(["bla"], list(self._get_shares({"global":{}, "bla":{}})))
+
     def test_len(self):
         shares = self._get_shares({"global": {}})
         self.assertEquals(0, len(shares))
index d8dabe345873c80a8ebd4c1b888a909096c90fdd..4f6e2fff4960ca687341746c7abe00f2e0693064 100644 (file)
@@ -232,6 +232,20 @@ static PyObject *py_lp_ctx_private_path(py_talloc_Object *self, PyObject *args)
        return ret;
 }
 
+static PyObject *py_lp_ctx_services(py_talloc_Object *self)
+{
+       struct loadparm_context *lp_ctx = PyLoadparmContext_AsLoadparmContext(self);
+       const char **names;
+       PyObject *ret;
+       int i;
+       names = lp_server_services(lp_ctx);
+       ret = PyList_New(str_list_length(names));
+       for (i = 0; names[i]; i++) {
+               PyList_SetItem(ret, i, PyString_FromString(names[i]));
+       }
+       return ret;
+}
+
 static PyMethodDef py_lp_ctx_methods[] = {
        { "load", (PyCFunction)py_lp_ctx_load, METH_VARARGS, 
                "S.load(filename) -> None\n"
@@ -253,6 +267,8 @@ static PyMethodDef py_lp_ctx_methods[] = {
                "Change a parameter." },
        { "private_path", (PyCFunction)py_lp_ctx_private_path, METH_VARARGS,
                "S.private_path(name) -> path\n" },
+       { "services", (PyCFunction)py_lp_ctx_services, METH_NOARGS,
+               "S.services() -> list" },
        { NULL }
 };
 
index 622c3b648602067160af6c43bcc485de9779e964..89a312e855174506bf31561b5eaef5e8451c83d1 100644 (file)
@@ -41,8 +41,11 @@ class SharesContainer(object):
             return len(self._lp)-1
         return len(self._lp)
 
+    def keys(self):
+        return [name for name in self._lp.services() if name != "global"]
+
     def __iter__(self):
-        return self.lp.services()
+        return iter(self.keys())
 
 
 class Share(object):